Explore BrainMass

Checkers board game in C

In this project, you will build a program that allows two human players to play the game of checkers. Your program will graphically maintain the state of the game board and prompt the players for moves on the standard input/output. Your program will also know the rules of checkers, and will check the validity of moves to maintain the integrity of the game board as the game progresses.

1 Rules of Checkers (slightly modified for this project)
Checkers is played on an 8 by 8 grid, or "checker board," with alternating dark and light squares. The board starts with 12 red and 12 white pieces, each situated on the 12 dark squares at opposing ends of the board. (Actually, the entire game is played on the dark squares only). Players take turns making moves, with the player of the red pieces moving first. Two kinds of moves are allowed. A player can "step" a piece one square diagonally if the diagonally adjacent square is unoccupied. Alternatively, if a player's piece is next to an opponent's piece, and the square beyond it is free, the player can "jump" over the opponent's piece onto the unoccupied square. The opponent's piece is removed from the board after the jumping move.

In the beginning, pieces can only move and jump forward. However, if a piece reaches the far end of the board, then it becomes a "king." A king is allowed to move and jump diagonally backwards and forwards. Kings can be captured like any other piece.

2. Maintaining the Game Board
Your checkers game should maintain the state of the game board, and print it to standard output using text characters. The squares of the board should be printed using the '-' and '|' characters; the row and column numbers of the board squares should be printed above and to the left of the board; and squares containing pieces should be labeled "r" and "w" for normal red and white pieces, respectively, and "R" and "W" for red and white pieces that are kings. For example, at the very beginning of the game (before any moves have been made), the board should look like:

0 1 2 3 4 5 6 7
0 | | r | | r | | r | | r |
1 | r| | r | | r | | r | |
2 | | r | | r | | r | | r |
3 | | | | | | | | |
4 | | | | | | | | |
5 | w | | w | | w | | w |
6 | | w | | w | | w | | w |
7 | w | | w | | w | | w | |

3 Moving Pieces
Your checkers program should prompt each player for a move, alternating between the red and white pieces. For example, if it's the red pieces' turn to move, your program should say:

RED's move:

At the prompt, your program should expect the corresponding player to enter 4 numbers:
the first 2 numbers specify the column and row, in that order (i.e., an x-y coordinate), of a square containing the piece to move, and the second 2 numbers specify the column and row of an empty square to move to. After a valid move is entered (see Section 4), your program should update the state of the game board, print the updated game board, and then prompt the opposing player for the next move.

4 Verifying Moves
After a player enters a move, but before the game board is updated, your program should verify that the move entered is a valid move. Your program should check several conditions:
1. The move should originate from and terminate to squares on the board (i.e., the
move should stay on the 8x8 grid).
2. The originating square must contain a piece belonging to the player making the
move, while the terminating square must be empty.
3. The move must be a legal stepping move or jumping move. A legal stepping move must move 1 square away along a diagonal. Normal pieces can only move in the
"forward direction" (for red, this means towards increasing y coordinates, and for
white, this means towards decreasing y coordinates); kings can move in both the
forward and backward directions.
4. A legal jumping move must move 2 squares away along a diagonal with the "jumped" square containing an opponent's piece. Similar to stepping moves, normal pieces can only jump in the forward direction; kings can jump in both the forward and backward directions.
If a move meets all these conditions, it is a valid move, and your program should update the game board accordingly. (For jumping moves, this includes removing the piece that was jumped). Otherwise, your program should discard the move, print the error message
"INVALID MOVE. TRY AGAIN!!", and prompt the same player for another move. Re-
prompting continues until a valid move is entered.
(Note, in the official rules of checkers, if a player can perform a jump, only a jumping move is valid. You do not have to enforce this rule in your program. Even if the player can make a jumping move, either a stepping move or jumping move is valid.)

5 Kings
Whenever a normal piece lands on a square in the far row of the board (rows 7 and 0 for red and white pieces, respectively), the piece becomes a king. When this happens, your program should capitalize the letter used to represent the piece on the game board (i.e., change "r" to "R" and "w" to "W" for red and white pieces, respectively). As described earlier, kings can step and jump in both the forward and reverse directions.

6 Multiple Jumps
The official rules of checkers allows a player to jump many times in a row with the same piece, capturing several of the opposing pieces on the same turn. This is known as "multiple jumps." You are not required to implement multiple jumps in this project. After a player makes a jumping move, you should assume the player's turn is over, and you should prompt the opposing player for a move.

7 Termination
Normally, a game of checkers ends when a player captures all of his/her opponent's pieces, or until all of a player's pieces are blocked so that they cannot move. In this project, you do not need to detect these game-ending conditions. Instead, your program should simply process moves, alternating between players. You may assume this continues forever. (Eventually, one player may capture all of another player's pieces, but you can assume this never happens).

A possible strategy is to implement the project's functionality in the following order:
? Print the game board
? Implement stepping moves for normal pieces
? Verify move conditions 1, 2, and 3 for normal pieces
? Implement jumping moves for normal pieces
? Verify move condition 4 for normal pieces
? Implement kings (stepping and jumping moves)
? Update your move verification for conditions 1, 2, 3, and 4 to handle kings