CS1101C Practical Exam

Session 1 (0800 - 0945 hours)

Queens

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

The deadline for this lab is Wednesday 13 April 2005, 09:45 hours. Strictly no submissions will be accepted after the deadline.

Background

A game of chess is conducted on an 8 x 8 board with 64 squares. Queens are the most powerful piece in a game of chess. They can move in any of the eight directions: four directions (up, down, left, right), and the four diagonals. They can move as many squares as they wish until obstructed by a piece. Queens may not jump over other queens.

One Queen

The following shows a single queen (marked by the letter Q) on an 8 x 8 board. The squares which a queen may move to or attack are shown by the lines.

.\..|../
..\.|./.
...\|/..
----Q---
.../|\..
../.|.\.
./..|..\
/...|...

Single Attack

The following shows three queens (marked by the letter Q) on an 8 x 8 board. Two of the queens are attacking each other since they are in each other's paths, shown by the line. The third queen is not involved in any attacks. The number of attacking lines in the following case is 1.

.Q......
..\...Q.
...\....
....\...
.....\..
......\.
.......Q
........

Adjacent Queens

The number of attacking lines in the following case is also 1. Adjacent queens also attack each other.

........
....Q...
...Q....
........
........
........
........
........

The number of attacking lines in the following case is 3.

........
....Q...
...QQ...
........
........
........
........
........

No attacks

The number of attacking lines in the following case is 0.

..Q.....
....Q...
.Q......
.......Q
Q.......
......Q.
...Q....
.....Q..

Multiple Attacks

The number of attacking lines in all the following cases is 2. Note that queens cannot jump over other queens.

.Q......  ........  ........
..\.....  ..Q.....  ........
...\....  ...\....  ...Q....
....Q...  ....Q...  ....Q...
.....\..  .....Q..  .....Q..
......\.  ........  ........
.......Q  ........  ........
........  ........  ........


The number of attacking lines in the following case is 3.

.Q-----Q
..\....|
...\...|
....\..|
.....\.|
......\|
.......Q
........


The number of attacking lines in the following case is 6.

.Q-----Q
.|\.../|
.|.\./.|
.|..X..|  The X represents the intersection of the diagonal lines.
.|./.\.|
.|/...\|
.Q-----Q
........


The number of attacking lines in the following case is 11.

........
.QQ.....
.QQ.....
.QQ.....
........
........
........
........


The number of attacking lines in the following case is 20.

........
.QQQ....
.QQQ....
.QQQ....
........
........
........
........


Objective

The objective of your program is to read in a text file containing queens on a chess board and determine the number of attacking lines for the layout of the queens.

Text File

To simply matters, the layout is stored in a text file called queens1.txt containing only integers. The following is a sample text file. We use 0 to indicate an empty square, and 1 to indicate a queen. So there are four queens in the following layout.

0 1 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 1
0 0 0 0 0 0 0 0

You can find other text files (queens2.txt, queens3.txt, etc.) in your practical exam account. We will test your program with other text files.

Sample Run

The following shows a sample run when using the file queens1.txt as shown above. Print out the chess board in the format shown below, using a full-stop for an empty square, and a letter Q to indicate a queen. You do not need to show the actual attacking lines on the board. Finally, display the number of attacking lines.

Follow the sample run exactly or you will not get any marks for the test cases. The UNIX prompt is shown by the $ sign.


$ gcc -Wall queens.c -o queens
$ ./queens
.Q.....Q
........
........
........
........
........
.Q.....Q
........
Attacking lines: 6
$

Remember to submit your program using the submit queens.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.