CS1101C Practical Exam

Session 5 (1100 - 1245 hours)

Blocks

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

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

Background

Blasto is an old game where mines that are placed together explode in a chain reaction. Given a 10 x 10 field, each square may either be empty or it may have exactly one mine on it.

Coordinates

We assume that each square is given by the coordinates (r,c) where r is the row number and c is the column number. The top left hand corner has coordinates (0,0) and the top right hand corner has coordinates (0,9).

Example Layout

The following shows an example initial layout. Mines are marked by the character o (a small letter o). Empty squares are marked by a full-stop.

....o.....
...o.o....
..o...o...
.o.....o..
oo......o.
oo..ooo..o
oo..ooo...
oo..ooo...
ooo.......
oooo......

Detonation

Any square in the field may be ignited by a missile landing on that square. If the square is empty, nothing happens.

If the square contains a mine, that mine will explode first. It will then cause all other mines around it to explode. All mines that are adjacent to it (one square to the left, right, up, down, and all the four diagonal directions) will explode, causing a chain reaction. In addition, those other mines that are adjacent to the exploded mines will explode until there are no more explosions.

Detonation Example

....o.....
...o.o....
..o...o...
.o.....o..
oo......o.
oo..ooo..o
oo..ooo...
oo..ooo...
ooo.......
oooo......

Suppose we start with the layout shown above.

Supposing a detonation occurs at (7,6). In the following diagrams, we indicate a mine that has just been exploded by a letter x.

Missile at (7,6):
Iteration 1:
Number exploded: 1
....o.....
...o.o....
..o...o...
.o.....o..
oo......o.
oo..ooo..o  The mine at (7,6) has just been exploded by a missile.
oo..ooo...  This is iteration 1, and 1 mine has been exploded.
oo..oox...
ooo.......
oooo......

Iteration 2:
Number exploded: 3
....o.....
...o.o....
..o...o...
.o.....o..
oo......o.
oo..ooo..o  All the neighbours of (7,6) are exploded too.
oo..oxx...  Note that the mine (7,6), once exploded, becomes an empty square.
oo..ox....  This is iteration 2, and 3 mines have been exploded.
ooo.......
oooo......

Iteration 3:
Number exploded: 5
....o.....
...o.o....
..o...o...
.o.....o..
oo......o.
oo..xxx..o  The chain reaction continues.
oo..x.....  This is iteration 3, and 5 mines have been exploded.
oo..x.....
ooo.......
oooo......

Iteration 4:
Number exploded: 0
....o.....
...o.o....
..o...o...
.o.....o..
oo......o.
oo.......o  The chain reaction stops because no more mines
oo........  are adjacent to the ones that have been exploded.
oo........  This is iteration 4, and 0 mines have been exploded.
ooo.......
oooo......

Objective

The objective of your program is to read in a text file containing the initial layout of the mines. You will then continue to read the same text file for the list of coordinates at which explosions caused by a missile occur. Show the layout after each explosion and chain reaction.

Text File

To simply matters, the layout is stored in a text file called blasto1.txt containing only integers. This text file is found in your sunfire directory. The following is a sample text file.

0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 1 0 0 0 0
0 0 1 0 0 0 1 0 0 0
0 1 0 0 0 0 0 1 0 0
1 1 0 0 0 0 0 0 1 0
1 1 0 0 1 1 1 0 0 1
1 1 0 0 1 1 1 0 0 0
1 1 0 0 1 1 1 0 0 0
1 1 1 0 0 0 0 0 0 0
1 1 1 1 0 0 0 0 0 0
7 6
5 4
0 5
0 4
9 3

The first ten lines show the initial layout. We use 0 to indicate an empty square and 1 to indicate a mine. The locations of the mines vary from file to file.

The remaining lines in the file (the number of which is unknown) is a list of coordinates that a missile will land on. So the first missile lands at (7,6), the second missile lands at (5,4), and so on.

We will test your program with other text files.

Sample Run

The following shows a sample run when using the file blasto1.txt as shown above. Print out the resulting board in the format shown below, using a full-stop for an empty square, a letter o to indicate an unexploded mine, and a letter x to indicate a mine that has just been exploded. Display the location of the missile when it lands. For each iteration, display the iteration number and the number of mines exploded during each iteration.

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 blasto.c -o blasto
$ ./blasto
Missile at (7,6):
Iteration 1:
Number exploded: 1
....o.....
...o.o....
..o...o...
.o.....o..
oo......o.
oo..ooo..o
oo..ooo...
oo..oox...
ooo.......
oooo......

Iteration 2:
Number exploded: 3
....o.....
...o.o....
..o...o...
.o.....o..
oo......o.
oo..ooo..o
oo..oxx...
oo..ox....
ooo.......
oooo......

Iteration 3:
Number exploded: 5
....o.....
...o.o....
..o...o...
.o.....o..
oo......o.
oo..xxx..o
oo..x.....
oo..x.....
ooo.......
oooo......

Iteration 4:
Number exploded: 0
....o.....
...o.o....
..o...o...
.o.....o..
oo......o.
oo.......o
oo........
oo........
ooo.......
oooo......

Missile at (5,4):
Iteration 1:
Number exploded: 0
....o.....
...o.o....
..o...o...
.o.....o..
oo......o.
oo.......o
oo........
oo........
ooo.......
oooo......

Missile at (0,5):
Iteration 1:
Number exploded: 0
....o.....
...o.o....
..o...o...
.o.....o..
oo......o.
oo.......o
oo........
oo........
ooo.......
oooo......

Missile at (0,4):
Iteration 1:
Number exploded: 1
....x.....
...o.o....
..o...o...
.o.....o..
oo......o.
oo.......o
oo........
oo........
ooo.......
oooo......

Iteration 2:
Number exploded: 2
..........
...x.x....
..o...o...
.o.....o..
oo......o.
oo.......o
oo........
oo........
ooo.......
oooo......

Iteration 3:
Number exploded: 2
..........
..........
..x...x...
.o.....o..
oo......o.
oo.......o
oo........
oo........
ooo.......
oooo......

Iteration 4:
Number exploded: 2
..........
..........
..........
.x.....x..
oo......o.
oo.......o
oo........
oo........
ooo.......
oooo......

Iteration 5:
Number exploded: 3
..........
..........
..........
..........
xx......x.
oo.......o
oo........
oo........
ooo.......
oooo......

Iteration 6:
Number exploded: 3
..........
..........
..........
..........
..........
xx.......x
oo........
oo........
ooo.......
oooo......

Iteration 7:
Number exploded: 2
..........
..........
..........
..........
..........
..........
xx........
oo........
ooo.......
oooo......

Iteration 8:
Number exploded: 2
..........
..........
..........
..........
..........
..........
..........
xx........
ooo.......
oooo......

Iteration 9:
Number exploded: 3
..........
..........
..........
..........
..........
..........
..........
..........
xxx.......
oooo......

Iteration 10:
Number exploded: 4
..........
..........
..........
..........
..........
..........
..........
..........
..........
xxxx......

Iteration 11:
Number exploded: 0
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........

Missile at (9,3):
Iteration 1:
Number exploded: 0
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........

$

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