CSE 143 Autumn 2000

Homework 4

Due:
Part I electronic submission by 10 pm, Wednesday, Nov 1.
Part I paper receipt due in quiz section on Thursday Nov 2.
Part II (entire assignment) electronic submission 10 pm, Wednesday, Nov 15.
Part II paper receipt due in quiz section on Thursday Nov 16.

 

Overview

You will be implementing the game Tetris. If you don't know the game check out www.tetris.com. Tetris is a deceptively simple, completely addictive puzzle game. Small pieces fall from the top of the Grid to the bottom. The pieces are comprised of 4 squares arranged into 7 different patterns. Players must rotate the pieces as they fall and fit them together to complete lines. When the player fills an entire line with blocks, that line is removed from the screen. If the player cannot complete lines, the blocks will eventually fill to the top of the screen and the game ends.

Concepts

The purpose of this assignment is to gain experience with the following new concepts:

Program Synopsis

This program plays the game of Tetris using graphics and keyboard input. Starter code has been provided and you will add code to it to make a complete game. A tetris piece composed of 4 contiguous squares falls from the top of the grid. The piece can be moved left, right, or down as well as rotate clockwise. When the piece lands on the bottom of the grid or another piece it becomes frozen, that is to say, a part of the grid, and then another piece is created. If an entire row fills up with parts of different pieces, then the entire row is removed and the rows above it are moved down. Play continues until the pieces pile up to the top and a new piece is created on top of already frozen pieces.

The starter code provided for you simply creates a single L-shaped piece that can move downwards. You will be enhancing this code in the Two Parts of the assignment.

PART I:

Overview: In PART I, you will add the ability for the L-shaped piece to move LEFT and RIGHT . In addition, you will modify the game so that it will produce another piece once the current one has stopped moving .


Program Details

  1. Read through and become familiar with all of the sample code provided. Determine what the different relations are between different classes and what the different member functions do. UpdatePiece and IsGameOver should be examined, as they are the interesting functions of the Game class.
  2. You do not need to be intimately familiar with GP142 as all of the drawing functionality has been provided for you. But you should read gp142display.h to have a sense of how the graphics work. For more information, read the GP142 or GP143 pages.
  3. The Piece needs to be able to move LEFT and RIGHT if the corresponding keys are pressed. For this assignment LEFT corresponds to pressing the letter 'j' and RIGHT the letter 'l' on the keyboard. In addition, the piece should move DOWN if ' ' (space) is pressed (this will move it faster than it normally falls). In order to handle key clicks, you will need to implement the function handle_key. In addition, you will have to modify some member function of one of the provided classes in order to get the correct behavior. Reading and understanding the code will make it clear which class you need to change.
  4. In order to get another piece to appear on the screen, the following line of code needs to be added:

    new_piece = new LShape(display,grid);

    You need to read through the starter code and find the correct place to put that line.

This part is due Wednesday/Thursday Nov 1st and 2nd.

This finished solution for PART I should work like this: Part I Solution

PART II:

Overview: In PART II you will complete the game. You will need to add the ability for the piece to rotate. Then you will implement the removal of solid rows from the grid. Then, the biggest part, you will need to create the other 6 pieces.

Implementation Requirements

  1. ROTATION: The L-shaped tetris piece needs to be able to rotate in quarter turns clockwise each time the 'k' key is pressed. See the figure below:

  2. To do this you will need to implement the LShape::Rotate() function that is in lshape.h A piece should only rotate if the grid squares that it would occupy are all empty and in addition are all within the boundaries of the grid (i.e. greater than zero and less than GRID_WIDTH). The currentPosition data member can be used to keep track of how rotated the piece is and where to move it next.
  3. In a little more detail: notice in the figure below how the different squares which make up the piece move their positions. The figure shows the surrounding grid so you can see more clearly where the movement happens. In particular, square 2 doesn't move at all, and 0,1,3 move relative to it. If you play the game you will find that this is the case for almost all pieces. You should implement rotation as close as possible to the sample solution. This means that you should identify which squares move relative to which other squares, for the different pieces.

  4. ROW-REMOVAL: You now need to implement Grid::CheckRows. This function should look through the grid and determine if there is a completely filled in row (a row of all non-EMPTY_SQUAREs). If such a row is found, it should be removed and all the rows above it should be moved down. All such rows should be found and removed in one call to CheckRows. You may find it useful to define a couple of private functions to help. See figure. (Note that at this point, you probably only have the L-shaped piece to work with.  The example below shows what can happen when the other pieces have been implemented.)

  5. OTHER PIECES: You now have a complete game for one piece. To finish the assignment you need to create classes for the other six pieces, by deriving them from the Piece class. In particular, you will need to figure out how to initialize the pieces and how to rotate the pieces. This will be similar to how the L-shaped piece was done, and, in fact, you may find it helpful to start each new class by copying the code for a previous shape and modifying it. The same boundary checking should happen when rotating all of the other pieces. Lastly, the pieces should be initalized in the following orientations:

The complete game is due Wed/Thurs Nov 15,16.

The complete solution looks like this: Tetris

Extensions

Some small amount of extra credit will be given for interesting extensions to the basic game. In particular you might consider doing some of the following:

Other Implementation Requirements

Program Hints

Download Files

Download the following files:

Electronic Submission

When you've finished your program, turn in your code using these turning forms:

  1. Part I: turnin 1 -- due Nov 1.
  2. Part II: turnin 2 -- due Nov 15.
Print out the receipt that appears, staple it, and hand it in during quiz section.