tetris game using java

profilerajeev4
CSCI_282_TetrisGame_Part_1_manual.pdf

CHAPTER 2

Tetris game, Part 1

Implementing the game animation of the falling brick, spawning a random series of pieces.

GAME FEATURES REQUIRED FOR PART 1

1. Game board with animated dropping shape

2. Shape must stop dropping at the bottom of the well.

3. New TetrisBrick must appear and start dropping after current brick is at the bottom of the well

4. Order of Bricks appearing must be random.

SECTION 1

Evaluation Criteria for Tetris Game Part 1.

You will be using the classes you created in the “Tetris - Get- ting Started Lab”. No additional classes are allowed. And all classes must be public, which means they must be included in their own source code file.

For Part 1, you will be writing the code inside all of the meth- ods for empty method bodies you created in Lab 4. Remem- ber, you must adhere to the UML diagram, shown on the next page. This illustrates not only which classes that have to be present but also which method must be present, their ac- cess, names and in which class they belong.

And I expect many questions from you. OOD is not an easy concept when you are accustomed to simple procedural pro- gramming. It will require a bit of getting used to. Following this UML will give you a good start on a OOD (object oriented design) for Tetris.

Now, while the UML shows you the methods for each class that must be present, you are free to create helper methods. I recommend that you adhere to the principle of “distinct pur- pose” when creating methods. Each method should have a single purpose. If you cannot easily name the method, chances are that you are trying to do too much within the method.

The following are the development efforts needed per class for Part 1 submission. It is recommended that you carry out the implementation in the order laid out here.

2

TetrisWindow This class requires no additional changes for Part1.

Tetris Brick You will be using this class in Part 1, so a brief explanation of the class and its attributes is needed. TetrisBrick is an ab- stract class and is used as the superclass for all of the specialty Tetris bricks. TetrisBrick contains all of the attributes and methods that all seven brick types bricks have in common. and will be inherited by its seven subclasses. Next is a dis- cussion on how the position of the falling brick is tracked us- ing the 2D array position of integer data type located in the TetrisBrick class. This array records the current location for each the four segments that make up the brick. This location is the row and col that the segments occupies in the well. In the image below, I have added a few feature to the game dis- play that are there as a teaching tool only, to illustrate how the position array attribute in the TetrisBrick class is related to rows and columns of the well as drawn in the display class. These ex- tra features, in- cluding the grid outline, row and col- umn number and table list-

3

Image A

ing the actual coordinates for each segment of the Tetris brick, should not be included in your game, they are a teaching tool only..

The light gray grid outline in the image below represents the rows and columns of the well. In Part 1, you will use these rows and columns to reference where the segments of the falling brick are located. In the image, you can see the row headers to the left of the well and column header at the top of the well. These are the rows and columns of the squares of the grid. The position attribute in TetrisBrick contains the coordinates of each of the four segments of a Tetris- Brick in reference to this grid. The first dimension of the position attribute represent the segment number of the TetrisBrick. This segment number is printed in each segment if the images provided: 0, 1, 2 and 3. The second dimension of the position attribute contain the

row (index 0) and column (in- dex 1) coordi- nates of the grid in which that segment is cur- rently situated in the well. The relation be- tween the posi- tions 2D array in the TetrisBrick

class and the row and column of the well (implied and not ac- tually implemented in this part) is the same as in the 2D array graphics problems we worked on in Module 0 at the begin- ning of this semester.

At the point of initialization, the content of the position array of the LongBrick is shown in image A. This image shows the starting position of the brick just as it is created and it begins its fall. It should be obvious that to move the brick down one row, the row component(found in [0] of the second dimension of the positions array) must be incremented by 1. In Image B, you can see that the column indexes are constant as the brick drops but the row index increases. This should give you a hint on how to make the brick fall.

How we will move the discussion to other attributes of the TetrisBrick class.

numSegments - refers to the number of segments that the brick has. This is 4 for the four segments that make up all Tetris bricks. And since this does not change over the life of the game, you can initialize this attribute where you declare it in the TetrisBrick class. There is no need to pass the value to the constructor as an argument.

colorNumber - is the index into the array of colors present in the TetrisDisplay class. This is what determines what color the brick should be. This can be hard coded in the con- structor of each sub-brick without having to pass it as an ar-

4

Image B

gument. If you plan to include a feature in your app to allow for the change of colors, you will have to change the color in the array, not the sub brick.

The methods needed in the TetrisBrick Class are as follows: moveDown( ) - This method adjusts the row element of each segment to move the whole brick down one row of the board.

moveUp( ) - This method adjusts the row element of each segment to move the whole brick up one row of the board. This is used only when the last move down method is invalid. It is not an allowed move for the player to select and as such will not be associated with a key for the user to trigger.

There are also several abstract methods that TetrisBrick must have that each subclass must overwrite. For part 1 of this project, the needed abstract method is initPositions. This method is abstract in the TetrisBrick class and must be overridden in each subclass to allows each brick to start at the appropriate place for its shape.

Sub Bricks Each of the subclasses listed below must overwrite the ab- stract methods initPosition from the TetrisBrick class: ElBrick, JayBrick, EssBrick, ZeeBrick, SquareBrick, Line Brick and StackBrick.

initPosition() - At the point of initialization, this method should assign the brick its position where it first appears on the board. It should take only one parameter, the center col- umn of the well. In image A of this section, you can see the initial position of the LongBrick. So, all 4 segments each with a row and column index must be set at the top and center of the well. This should adjust when the number of columns changes. Use the example of the long brick to model the initPosition methods for the other subbricks.

TetrisDisplay This class creates the graphic game display by extending a JPanel. In Part 2 it will be responsible for establishing the key listener responsible for the key clicks and for interpreting the key strokes into a move that the game will understand. This class is also responsible for the animation and displaying the updated game status. The only ‘data structure’ (term used loosely) needed to run the game in this class is a TetrisGame object. The TetrisDisplay class has a constructor that takes a TetrisGame object as a parameter. You should not create or use any other arrays or lists within this class aside from the color array which declared as an attribute in the UML dia- gram.

The methods for the TetrisDisplay class are: • cycleMove () - called within the actionPerformed meth-

ods of the anonymous ActionListener defined for the Timer. It will call the games makeMove method passing

5

it the symbol for DOWN (your choice, but the letter D sounds like a good choice).

• paintComponent(Graphics g) - is the method called whenever the repaint( ) method is call, to create the cus- tom graphics of the Tetris game board. You are required to use helper methods to break up the tasks of the drawing the game components. For example: drawWell( ...), drawBrick(... ) or drawBackground( ...) . This is done so that you can cleanly separate out the drawing of the falling brick and the background of the well. Helper methods have the access modifier of private.

TetrisGame - This class contains all the logic for running the game, such as ...

• responding to the moves given from the Tetris Display class,

• checking that a move is valid and undoing it if it is not valid.

• spawning a new brick when the current brick has reached as far as it will go.

• updating the game board when required • removing full rows and adjusting the score • end-game detection.

In Part 1 of the implementation, you will be concerned with only the first three points. The methods of the TetrisGame class that you are responsible to implement in this part of the development are as follows:

• constructor - takes the number of rows and column in the well as arguments. Should instantiate the random number generator.

• spawnBrick - Should randomly generate a new “falling brick” that starts at the top of the well.

• makeMove - Should take a move code as argument. The move code should indicate which direction the falling brick should move; down, left, right, up, or rotate. For part 1, the only move is “down”. This methods should call the vali- dateMove methods to make sure each move is allowed. The move should be reversed if validateMove results in false. In part 1, it would reverse the last move (when brick hits the bottom and tries to move into the border of the well).

• validateMove - checks the tentative move to make sure the brick is in bounds. For part one, it simply has to check that the brick is not below the bottom of the well.

• getNumSegs - returns the number of segments in the fall- ing brick.

• getFallIngBrickColor -returns the color number of the falling brick.

• getSegRow - takes, as argument, the segment number (0, 1, 2 or 3) and returns the row coordinate of the indicated segment (from the positions array of the brick).

• getSegCol- takes, as argument, the segment number (0, 1, 2 or 3) and returns the column coordinate of the indicated segment (from the positions array of the brick).

6

Evaluation Criteria:

You are expected to follow good programming form. You will loose substantial points for not doing so.

Pay close attention to the “No numeric literals” conven- tion. This states that no numeric literals are allowed in code except to assign a value to a variable. The only exception to this are the values of 0, 1 or 2. This is especially important when forming your graphics and includes indices or arrays.

To avoid using number for array indices, initialize your arrays with lists, not one element at a time.

You should assign needed values to variables when to start to code. There is never enough time at the end of a project to correct sloppy practices you indulged in at the start of the project. You will loose substantial points for not following the numeric literal convention. Here are several other require- ments.

1. You must have all of the classes with the methods shown in the UML. However, you may have additional helper meth- ods (private methods, in fact it is recommended)

2. Game and Display objects are instantiated in Window class.

3. The Display class controls animation and reports moves to be made to the game class.

4. Game class controls the logic of the game. It receives move from the display class, validates it and determines when the brick and reached its stopping location.

Use the rubric provided at the submission link, as a check off list BEFORE submission. You should know what your score will be before you submit your work. Remember, if one part of the criteria is not correct, all points for that criteria are lost. There is no partial credit. This is to estab- lish the importance of completing a task and attention to de- tail. If you have any questions on a criteria, ASK!

Enjoy learning and don’t wait too long to come for assistance!!

7

  • Tetris game, Part 1
    • Evaluation Criteria for
    • Tetris Game Part 1.