CS1101C Practical Exam

Session 4 (1000 - 1145 hours)

Minefield

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

The deadline for this lab is Wednesday 23 April 2008, 11:45:59 hours. Strictly no submissions will be accepted after the deadline.

Introduction

As the general of your army, your task is to infiltrate a very dangerous minefield with a 9-men squad. But can your men get out alive?

The minefield is a 10x10 field. Each square on the field can either be empty (safe), or it could contain a mine; if a soldier steps or lands on a mine, the explosion will remove both the mine as well as the soldier that steps on it. Soldiers in adjacent squares are not affected.

Text File

A sample text file called minefield.txt has been copied into your directory by the pesetup command. Here is the sample text file:
1 1 1 1 0 0 0 1 1 1
1 1 1 1 0 0 0 1 1 1
1 1 1 1 0 0 0 1 1 1
1 1 1 0 0 0 0 1 1 1
1 1 1 0 0 0 0 1 1 1
1 1 1 0 0 1 0 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 0 0 0 0 1 1 1
1 1 1 0 0 0 0 1 1 1
1 1 1 0 0 0 0 1 1 1
4 5
0
1
2
3
4
5
6
6
5
6
7
0

The first ten lines of the file show the layout of the 10x10 minefield; a 0 indicates an empty (safe) square, and a 1 indicates a square that contains a mine.

The next line of the file contain two digits, in this case, 4 5. This is the coordinates (4,5) of the squad of 9-men to be placed on the minefield. The squad of 9-men are always in a 3x3 formation, and the coordinates indicate the soldier in the center of the 3x3 formation.

The top-left (North-West) corner of the minefield has coordinates (0,0), and the top-right (North-East) corner of the minefield has coordinates (0,9).

The remaining lines in the file (of which there are an unknown number) indicate the movement of the squad. Each movement is always a distance of one square. The movement can be North, South, East, West, or diagonally; North-West, North-East, South-West, or South-East. The movement is indicated by an integer code and is shown in the following table:

Code
Movement
0
North-West
1
North
2
North-East
3
East
4
South-East
5
South
6
South-West
7
West

The squad always moves as one unit with each (remaining) man moving in the same direction for the distance of one square. For simplicity, you may assume that the coordinates of the center soldier in the squad will always be between (1,1) and (8,8).

Sample Output

Sample runs of the program are shown below. The sample runs are self-explanatory. Follow the sample output precisely. Comments are indicated in bold and preceded by //, and you should not print out the comments.

We will test your program with other text files.

Note that in the output, we use a full-stop to indicate an empty (safe) square, a M to indicate a square with a mine, and a S to indicate a soldier.

$ gcc -Wall minefield.c -o minefield

$ ./minefield minefield.txt

Original minefield:
M M M M . . . M M M
M M M M . . . M M M
M M M M . . . M M M
M M M . . . . M M M
M M M . . . . M M M
M M M . . M . M M M
M M M M M M M M M M
M M M . . . . M M M
M M M . . . . M M M
M M M . . . . M M M

Placing 9-men squad at (4, 5):
M M M M . . . M M M
M M M M . . . M M M
M M M M . . . M M M
M M M . S S S M M M
M M M . S S S M M M  // The soldier in the center of the squad
M M M . S . S M M M  // is placed at (4,5). Note that the soldier
M M M M M M M M M M  // at (5,5) dies as he lands on a mine.
M M M . . . . M M M  // Both the soldier and the mine are removed.
M M M . . . . M M M  // Print out the number of men remaining.
M M M . . . . M M M
Number of men remaining: 8

Moving squad to (3, 4):
M M M M . . . M M M
M M M M . . . M M M
M M M . S S . M M M
M M M S S S . M M M  // Squad moves North-West, one soldier dies.
M M M S . S . M M M
M M M . . . . M M M
M M M M M M M M M M
M M M . . . . M M M
M M M . . . . M M M
M M M . . . . M M M
Number of men remaining: 7

Moving squad to (2, 4):
M M M M . . . M M M
M M M M S S . M M M
M M M S S S . M M M  // Squad moves North, no one dies.
M M M S . S . M M M
M M M . . . . M M M
M M M . . . . M M M
M M M M M M M M M M
M M M . . . . M M M
M M M . . . . M M M
M M M . . . . M M M
Number of men remaining: 7

Moving squad to (1, 5):
M M M M . S S M M M
M M M M S S S M M M  // Squad moves North-East, no one dies.
M M M . S . S M M M
M M M . . . . M M M
M M M . . . . M M M
M M M . . . . M M M
M M M M M M M M M M
M M M . . . . M M M
M M M . . . . M M M
M M M . . . . M M M
Number of men remaining: 7

Moving squad to (1, 6):
M M M M . . S . M M
M M M M . S S . M M  // Squad moves East, three soldiers die.
M M M . . S . . M M
M M M . . . . M M M
M M M . . . . M M M
M M M . . . . M M M
M M M M M M M M M M
M M M . . . . M M M
M M M . . . . M M M
M M M . . . . M M M
Number of men remaining: 4

Moving squad to (2, 7):
M M M M . . . . M M
M M M M . . . S M M
M M M . . . S S M M  // Squad moves South-East, no one dies.
M M M . . . S M M M
M M M . . . . M M M
M M M . . . . M M M
M M M M M M M M M M
M M M . . . . M M M
M M M . . . . M M M
M M M . . . . M M M
Number of men remaining: 4

Moving squad to (3, 7):
M M M M . . . . M M
M M M M . . . . M M
M M M . . . . S M M
M M M . . . S . M M  // Squad moves South, one soldier dies.
M M M . . . S M M M
M M M . . . . M M M
M M M M M M M M M M
M M M . . . . M M M
M M M . . . . M M M
M M M . . . . M M M
Number of men remaining: 3

Moving squad to (4, 6):
M M M M . . . . M M
M M M M . . . . M M
M M M . . . . . M M
M M M . . . S . M M
M M M . . S . M M M  // Squad moves South-West, no one dies.
M M M . . S . M M M
M M M M M M M M M M
M M M . . . . M M M
M M M . . . . M M M
M M M . . . . M M M
Number of men remaining: 3

Moving squad to (5, 5):
M M M M . . . . M M
M M M M . . . . M M
M M M . . . . . M M
M M M . . . . . M M
M M M . . S . M M M
M M M . S . . M M M  // Squad moves South-West, one soldier dies.
M M M M . M M M M M
M M M . . . . M M M
M M M . . . . M M M
M M M . . . . M M M
Number of men remaining: 2

Moving squad to (6, 5):
M M M M . . . . M M
M M M M . . . . M M
M M M . . . . . M M
M M M . . . . . M M
M M M . . . . M M M
M M M . . S . M M M
M M M M S M M M M M  // Squad moves South, no one dies.
M M M . . . . M M M
M M M . . . . M M M
M M M . . . . M M M
Number of men remaining: 2

Moving squad to (7, 4):
M M M M . . . . M M
M M M M . . . . M M
M M M . . . . . M M
M M M . . . . . M M
M M M . . . . M M M
M M M . . . . M M M
M M M M S M M M M M
M M M S . . . M M M  // Squad moves South-West, no one dies.
M M M . . . . M M M
M M M . . . . M M M
Number of men remaining: 2

Moving squad to (7, 3):
M M M M . . . . M M
M M M M . . . . M M
M M M . . . . . M M
M M M . . . . . M M
M M M . . . . M M M
M M M . . . . M M M
M M M . . M M M M M
M M . . . . . M M M  // Squad moves West, two soldiers die.
M M M . . . . M M M
M M M . . . . M M M
Number of men remaining: 0

Moving squad to (6, 2):
M M M M . . . . M M
M M M M . . . . M M
M M M . . . . . M M
M M M . . . . . M M
M M M . . . . M M M
M M M . . . . M M M  // Squad moves North-West, no one dies.
M M M . . M M M M M  // Note that even though no soldiers remain,
M M . . . . . M M M  // we still continue. We stop only when the
M M M . . . . M M M  // end of the file has been reached.
M M M . . . . M M M
Number of men remaining: 0

$

You may assume that the text file always exists, and is always supplied as an argument on the command line. The name of the text file is obtained from this command line argument.

Important Notes

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 minefield.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.