CS1101C Practical Exam

Session 1 (0800 - 0945 hours)

Checkers

The name of your C program file must be called checkers.c, files with any other name will not be marked.

The deadline for this lab is Wednesday 15 November 2006, 09:45:59 hours. Strictly no submissions will be accepted after the deadline.

Background

Checkers is a game played on an 8 x 8 chessboard. The two players are called Red and Black. The initial layout of the game is shown below:

. b . b . b . b
b . b . b . b .
. b . b . b . b
. . . . . . . .
. . . . . . . .
r . r . r . r .
. r . r . r . r
r . r . r . r .

The meanings of the symbols are:

There are two other pieces. Their symbols are:

Moves

The coordinate of the top-left square is (0, 0). The coordinate of the top-right square is (0, 7).

All pieces can only move one square diagonally. Red Peasants move and capture only upwards, Black Peasants move and capture only downwards. Pieces may not move into a square that is occupied by any other existing piece. Players take turns to move, with Red moving first.

The Red Peasant at (5, 6) can move to either (4, 5) or (4, 7).

Suppose two moves are made:

  1. (5, 6) to (4, 7)
  2. (2, 5) to (3, 6)

The board now looks like:

. b . b . b . b
b . b . b . b .
. b . b . . . b
. . . . . . b .
. . . . . . . r
r . r . r . . .
. r . r . r . r
r . r . r . r .

Captures

A piece captures another piece by jumping over it. Once a piece is captured, it is removed from the board. From the layout above, the Red Peasant at (4, 7) can capture the Black Peasant at (3, 6). The capture is shown as a move from (4, 7) to (2, 5). The resulting layout is:

. b . b . b . b
b . b . b . b .
. b . b . r . b
. . . . . . . .
. . . . . . . .
r . r . r . . .
. r . r . r . r
r . r . r . r .

Now, either of the Black Peasants at (1, 4) or (1, 6) can capture the Red Peasant at (2, 5). Note that pieces are not forced to capture, and each piece can only make one capture per move.

Promotions

If a Red Peasant reaches the top row either by moving or capturing, he is promoted to a Red King. If a Black Peasant reaches to the bottom row either by moving or capturing, he is promoted to a Black King. A Red King is identical to a Red Peasant, except that he in addition to moving forward (one square), he has the option to move backward (one square). He can also capture forwards or backwards. A Black King moves and captures in the same way as a Red King. As with Peasants, Kings are not forced to capture, and each King can only make one capture per move.

Data File

A text input file called moves1.txt (which has been copied into your directory by the pesetup command) contains all the moves in a checkers game. Your job is to read the moves from this file, execute all the moves in sequence, and show the final layout of the board. Show also the number of pieces captured as shown in the sample output.

Suppose the lines of the text file contains:

5 6 4 7
2 5 3 6
4 7 2 5

Each move in contained in one line. The first line says move a piece from (5, 6) to (4, 7). The second line says move a piece from (2, 5) to (3, 6). The third line says move a piece from (4, 7) to (2, 5). The third move is actually a capture so the board and statistics must both be updated correctly.

You do not need to do any error checking. You may assume that all the moves and captures are valid and that players take turns to move their pieces.

Assuming the text input file shown above, the following is the correct output of the program. Follow it precisely, else marks may be deducted!

Initial layout:
. b . b . b . b
b . b . b . b .
. b . b . b . b
. . . . . . . .
. . . . . . . .
r . r . r . r .
. r . r . r . r
r . r . r . r .

Final state:
. b . b . b . b
b . b . b . b .
. b . b . r . b
. . . . . . . .
. . . . . . . .
r . r . r . . .
. r . r . r . r
r . r . r . r .

Red peasants captured: 0
Black peasants captured: 1
Red kings captured: 0
Black kings captured: 0

We will test your programs with other (more complicated) text input files.

Do not use any structures in your program, else no credit will be given.

Remember to submit your program using the submit checkers.c command, and check your submission using the check command.

All the best!

UNIX commands

Some useful UNIX commands (in case you forgot what you did in Lab 0):

  1. dir”: lists all the files in the directory.
  2. cp a.txt b.txt”: copies a.txt to b.txt.
  3. mv a.txt b.txt”: moves / renames a.txt to b.txt.
  4. cat a.txt”: shows the contents of a.txt.