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

Write a java program to implement Checkers game. Solution import java.awt.*; imp

ID: 639740 • Letter: W

Question

Write a java program to implement Checkers game.

Explanation / Answer

import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.Vector; public class Checkers extends Applet { /* The main applet class only lays out the applet. The work of the game is all done in the CheckersCanvas object. Note that the Buttons and Label used in the applet are defined as instance variables in the CheckersCanvas class. The applet class gives them their visual appearance and sets their size and positions.*/ public void init() { setLayout(null); // I will do the layout myself. setBackground(new Color(0,150,0)); // Dark green background. /* Create the components and add them to the applet. */ CheckersCanvas board = new CheckersCanvas(); // Note: The constructor creates the buttons board.resignButton // and board.newGameButton and the Label board.message. add(board); board.newGameButton.setBackground(Color.lightGray); add(board.newGameButton); board.resignButton.setBackground(Color.lightGray); add(board.resignButton); board.message.setForeground(Color.green); board.message.setFont(new Font("Serif", Font.BOLD, 14)); add(board.message); /* Set the position and size of each component by calling its setBounds() method. */ board.setBounds(20,20,164,164); // Note: size MUST be 164-by-164 ! board.newGameButton.setBounds(210, 60, 100, 30); board.resignButton.setBounds(210, 120, 100, 30); board.message.setBounds(0, 200, 330, 30); } } // end class Checkers class CheckersCanvas extends Canvas implements ActionListener, MouseListener { // This canvas displays a 160-by-160 checkerboard pattern with // a 2-pixel black border. It is assumed that the size of the // canvas is set to exactly 164-by-164 pixels. This class does // the work of letting the users play checkers, and it displays // the checkerboard. Button resignButton; // Current player can resign by clicking this button. Button newGameButton; // This button starts a new game. It is enabled only // when the current game has ended. Label message; // A label for displaying messages to the user. CheckersData board; // The data for the checkers board is kept here. // This board is also responsible for generating // lists of legal moves. boolean gameInProgress; // Is a game currently in progress? /* The next three variables are valid only when the game is in progress. */ int currentPlayer; // Whose turn is it now? The possible values // are CheckersData.RED and CheckersData.BLACK. int selectedRow, selectedCol; // If the current player has selected a piece to // move, these give the row and column // containing that piece. If no piece is // yet selected, then selectedRow is -1. CheckersMove[] legalMoves; // An array containing the legal moves for the // current player. public CheckersCanvas() { // Constructor. Create the buttons and lable. Listen for mouse // clicks and for clicks on the buttons. Create the board and // start the first game. setBackground(Color.black); addMouseListener(this); setFont(new Font("Serif", Font.BOLD, 14)); resignButton = new Button("Resign"); resignButton.addActionListener(this); newGameButton = new Button("New Game"); newGameButton.addActionListener(this); message = new Label("",Label.CENTER); board = new CheckersData(); doNewGame(); } public void actionPerformed(ActionEvent evt) { // Respond to user's click on one of the two buttons. Object src = evt.getSource(); if (src == newGameButton) doNewGame(); else if (src == resignButton) doResign(); } void doNewGame() { // Begin a new game. if (gameInProgress == true) { // This should not be possible, but it doens't // hurt to check. message.setText("Finish the current game first!"); return; } board.setUpGame(); // Set up the pieces. currentPlayer = CheckersData.RED; // RED moves first. legalMoves = board.getLegalMoves(CheckersData.RED); // Get RED's legal moves. selectedRow = -1; // RED has not yet selected a piece to move. message.setText("Red: Make your move."); gameInProgress = true; newGameButton.setEnabled(false); resignButton.setEnabled(true); repaint(); } void doResign() { // Current player resigns. Game ends. Opponent wins. if (gameInProgress == false) { message.setText("There is no game in progress!"); return; } if (currentPlayer == CheckersData.RED) gameOver("RED resigns. BLACK wins."); else gameOver("BLACK resigns. RED winds."); } void gameOver(String str) { // The game ends. The parameter, str, is displayed as a message // to the user. The states of the buttons are adjusted so playes // can start a new game. message.setText(str); newGameButton.setEnabled(true); resignButton.setEnabled(false); gameInProgress = false; } void doClickSquare(int row, int col) { // This is called by mousePressed() when a player clicks on the // square in the specified row and col. It has already been checked // that a game is, in fact, in progress. /* If the player clicked on one of the pieces that the player can move, mark this row and col as selected and return. (This might change a previous selection.) Reset the message, in case it was previously displaying an error message. */ for (int i = 0; i = 8 || c2 < 0 || c2 >= 8) return false; // (r2,c2) is off the board. if (board[r2][c2] != EMPTY) return false; // (r2,c2) already contains a piece. if (player == RED) { if (board[r1][c1] == RED && r2 > r1) return false; // Regualr red piece can only move down. return true; // The move is legal. } else { if (board[r1][c1] == BLACK && r2
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