CS1101C Lab 4 (Odd Week)
Let's Play Chess!
The deadline for this lab question is Wednesday 30 March 2005,
23:59:59 hours.
The name of your C program file must be called chess.c,
files with any other name will not be marked.
Background
Chess is a fascinating game of skill and strategy where expert players
pit their intelligence and wit against each other on a square board.
Each chess board is an 8 x 8 board containing 64 squares.
The Teams
The two teams are called “White” (abbreviated as
“W”) and “Black” (abbreviated as
“B”). A chess piece is either a White piece or a Black
piece.
The Pieces
There are six different types of chess pieces. Their names and
abbreviations are shown in the following table:
Piece |
Abbreviation |
Pawn |
P |
Knight |
N |
Bishop |
B |
Rook |
R |
Queen |
Q |
King |
K |
A chess piece is abbreviated by its colour followed by the type of
piece. Here are some examples:
- WP = White Pawn.
- BN = Black Knight.
- WR = White Rook.
- BQ = Black Queen.
The Board
The chess board consists of an 8 x 8 square board with 64 squares. The
initial position of a chess board is shown below:
BR BN BB BQ BK BB BN BR
BP BP BP BP BP BP BP BP
.. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. ..
WP WP WP WP WP WP WP WP
WR WN WB WQ WK WB WN WR
Note that we use two periods (or full-stops) to indicate an empty
square. Each square can only be occupied by one piece at a time. So, the
top left hand corner is occupied by the Black Rook, and on its right is
the Black Knight, and so on. The four middle rows begin as all empty
squares.
Each square has coordinates in the form (r,c), where r is the row
number and c is the column number. The top-left square has coordinates
(0,0). The bottom right square has coordinates (7,7). So, in the initial
position of a chess board shown above, the Black Queen (BQ) has
coordinates (0,3).
Chess Moves
A chess move is simply specified by a source coordinate and a
destination coordinate. It means that the piece at the source coordinate
is to be moved to the destination coordinate. So, if the source
coordinate is (6,4) and the destination coordinate is (4,4), it means
that the White Pawn which is in front of the White King will move two
steps forward.
Each chess move is stored in a data file as four integers; source_row,
source_column, destination_row, destination_column. The above
chess move will be stored as:
6 4 4 4
Suppose another chess move is:
0 1 2 3
this means that the piece at coordinate (0,1) will be moved to
coordinate (2,3).
The data file contains a list of such chess moves. The number of moves
in each data file is unknown.
Captures
If the source coordinate and the destination coordinate represent two
pieces of different colour, the piece at the destination coordinate is
captured and removed by the piece at the source coordinate. The piece at
the source coordinate then moves to the destination coordinate.
Invalid Move
For simplicity, we assume that the data file contains mostly legal
moves. There are only two types of erroneous moves we want to catch
and disallow:
- “Friendly fire” errors: the piece at the source
coordinate is the same colour as the piece at the destination
coordinate.
- “No one’s there” errors: there is no piece at the
source coordinate.
We are not concerned with all other errors, including pieces not moving
as they do in a normal chess game, pieces jumping over pieces, players
not taking turns, etc. You may also assume that all the coordinates are
always between 0 to 7 inclusive.
Purpose
The purpose of your program is to simulate a chess game starting from
the initial chess board position, and reading a list of moves from a
data file. Your program must then show the final chess board position as
well as print a list of statistics showing the number of each type of
erroneous moves and the number of each piece captured.
Statistics
The statistics we want to capture are:
- Number of each type of piece captured (e.g. WP, WR, ...)
- Number of “friendly fire” erroneous moves.
- Number of “no one’s there” erroneous moves.
Sample Run
Assume that the text input file is always called
chess1.txt.
You may copy the file into your current directory by typing the
following cp (copy) command at the Unix prompt. Highlight the
entire line, right-click on the mouse and choose
“Copy”, then switching to your Secure Shell Window,
right-click on the mouse and choose “Paste”. Press Enter to
execute the command.
cp ~cs1101cl/www/lab4/oddweek/chess1.txt .
The following shows one sample run. Your program must work with other
chess1.txt
files that contain different text. Note that
$
indicates the Unix prompt. Follow the sample output
exactly, else marks will be deducted.
Sample Run 1
The following shows a sample run for the sample text input file chess1.txt
.
$ gcc -Wall chess.c -o chess
$ ./chess
Final position:
BR .. BB BK .. BB .. BR
BP BP BP .. .. BP BP BP
.. .. BP .. .. .. .. ..
.. .. .. .. WP BN .. ..
.. .. .. .. .. .. .. ..
.. .. .. .. .. WN .. ..
WP WP WP .. .. WP WP WP
WR WN WB .. .. WR WK ..
Capture statistics:
WP: 1
WR: 0
WN: 0
WB: 1
WQ: 1
WK: 0
BP: 1
BR: 0
BN: 1
BB: 0
BQ: 1
BK: 0
Error statistics:
Friendly fire: 1
No one's there: 1
$
Things To Notice
- The first line in the text input file
chess1.txt
contains a “friendly
fire” erroneous move.
- The last line in the text input file
chess1.txt
contains a “no
one’s there” erroneous move.
- The two possible types of errors can occur anywhere in the file, not
necessarily just the first and last lines. The above is just an example
text input file.
All The Best!
Luck does not feature in programming.
This document, index.html, has been accessed 27 times since 25-Jun-24 11:57:13 +08.
This is the 1st time it has been accessed today.
A total of 16 different hosts have accessed this document in the
last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 8 times.
If you're interested, complete statistics for
this document are also available, including breakdowns by top-level
domain, host name, and date.