CS1101C Practical Exam

Session 6 (1000 - 1145 hours)

Mine Sweeper

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

The deadline for this lab is Wednesday 21 November 2007, 11:45:59 hours. Strictly no submissions will be accepted after the deadline.

Background

Mine Sweeper was a common game on Windows PCs in the 1990s. The objective of the game was to locate mines on a board based on hints that are given on empty squares.

Board

The game is played on a board with 16 rows and 30 columns. The top left-hand column has coordinates (0,0). The top right-hand column has coordinates (0, 29).

Each square can either be empty, or it can contain a mine.

Text File

The layout of the board is stored in a sample text file called “msweeper.txt” which has been copied into your directory by the pesetup command. An integer of 1 indicates that the square contains a mine; an integer of 0 indicates that the square is empty.

The contents of the sample text file is shown below:

0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 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 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 0 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

This can be printed on the screen in the following format, where we use a full-stop (or period) to indicate an empty square, and an asterisk to indicate a mine.

.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*****.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*****.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
..............................
..............................
..............................
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.

Hints

Hints allow the player to determine the location of the mines. Hints are placed on empty squares only, and it is simply a number between 1 to 8 inclusive that states the number of mines that are adjacent to it. Here, adjacent means horizontally, vertically, or diagonally. Here are some examples of a smaller board.

Suppose there is just one mine as shown below:

.....
.....
..*..
.....
.....

We place hints on empty squares that are adjacent to mines; squares that are not adjacent to mines will remain unchanged. After placing the hints, the board will look like this:

.....
.111.
.1*1.
.111.
.....
The “1” indicates that there is a mine adjacent to the empty square.

Here is a more complicated example. Before placing the hints:

.*.*.*.
*****.*
.*.*.*.
*****.*

After placing the hints:

3*5*4*2
*****4*
5*8*6*3
*****3*

Task

Your program will do the following tasks:

  1. Read the layout of the board from the text file.
  2. Print the layout of the board on the screen (before placing the hints).
  3. Place the hints.
  4. Print the layout of the board on the screen (after placing the hints).

Sample Output

The following is the sample output using the above sample file. Remember that we will test your program with other input files. Do not print the lines “Begin Sample Output” and “End Sample Output”.

===== Begin Sample Output =====

Before placing hints:

.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*****.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*****.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
..............................
..............................
..............................
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.

After placing hints:

2*3*3*3*3*3*3*3*3*3*3*3*3*3*3*
*4*4*4*4*4*4*4*4*4*4*4*4*4*4*3
4*6*5*4*4*4*4*4*4*4*4*4*4*4*4*
*****4*4*4*4*4*4*4*4*4*4*4*4*3
5*8*6*4*4*4*4*4*4*4*4*4*4*4*4*
*****4*4*4*4*4*4*4*4*4*4*4*4*3
3*5*4*3*3*3*3*3*3*3*3*3*3*3*3*
112121212121212121212121212121
..............................
112121212121212121212121212121
2*3*3*3*3*3*3*3*3*3*3*3*3*3*3*
*4*4*4*4*4*4*4*4*4*4*4*4*4*4*3
3*4*4*4*4*4*4*4*4*4*4*4*4*4*4*
*4*4*4*4*4*4*4*4*4*4*4*4*4*4*3
3*4*4*4*4*4*4*4*4*4*4*4*4*4*4*
*3*3*3*3*3*3*3*3*3*3*3*3*3*3*2

===== End Sample Output =====

You are reminded to follow the sample output exactly; else marks will be deducted.

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

Do not use any structures or any form of dynamic memory allocation (using malloc or calloc) in your program, else no credit will be given.

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