Advanced Placement Computer Science AB:
Assignment 19:  Classes
   Sewickley Academy, 2000-2001

See Course Home Page.
 
Date Assigned: Fri Nov-3
Date Due: Mon Nov-6

1.  Write a class called TwoPlayerBoardGame which handles all the generic functions necessary to play a two player board game on an 8x8 board (like chess, checkers, othello, etc).  It should have a public function, playGame(), which should ask for both players' names, determine who goes first, then manage the entirety of playing a game.  It will call some virtual functions, such as the following (you may slightly modify these to your needs, but you must have something similar in spirit to this design):

int initializeBoard()
This should initialize the board according to the particular game.  A chessboard gets chess pieces, a checkerboard gets checkers, an othello board gets othello pieces.

bool makeMove(int row1, int col1, int row2, int col2)
This attempts to move from row1,col1 to row2,col2.  In a game such as Othello where you do not move pieces but just place new pieces on the board, row1,col1 are ignored.  (Question:  what can you do that is more elegant than this?)  The move (0,0,0,0) is a pass, which is never legal in some games (such as chess), but in fact may be required at times in other games, such as othello (if you have no legal move, you must pass; the game is over when neither side has a legal move).  In any case, this function returns true if the move was legal, and false otherwise.  If it returns false, the superclass prints some suitable message and asks the current player to re-enter their move.

void printSquare(int row, int col)
This prints the piece at this particular square.  The superclass will print a board, but while printing a board it will let the subclass determine how to print each square (though the superclass will output a newline after each row).

bool gameOver()
This is how the subclass tells the superclass that the game is over.  In chess, it is at checkmate.  In checkers, it is when the opponent has no more pieces.  In Othello, it is either when the opponent cannot make a move.

int getWinner()
This returns -1 if the game is not over, else 0 if player 0 won, and 1 if player 1 won.  Of course, the superclass knows the players actual names (the subclass never knows this) and so will have a pretty report, such as "Joey won that game!".

2.  Write a subclass of TwoPlayerBoardGame called Checkers.  It should implement the virtual functions listed above, and should (oddly enough) play checkers.  Do not worry about multiple jumps, but you should support kings, which are acquired when pieces reach the last row.

3.  For extra credit which is surprisingly easy after you have done parts 1 and 2:  Write another subclass called Othello, which should play othello.  The rules to this game are very simple, and are available in many locations online (such as here).



See Course Home Page.