/** * Class Board is the model (data structure) that holds and updates the * game status as Othello is played. It provides methods to initialize * the game (done automatically when the game is first created), make moves, * and access the game status, which can be used by client code to display * the game board and the state of the game * * @author Hal Perkins * @version CSE142 Homework 7, August 5, 2001 */ public class Board { // public constants /** Number of rows/columns in game board (must be even) */ public static final int NROWSCOLS = 8; /** Non-existant square */ public static final int BADSQUARE = -1; /** Empty board square */ public static final int EMPTY = 0; /** Board square containing black piece */ public static final int BLACK = 1; /** Board square containing white piece */ public static final int WHITE = 2; // private instance variables private int[][] board; // game board private int numBlack; // # of black pieces private int numWhite; // # of white pieces private int currentColor; // color currently moving private Othello display; // the gameboard display - use it to // update display after changes ///////////////////////////////////////////////////////////////////// // The following methods form the interface between the game board // // (this class) and the GUI display controller (class Othello) // // These interfaces should not be modified. The implementation of // // processClick WILL need to be changed to play Othello. // ///////////////////////////////////////////////////////////////////// /** Construct and initialize new game board * @param display The Othello object for this game */ public Board(Othello display) { this.board = new int[NROWSCOLS][NROWSCOLS]; this.display = display; this.newGame(); } /** Initialize new game */ public void newGame() { // set all board squares to empty for (int row = 0; row < NROWSCOLS; row++) { for (int col = 0; col < NROWSCOLS; col++) { this.board[row][col] = Board.EMPTY; } } // set initial game conditions this.board[NROWSCOLS/2-1][NROWSCOLS/2-1] = Board.WHITE; this.board[NROWSCOLS/2-1][NROWSCOLS/2 ] = Board.BLACK; this.board[NROWSCOLS/2 ][NROWSCOLS/2-1] = Board.BLACK; this.board[NROWSCOLS/2 ][NROWSCOLS/2 ] = Board.WHITE; this.numWhite = 2; this.numBlack = 2; this.currentColor = Board.WHITE; } /** Return white score */ public int getWhiteScore() { return this.numWhite; } /** Return black score */ public int getBlackScore() { return this.numBlack; } /** Return contents of game board square at given row/column * (Squares numbered from 0 to NROWSCOLS-1). */ public int getSquare(int row, int col) { if (inBounds(row,col)) { return this.board[row][col]; } else { return Board.BADSQUARE; } } /** Return color of the pieces currently moving */ public int getCurrentColor() { return this.currentColor; } /** Process a mouse click in the square at the given row and column. * If it is a legal move, process it, otherwise ignore. * (Called from user interface when a click is detected; assumes * row/col numbers are in bounds.) */ public void processClick(int row, int col) { // starter program code - needs to be modified to play Othello. // Place the current color on the selected square, then switch // to other color. Call display object to update picture this.board[row][col] = this.currentColor; this.toggleMove(); this.display.updateDisplay(); } //////////////////////////////////////////////////// // Methods beyond this point are private to Board // //////////////////////////////////////////////////// // Return color of other pieces not moving now private int getOther() { if (this.currentColor == Board.WHITE) { return Board.BLACK; } else { return Board.WHITE; } } // Change current move to other color private void toggleMove() { this.currentColor = getOther(); } // Return true if row and column number are inside the board, // otherwise return false private boolean inBounds(int row, int col) { return (row >= 0 && row < NROWSCOLS && col >= 0 && col < NROWSCOLS); } // Return true if it current player (Board.WHITE or Board.Black) // can make a move at the given row and column private boolean canMove(int row, int col) { // replace the body of this method with your code return false; // replace } // Make a move for the current player at the given row and column. // Precondition: canMove(row,col) is true when this is called. private void makeMove(int row, int col) { // replace this comment with your code } }