CS1101C Practical Exam

Session 2 (1000 - 1145 hours)

Blocks

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

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

Background

A man is moving blocks in an 8 x 8 board. The blocks are very heavy, so the man can only push one block at a time. The man may move in any of the four directions: up, down, left, and right. The man always moves only one square at a time.

Example

The following shows an example initial layout. The man is marked by the character @. The blocks are marked by the character O (capital o).

......O.
......O.
.....@.O
......O.
........
......O.
........
........

Movement

The following shows the movement codes and directions.

CodeDirection
1
Left
2
Up
3
Right
4
Down

Moves

A successful move is defined as a man moving to an empty square.

An unsuccessful move is one in which the man tries to move out of the 8 x 8 board. All unsuccessful moves must be caught and disallowed by your program.

Suppose the first movement code is 3, which means move to the right. The man will move to the right, resulting in the following layout. This is a successful move.

......O.
......O.
......@O
......O.
........
......O.
........
........

Pushes

A push is defined as a man moving against a block. A push is successful if the square beyond the block is empty.

A push is unsuccessful if the square beyond the block is occupied by another block, or if the square beyond the block is outside of the boundary of the 8 x 8 board. All unsuccessful pushes must be caught and disallowed by your program.

Suppose the next movement code is 4, which means to move down. The man will push the block downwards, resulting in the following layout.

......O.
......O.
.......O
......@.
......O.
......O.
........
........

Objective

The objective of your program is to read in a text file containing the initial layout of the man and the blocks. You will then continue to read the same text file for the list of moves for the man to execute, showing the final layout, as well as keeping statistics regarding moves and pushes.

Text File

To simply matters, the layout is stored in a text file called blocks1.txt containing only integers. The following is a sample text file.

0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0
0 0 0 0 0 2 0 1
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
3
4
2
3
2
1
2
2
2
3
3

The first eight lines show the initial layout. We use 0 to indicate an empty square, 1 to indicate a block, and 2 to indicate a man. The locations of the blocks and the man vary from file to file.

The remaining lines in the file (the number of which is unknown) is a list of moves that the man will execute, shown by the movement code. So the first move is 3 which means move to the right, the second code is 4 which means move down, and so on.

We will test your program with other text files.

Sample Run

The following shows a sample run when using the file blocks1.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 a block, and the character @ to indicate the man. Show only the final layout of the board after all the man's moves are executed. Then display the statistics as shown.

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 blocks.c -o blocks
$ ./blocks
Final layout:
......@O
......O.
.......O
........
......O.
......O.
........
........
Total commands: 11
Valid moves: 5
Valid pushes: 2
Invalid moves: 1
Invalid pushes: 3
$

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