Checkers - This applet lets two uses play checkers against each other Red always starts the game If a player can jump an opponent's piece then the

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
/* This applet lets two uses play checkers against each other. Red always starts the game. If a player can jump an opponent's piece, then the player must jump. When a plyer can make no more moves, the game ends. This file defines four classes: the main applet class, Checkers; CheckersCanvas, CheckersMove, and CheckersData. (This is not very good style; the other classes really should be nested classes inside the Checkers class.) */ * import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.Vector; i public class Checkers extends Applet { p /* 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(; 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); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
} // end class Checkers } class CheckersCanvas extends Canvas implements ActionListener, MouseListener { c // 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;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/15/2010 for the course CSC 115 taught by Professor Jackson during the Spring '09 term at University of Victoria.

Page1 / 13

Checkers - This applet lets two uses play checkers against each other Red always starts the game If a player can jump an opponent's piece then the

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online