CS1101C Practical Exam

Session 3 (1215 - 1400 hours)

Hunt!

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

The deadline for this lab is Wednesday 15 November 2006, 14:00:59 hours. Strictly no submissions will be accepted after the deadline.

Background

In the traditional game of Hunt!, players walk around in a maze of trees trying to kill each other. In this simple simulation, we will simulate one player and his powerful gun.

Field

The game takes place in a 20 x 20 field. A sample field is shown below:

W W W W W W W W W W W W W W W W W W W W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . v . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . t . t t . . . . . . . W
W . . . . . . . t . W t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W W W W W W W W W W W W W W W W W W W W
The meaning of the symbols are:

Walls and the player are always indestructible (cannot be killed or destroyed). Trees can be destroyed by any explosions.

Explosions

A player always fires in the direction he is facing. The bullet explodes when it hits a tree or a wall. The size of the explosion is determined by the power of the bullet used. The size of the explosion is always a positive odd integer in the range of 1, 3, 5, ..., 99.

In the above scenario, suppose the player fires a bullet of explosion size three. The point of impact is the first tree or wall that it hits. The point of impact is the center of the explosion. In this case, the impact is a square of size three. All trees in the explosion square are destroyed. Thus, the following trees (marked with an asterisk) are destroyed by the explosion. Note that the wall is not affected even though it is in the area of the explosion.

W W W W W W W W W W W W W W W W W W W W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . v . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . t . t t . . . . . . . W
W . . . . . . . * . W t . . . . . . . W
W . . . . . . . * * * t . . . . . . . W
W . . . . . . . * * * t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W W W W W W W W W W W W W W W W W W W W

The trees that are destroyed are removed, leaving:

W W W W W W W W W W W W W W W W W W W W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . v . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . t . t t . . . . . . . W
W . . . . . . . . . W t . . . . . . . W
W . . . . . . . . . . t . . . . . . . W
W . . . . . . . . . . t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W W W W W W W W W W W W W W W W W W W W

Remember that walls and the player are immune to destruction. If a bullet impacts a wall, the wall is not affected, but any trees in the area of the explosion will be destroyed.

Data File

Here is a sample data file called hunt1.txt which has been copied into your directory by the pesetup command.
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 1 0 2 1 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3

The field is a 20 x 20 field. The first twenty lines consist of the field layout, with the integers:

Code
Meaning
0
Empty Square
1
Tree
2
Wall
3
Player Facing Up
4
Player Facing Right
5
Player Facing Down
6
Player Facing Left

This is followed by a single number (which we call n) that represents the explosion size of the bullet fired. There is always only one player, and the player always fires just one bullet with the explosion size n. In the above text file, you can see that n is three.

You may assume that the field is always bounded by walls as shown above.

Task

Your task is to read the field layout from the data file called hunt1.txt (you may assume that the file always exists), read the value of n, and display the initial field layout as well as the final field layout after all the trees have been destroyed by the explosion. Display also the number of trees destroyed. You may assume that all numbers in the data file are valid, and n is a positive odd integer from 1 to 99 inclusive. No error checking is required.

Sample Output

The following is the sample output using the above file. Remember that we will test your program with other input files.
Initial layout:
W W W W W W W W W W W W W W W W W W W W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . v . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . t . t t . . . . . . . W
W . . . . . . . t . W t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W W W W W W W W W W W W W W W W W W W W

Final layout:
W W W W W W W W W W W W W W W W W W W W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . v . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W . . . . . . . t . t t . . . . . . . W
W . . . . . . . . . W t . . . . . . . W
W . . . . . . . . . . t . . . . . . . W
W . . . . . . . . . . t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . t t t t . . . . . . . W
W . . . . . . . . . . . . . . . . . . W
W W W W W W W W W W W W W W W W W W W W

Number of trees destroyed: 7

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

Do not use any structures in your program, else no credit will be given.

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