Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

More rules: Methods to be created: Thanks in advance! Objective: Create a two-pl

ID: 3902715 • Letter: M

Question

More rules:

Methods to be created:

Thanks in advance!

Objective: Create a two-player checkers game. Background: Rules of Checkers: *indicates a rule not enforced by the program Checkers is played on an 8 by 8 board Rows labeled 1-8 Columns labeled A-H o o Two players: red and black .Each player starts with 12 pieces . Players make one move per turn* . Each tile may be occupied by at MOST one piece at a time A piece may move one tile diagonally towards the other players side o Once other side is reached the piece gets promoted to a "king" King pieces may move one tile diagonally in ANY direction (forward or back) A player's piece may "jump" an opponent's piece if there is an open slot directly on the other side of the opponent's piece Place the player's piece on the other side of the opponent's piece and remove the opponent's piece from play If there is a jump the jumping player takes another turn* o o If there is the ability to make a jump the player MUST take that jump* » If a player jumps an opponent's piece they take an additional turn Game ends when any player loses all their pieces*

Explanation / Answer

public class CheckersGUI {

private final JPanel gui = new JPanel(new BorderLayout(3, 3));

private JButton[][] Squares = new JButton[8][8];

private JPanel Board;

private final JLabel message = new JLabel(

"Ready to play!");

private static final String COLS = "ABCDEFGH";

private ImageIcon brown = new ImageIcon("brown.jpg");

private ImageIcon red = new ImageIcon("red.png");

CheckersGUI() {

create();

//create ButtonHandler

/* ButtonHandler handler = new ButtonHandler();

for (int i=0; i<Squares.length; i++){

for (int j=0; i<Squares[0].length; i++){

Squares[i][j].addMouseListener(handler);

}

}

}

private class ButtonHandler implements MouseListener{

public void MouseClicked ( MouseEvent event){

}*/

}

public final void create() {

// set up the main GUI

gui.setBorder(new EmptyBorder(5, 5, 5, 5));

JToolBar tools = new JToolBar();

tools.setFloatable(false);

gui.add(tools, BorderLayout.PAGE_START);

Action newGameAction = new AbstractAction("New") {

@Override

public void actionPerformed(ActionEvent e) {

setupNewGame();

}

};

tools.add(newGameAction);

// TODO - add functionality!

tools.addSeparator();

tools.add(message);

gui.add(new JLabel(""), BorderLayout.LINE_START);

Board = new JPanel(new GridLayout(0, 9)) ;

Board.setBorder(new CompoundBorder(

new EmptyBorder(8,8,8,8),

new LineBorder(Color.BLACK)

));

JPanel boardConstrain = new JPanel(new GridBagLayout());

boardConstrain.add(Board);

gui.add(boardConstrain);

// create the chess board squares

Insets buttonMargin = new Insets(0, 0, 0, 0);

for (int ii = 0; ii < Squares.length; ii++) {

for (int jj = 0; jj < Squares[ii].length; jj++) {

JButton b = new JButton();

b.setMargin(buttonMargin);

ImageIcon icon = new ImageIcon(

new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB));

b.setIcon(icon);

if ((jj % 2 == 1 && ii % 2 == 1)

|| (jj % 2 == 0 && ii % 2 == 0)) {

b.setBackground(Color.WHITE);

} else {

b.setBackground(Color.BLACK);

}

Squares[jj][ii] = b;

}

}

/*

* fill the chess board

*/

Board.add(new JLabel(""));

// fill the top row

for (int ii = 0; ii < 8; ii++) {

Board.add(

new JLabel(COLS.substring(ii, ii + 1),

SwingConstants.CENTER));

}

// fill the black non-pawn piece row

for (int ii = 0; ii < 8; ii++) {

for (int jj = 0; jj < 8; jj++) {

switch (jj) {

case 0:

Board.add(new JLabel("" + (9-(ii + 1)),

SwingConstants.CENTER));

default:

Board.add(Squares[jj][ii]);

}

}

}

}

public final JComponent getGui() {

return gui;

}

/**

* Initializes the icons of the initial chess board piece places

*/

private final void setupNewGame() {

message.setText("Make your move!");

for (int ii = 0; ii < 8; ii++) {

Squares[ii][1].setIcon(red);

}

for (int ii = 0; ii < 8; ii++) {

Squares[ii][2].setIcon(red);

}

for (int ii = 0; ii < 8; ii++) {

Squares[ii][5].setIcon(brown);

}

for (int ii = 0; ii < 8; ii++) {

Squares[ii][6].setIcon(brown);

}

}

public static void main(String[] args) {

CheckersGUI cg = new CheckersGUI();

JFrame f = new JFrame("Checkers");

f.add(cg.getGui());

f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

// ensures the frame is the minimum size it needs to be

// in order display the components within it

f.pack();

f.setResizable(false);   

f.setVisible(true);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote