CS1101C Practical Exam

Session 4 (1600 - 1745 hours)

Tetris T Blocks

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

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

Background

Tetris is a game where players try to drop blocks in order to form complete lines. To keep things simple, we will merely be dropping T shaped blocks, and we do not remove any blocks that form a complete line.

Playing Area

The game occurs in an 8 x 8 playing area. T blocks are dropped from the top of the playing area and they will fall down due to gravity until they land on the floor or on another block. T blocks can be stacked up in this way to form interesting patterns.

Rotations

Each T block can be rotated in one of four possible rotations. We call them rotation 1, rotation 2, rotation 3, and rotation 4. The four rotations are shown below, where each T block is indicated by four letter X’s. An empty square is shown by a full-stop.

....
.X..  Rotation 1
XXX.

.X..
.XX.  Rotation 2
.X..

..X.
.XX.  Rotation 3
..X.

....
.XXX  Rotation 4
..X.

Position

Each T block can also be dropped at a particular position according to the column number. The leftmost column is column 0, and the rightmost column is column 7. Placing a T block at column n means that the leftmost block of the T block is at column n.

Here are some examples showing the rotation number and the position (column number) of a T block. Each T block is placed in an empty playing area.

........
........
........
........
........  Rotation 1, Position 0
........
.X......
XXX.....

........
........
........
........
........  Rotation 1, Position 1
........
..X.....
.XXX....

........
........
........
........
........  Rotation 2, Position 0
X.......
XX......
X.......

........
........
........
........
........  Rotation 3, Position 2
...X....
..XX....
...X....

........
........
........
........
........  Rotation 4, Position 5
........
.....XXX
......X.

Stacking

When part of one block lands on another block, it remains in that position. There is never any danger of blocks toppling over. Here is a simple example of two blocks stacked on one another.
........
........
........
........
........  Rotation 4, Position 0
........
XXX.....
.X......

........
........
........
........
...X....  Rotation 1, Position 2
..XXX...
XXX.....
.X......

Objective

Initially, the playing area is a empty. The objective of your program is to read in a text file. Each line contains the details of where to drop a T block and its rotation number. Keep stacking the T blocks as they are dropped, and show the layout of the playing area after each T block is dropped.

Text File

Your program must read in a text file called tetris1.txt. Each line contains two integers: the first integer show the position of the T block, and the second integer shows the rotation number of the T block. The following is a sample text file.

0 1
5 1
0 2
6 3
1 4
4 4

The above shows that the first T block has rotation 1 and is dropped at position 0, the second T block has rotation 1 and is dropped at position 5, and so on. No error checking is required; you may assume that the rotation and position numbers always keep the entire T block within the 8 x 8 playing area. You may also assume that we will not put too many T blocks such that they overflow from the top of the playing area.

We will test your program with other text files.

Sample Run

The following shows a sample run when using the file tetris1.txt as shown above. For each block that is dropped, show its position number, rotation number, and the layout of the playing area.

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 tetris.c -o tetris
$ ./tetris
T block dropped at position 0, rotation 1:
........
........
........
........
........
........
.X......
XXX.....

T block dropped at position 5, rotation 1:
........
........
........
........
........
........
.X....X.
XXX..XXX

T block dropped at position 0, rotation 2:
........
........
........
........
X.......
XX......
XX....X.
XXX..XXX

T block dropped at position 6, rotation 3:
........
........
........
........
X......X
XX....XX
XX....XX
XXX..XXX

T block dropped at position 1, rotation 4:
........
........
........
........
XXXX...X
XXX...XX
XX....XX
XXX..XXX

T block dropped at position 4, rotation 4:
........
........
........
........
XXXXXXXX
XXX..XXX
XX....XX
XXX..XXX

$

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