Course resources for the Introduction to Computer Science I by James Tam Return to the course web page

CPSC 231: Assignment 5

Due: 4 PM Friday December 9

New Concepts

Introduction

For this assignment you’ll be asked to implement a simplified version of the checkers game.

Checkers is a game that's played on a board with 64 squares (8 rows x 8 columns). There are two players: one controls 12 white pieces, while the other controls 12 red pieces. The game is run on a turn-by-turn basis during which each player can move one of his/her pieces or take an opposing piece. Player's alternate turns.

In this simplified version of checkers, there are only two possible types of moves for each player.

  1. Pieces can move to an adjacent, empty square  that is left, right, forward or backward (Figure 1). When a piece is moved, the previous square is empty and the destination will contain the piece that was moved.

    Figure 1: For each piece there is a maximum of 4 valid destinations

  2. An opponent’s piece can be taken from the board (Figure 2). The following conditions must hold for this to be possible.

    1. The player’s piece is adjacent (left, right, forward, or backwards) from an opponent’s piece.

    2. The square after the opponent’s piece (continuing in the same direction) is empty, the player can take the opponent’s piece.


 

 

 

Figure 2A: Valid capture

 

Figure 2B: Invalid capture (destination not empty)

The game is over when all of one of the player's pieces has been removed from the game. (This player loses the game.)

Program design

Your program needs to be written using the three classes: Piece, Checkers and Board. Each of these classes should have methods and attributes that are appropriate for the class.

A partial solution [Link to the starting code] is provided if you wish to use it. This solution contains a complete implementation for the Piece and Checkers classes and a partial solution for the Board class. The methods that are required in the Board class are clearly indicated.

If you wish you can write your own code for class Piece and class Checkers but it is isn't recommended because it will add a fair amount of work and you won't get credit for the code in these classes.

Similar to previous assignments your program will be marked both for functionality and quality of design and code.

Program requirements

In order to get full credit for the assignment you are to follow the rules stated in this assignment description rather than any other outside sources. Your program will be played by two human players, there is no computer-controlled opponent.

When the game is started, the user must be presented with option of continuing a saved game. If the user does not wish to start with a saved game, the first player to take a turn should be white and board should be set up as follows:

 

If the user does want to continue a saved game, then ask the user for the name of the file the game was saved in and read the board configuration and the player whose turn it is from the file. The next section contains a description of the format that the game should be saved in.

After the starting positions of the pieces have been determined the game will proceed in the following fashion:

  1. Display the board and current mode (in this case it's white)

  2. White player's turn1

  3. Determine if the white player has won, end the game as appropriate.

  4. Display the board and current mode (in this case it's red)

  5. Red player's turn

  6. Determine if the red player has won, end the game as appropriate.

1 Whenever a new game starts the white player gets the first move. If the game has been saved and reloaded from file then the game will resume where it left off (during the red or the white player's turn as appropriate).

The above cycle repeats until either a player quits or the game has been won. The game can end during either player's turn.


During a single player’s turn

  1. Display a menu of options: (m)ove a piece (s)ave the game (q)uit the game without saving. (The game should work if either upper or lower case input has been entered). When the quit option is selected, the game and program should be ended. When the save option is selected, the game configuration should be saved in file (board configuration described at the end of this document and player turns format described in next step 2). Choosing to save the game does not end the player’s turn. The same player is presented with another option to select. The next point describes the move option.

  2. When the move option is selected, the game will prompt the player for a row and column of the piece to move. Continue to prompt the user until a valid piece has been selected.

    1. Valid values for rows and columns are between 1 and 8.

    2. A valid square must contain the player’s piece and it must be possible to move the piece.

    3. The player can skip his/her turn by entering 0 for either the row or column.

  3. Prompt the user for a direction to move the piece. Present the user with a menu of valid directions.

    1. Directions have been mapped to locations of the number pad on computer keyboards (4 = left, 6 = right and 8 = up 2 = down)

    2. Selecting zero for the direction indicates that the player has chosen to pass his/her move.

    3. Re-prompt user until a valid choice has been entered for the piece that was selected.




Figure 4: Movement menu

After each player has moved, the game should determine if the game has been won and if so the game should immediately end with an appropriate and correct congratulatory message (e.g., red player wins) and display the board one last time. (It should not proceed to the next player's turn because that person will have no pieces to move). Remember that the game can also end whenever either player selects 'q' at the main menu.


Format of the input file

Each line of the input file (up to line 8) will determine the position of pieces on one row of the board. A single character will either represent the position of a piece or an empty square. As a character is read in from the file, a 'Piece' object will be created and its appearance will match the character from the file.

For example if a line of the text file contained the characters: r<sp>w<sp><sp><sp><sp><sp> then the corresponding row of the board would contain 8 Piece objects. The first and third objects would have their appearance attribute set to 'r' and 'w' respectively whereas the rest would have their appearance set to a blank space. Line 9 indicates at which turn the game left off (W = white turn, R = red turn). By default the white player goes first so the starting input file contains a 'W'. Later if the game is saved, line 9 may contain an 'R' (if the game was saved during the red player's turn).

To make the file format consistent for marking, your program must be able to read the starting positions from the following file: [input.txt]. There is another input file that you can use to test the win game conditions: [testWins.txt] (It's recommended that you may also make additional versions of the input file for testing other scenarios).

Reminders