CS1101C Practical Exam

Session 4 (1600 - 1745 hours)

Connect Four

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

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

Background

Connect Four is a game played on a board with 6 rows and 8 columns. There are two players; White and Black. The White player always goes first. Each player takes turns to place a small disc of his own colour at a particular column. The board is placed vertically, so a disc will drop down as far as it can go.

Moves

The board initially starts off empty, as shown below:
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .

We use '.' (full-stop) to represent an empty slot.

The columns are numbered from 0 to 7, with 0 being the leftmost column.

White moves first, and suppose he places his disc at column 2. The white disc will drop all the way to the bottom, as shown below:

. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . W . . . . .

We use 'W' to represent a white disc, and 'B' to represent a black disc.

Now it is Black's turn, and suppose he places his disc at column 3. The black disc will drop all the way to the bottom, as shown below:

. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . W B . . . .

Suppose the board position is now as shown below:

. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . W . . . .
. . W B . . . .

If Black places his disc at column 3, it will land on the white disc, as shown below:

. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . B . . . .
. . . W . . . .
. . W B . . . .

Winning

A player wins whenever he manages to get four of his own colour discs lined up consecutively in a straight line, either horizontally, vertically, or diagonally. Here are some examples:
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
B B B . . . . .
W W W W . . . .

In the above example, white wins, and the coordinates of the winning line are from (5,0) to (5,3), or (5,3) to (5,0). Note that (0,0) is the top left-hand corner, and (5,0) is the bottom left-hand corner.

. . . . . . . .
. . . . . . . .
. . B . . . . .
. . B . W . . .
. . B . W . . .
. . B . W . W .

In the above example, black wins, and the coordinates of the winning line are from (2,2) to (5,2). Note that the winning line is also from (5,2) to (2,2); the order does not matter.

. . . . . . . .
. . . . . . . .
. . . W . . . .
W B W B . . . .
B W B W . . . .
W B W B . . . .

In the above example, white wins, and the coordinates of the winning line are from (5,0) to (2,3). Note that the winning line is also from (2,3) to (5,0); the order does not matter.

Task

Your program should read a list of moves for each player from a text file.

Text File

Each line in the text file called connect1.txt represents a player's move according to the column number, starting with White, and then alternating with Black, then White, then Black, etc. This text file has been copied into your directory by the pesetup command. Here is the text file connect1.txt:
2
3
0
1
1
2
3
0
2
3
0
1
3
0
1
2

This says that White first places his disc at column 2, then Black places his disc at column 3, then White places his disc at column 0, then Black places his disc at column 1, and so on.

You may assume that the column number is always from 0 to 7 inclusive, and that players will never place a disk on a column that is already full. No error checking is required.

Your program must keep reading player's moves, and terminate immediately whenever a player wins, even though there may be further moves stored in the text file. If no one wins after reading all the moves from the text file, it is a draw. Before termination, display the final state of the board, and print out the result: White wins, Black wins, or Draw result.

When a player wins, display also the coordinates of the starting and ending point of the winning line. The starting and ending point can be in any order. If the winning line contains more than four discs, the starting and ending points must contain exactly four discs. If there are multiple winning lines for a single colour, display any one of the winning lines.

Sample Run

Assuming the sample file connect1.txt contains lines as shown above, the following is a sample run:
. . . . . . . .
. . . . . . . .
. . . W . . . .
W B W B . . . .
B W B W . . . .
W B W B . . . .
White wins: (5,0) to (2,3)
You are reminded to follow the sample output exactly; else marks will be deducted.

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