tetris game using java
tetris game
20 days ago
80
CSCI_282_TetrisGame_Part_3_manual.pdf
CSCI_282_TetrisGame_Part_2_manual-2.pdf
- CSCI_282_TetrisGame_Part_1_manual.pdf
- CSCI_282_TetrisGame_Lab_4_manual.pdf
CSCI_282_TetrisGame_Part_3_manual.pdf
CHAPTER 4
Tetris Game Part 3
Completing the logic of the Tetris game for removing full rows and scoring
Establishing a leader board
Save and Retrieving games from file
FULL GAME FUNCTIONALITY
1. Filled rows scored and disappeared
2. Score tracked on TetrisDisplay.
3. All bricks above scored line drop down to fill blank line
4. Game over detection
5. High score must be tracked, displayed in sorted order and saved to file.
6. Menu system in place with two pull down menus with functioning menuItem
7. Save game to file with a user created name
8. Retrieve game from selected file name.
SECTION 1
Evaluation Criteria for Tetris Game Part 3.
In part 3, you will be evaluated on new aspects of the game. The points you earned points for on Parts 1 and 2 will not be
earned in part 3 even if you did not submit part 2. However, any er- rors not corrected from previous parts will earn you penal- ty points.
In Part 3 you will be finishing up your game such that the game can be saved to and read from a file, full lines will be detected, re- moved and scored. Also a High score leader board must be established. There must also be a functioning menu system.
2
Itemized Part 3 requirements of user interaction
1. The user should be about to save a current game to file to finish later by retrieving it from file. As such the methods saveToFile and retrieveFromFile should be added to the game class.
2. When saving a game to a file name, the user must be able to enter a file name.
3. You must be able to retrieve a game back from file using a GUI Interface to select file (JFileChooser)
4. There must be a menu bar in the window class with two pull down menus.
• One Menu items must have an option to display leader board, and one to delete all high scores to start over.
• The other Menu should deal with the new game, and saving and retrieving games from file.
Itemized Part 3 requirements for game play
1. Full line detection followed by full line disappearing and score increasing must be complete and correct.
2. All rows above disappeared lines should drop one row.
3. Score must be incremented by 100 if just one line disap- pears, 300 for two lines, 600 for three lines and 1200 for four lines.
4. Score must be shown on the Game display.
Itemized Part 3 requirements for Leader Board
1. You must design you own LeaderBoard class that is GUI
2. This leader board must save the highest 10 score display in descending order. Scores must be accompanied by a name
3. Score must be persistent.
4. Functionality must include adding a new high score in proper order and clearing all high scores.
5. This class is called from the Window class menu option.
6. The UML class diagram for your class must be included in your worksheet
The grading criteria are available at the submission site on the Moodle class page. Please read all criteria over carefully. If you have any questions over these criteria, make sure you ask. Not understanding a criteria does not prevent you from loos- ing points if you did it incorrectly.
3
QUESTIONS TO CONSIDER
1. How many rows have to be checked?
2. What happens if more than one full row is detected?
3. What happens if the full rows are not consecutive?
SECTION 2
Detecting full rows Detecting Full rows. I will leave how to score a full row to you. So there are no required methods. I recommend that you use helper functions that looks for full rows. But you have to realize you can have one, two, three or four full rows with one brick placement. The game boards below show various situation for row scoring. The full lines should be detected and scored BEFORE the next brick is spawned.
The images above depict one full row scored.
The images above depict two full rows
4
scored.
The images above depict three full rows scored.
The images above depict four full rows scored.
But you must realize that full rows do not need to be consecu- tive. Full rows can have a row with spaces in between them.
Please note that the only rows that need to be checked are those that involve the position where the latest brick was posi- tioned in the board. Possibly a helper function in the Tetris
brick called getMinimumRow and getMaximumRow would be useful.
I recommend that you use helper methods to accomplish the sub tasks needed to accomplish the detection, removal and scoring of a full row. This improves readability of your code and if you don’t you could have up to 4 to 5 nested loop struc- tures in one method, which can be difficult to debug.
Some suggested methods
• rowHasSpace( int rowNumber) - returns true if there is a space in the row of the board. This is used to determine if the row is full and needs scoring.
• rowHasColor( int rowNumber) - return true if there is color in row of the board. This would be used to detect the end point of shifting rows down.
• copyRow(int rowNumber) - copy row content to the row below. Used to shift a row down when a full row is scored. The method affectively over writes the content of the row below, making the deletion of this row unnecessary, unless of course you want to go for extra credit and make some special effect that shows the bricks imploding or something equally dramatic. :0)
• copyAllRows(int rowNumber) - calls the method copyRow on each row starting with rowNumber and above (located higher in the well, or at a lower row number).
A suggest approach uses the following pseudo code:
5
1 .Determine the range of rows to be checked. 2. currentRow - highest row number in range 2. REPEAT for each row within the range starting with max 3. IF rowHasSpace( int rowNumber) is false 4. call copyAllRows(currentRow) 5. current row is incremented (so that when for loop decrements current, it remains the same 6. min row of range is decremented 7. END IF 8.END REPEAT
But this is just a suggestion. There are other equally valid ap- proaches.
hint: once you have copied the row above the scored row to the position of the row that was scored, you have to recheck that row for fullness, unless that row was not in the range of rows affected by the last play.
Now, this suggested approach is how to get the rows to disap- pear. You also have to work out how to score this process. I would recommend that you count the number of row removed and then figure out the score after going through all rows that need checking.
Once again, I will give you the freedom to use what ever helper methods you want, but you should use helper methods. That is just good programming form. Warning: You will be
penalized if there is more than three nested for loops in any of your methods. I would like to see two or less. I want you to practice good method design by making functions short and concise. I have given you a couple of suggestions.
6
You are required to
7
SECTION 3
Interfaces
1. UML Representation of Interfaces
2. Functionality of Savable Game.
3. Change in TetrisWindow class
- Tetris Game Part 3
- Evaluation Criteria for Tetris Game Part 3.
- Detecting full rows
- Interfaces
CSCI_282_TetrisGame_Part_2_manual-2.pdf
CHAPTER 3
Tetris Game Part 2
Functionality to be added: • Transfer of color to background • Color validation • Move right and left • Side boundary validation • Rotate • The spacebar must be programmed to
pause and restart the animation as a
PART 2 CRITERIA
1. Color Transferred to board when brick stops falling.
2. Pause key on typing space bar
3. Key listener installed in TetrisDisplay class
4. Functionality of left and right move of shape
5. Validity checking on left and right move.
6. Functionality of rotation and unrotation (also an abstract method)
7. Validity checking on rotation move.
8. New Game when ‘N’ is pressed
9. Pause game on ‘space bar’
SECTION 1
Evaluation Criteria In part 2, you will be evaluated on new aspects of the game. The points you earned points in Parts 1 will not earn points this time. However, any errors not corrected from previous parts will earn you penalty points. The UML is shown below, with all new methods shown in red.
2
For this phase of the development of the Tetris game we will be accomplishing the following:
•Transfer the brick color to background •Move Tetris brick right and left •Rotate Tetris brick •Validation of move extended to side boundaries and color •New game capability
Here are the new methods. They have arguments and return values only where indicated. 1. TetrisWindow - no new methods. 2. TetrisDisplay -
• translateKey () - called within the keyTyped method of the anonymous KeyListener. This method will call the make move method from the game with the appropriate move code and takes the key event as argument.
• drawBackground() - Will draw the content of the Background attribute of the game object taking Graphics object as argument.
3. TetrisGame - • initboard - will set the background attribute to default
content. • newGame - will start a new game. • fetchBoardPosition - will return the integer content
of the game attribute, background, based on the row and column passed to the method.
• transferColor - when a brick has no place to fall, it’s color is transferred to the board array in game.
4. TetrisBrick (abstract class) - New methods: • rotate- abstract method in Tetris brick, and must be
overwritten in each individual brick subclass. • unrotate- abstract method, used if validation of rota-
tion move fails. It is an abstract class in Tetris brick, and must be overwritten in each individual brick subclass to undo the rotate move
• moveLeft- translates each segment of the brick one cell to the left.
• moveRight - translates each segment of the brick one cell to the right.
Overall Functionality to complete in Part 2:
It is recommended that you proceed to build up you game one functionality at a time, debugging as you go. Here is the rec- ommended order.
1. The colors from the current falling brick must be trans- ferred to the background when the brick has stopped falling and no brick may fall over a colored brick in the back ground (color validation).
2. You must install the key listeners in the Display class.
3. Make the space bar the Pause key that toggles pausing the animation of the game and restarting it.
4. The functionality of the left and right arrow keys must in- corporate move validation, meaning it cannot over run the
3
side boundaries of the game board or move over a colored segment.
5. The rotation functionality in the game class must be com- plete (calling the rotation method of the falling brick) along with validation of the move , meaning it cannot over run any boundaries of the game board or move over a colored segment.
6. A new game must be started when ‘N’ is typed.
Use the Criteria table posted on the Moodle submission site as a check off list for the completion of your project BEFORE submission. You should know what your score will be before you submit your work. Remember, if one part of the cri- teria is not correct, all points for that criteria are lost. There is no partial credit. This is to establish the impor- tance of completing a task and attention to detail.
Also, use the penalty table provide at the submission point, to check for possible loss of points for poor programming form or improper submission practice. Again, full points deducted!
If you have any questions on a criteria, ASK!
Enjoy learning and don’t wait too long to come for assistance!!
4
INCORPORATING COLOR AND VALIDATION COLOR
Color of the current brick is entered into the bricks last valid position on the board. (transferColor)
A move cannot allow the current brick to cover any part of the background that has a color number greater than Zero. (validateMove)
SECTION 2
Transfer of color into back ground
The first functionality to be added is incorporating the color of the current brick into the background. This is fairly straight forward. Remember the following
The well is divided into cells that are initialized to zero or -1 or any number you wish to use to indicate no color. You should coordinate the color number of a specific Tetris brick with the index of the rray of colors in the display class.
The diagrams that follow were created by drawing an outline around each cell representing the available positions within the new background attribute that will represent the content of the well, especially as the bricks accumulate.
I have also printed the value of each cell to the center of that cell. This is for teaching purposes. You can see that as the
5
brick falls (fig. A) the color is not incorporated into the the background. The falling brick is drawn over the background. But when the brick reaches the point where it cannot fall with- out violating either a boundary or a color condition ( fig. B ), then the color of that brick is incorporated into the background and a new brick spawned. In this example the first color num- ber is 5 for the light blue.
Each time the falling brick stops, the color is incorporated into the background ( fig. C through fig. E ). This process continues until there is no room at the top of the background, at which point the game is over.
The process of incorporating the color into the background is simple. It consists of assigning the color number of the falling brick to the coordinates of the background that are stored in the positions array of the the falling brick, after the final move is validated.
The pseudo code for transfer of color process is given below
1. REPEAT for each segment of the brick’s position array 2. the background element designated by coordinate stored in that segment are assigned the falling brick’s color number
Color validation (validateMove method) uses the process of go- ing through each segment of the falling brick, taking its coordi- nates (row and col of background) and checking that position of the background, to make sure that the value stored there is not a color number. In the case of this example, check that the
number is not zero. If the value is not zero, the move in NOT VALID.
6
VALIDITY OF SIDEWAYS MOVE
1. Moving left - subtract 1 to each segment’s column
A. Check that the column coordinate is not less then 0
B. Check that there is no color in each segment of the new position
2. Moving right - add 1 to each column
A. Check that column is not greater or equal to maximum Column index
B. Check that there is no color in each segment of the new new positions array.
SECTION 3
Moving left and right The process of moving a brick to the left or right, is the same for all bricks and as such this method must be implemented in the TetrisBrick class. These methods had not argument and n0 return value.
When the left arrow key (VK_LEFT) is pressed: To move left, the column coordinate of each segment of the brick is decremented.
When the right arrow key (VK_RIGHT) is pressed: To move right, the column coordinate of each segment is incre- mented.
In general, the makeMove, validateMove and the associate move methods of the brick classes work together. The only method of these that has an argument is makeMove be- cause in must translate the move code.
To validate the left move (validateMove method), the brick must be in bounds (each segment must have a column num- ber greater than -1) and it must be color validated (see section 2 for this technique). If the move is invalid, it must be moved back (move right).
Like wise, to validate the right move, it must be in bounds (each segment must have a column number less than the number of column in the array) and it must be color validated (see section 2 for this technique). If the move is invalid, it must be reversed (move left).
7
VALIDITY OF ROTATION MOVE
1. rotation - each sub brick has a unique rotation method
A. Check that columns is not less then zero or greater then he number of columns
B. Check that rows are not equal or greater than the number of rows
C. Check that there is no color in new positions
SECTION 4
Rotation Since the coordinates for each type of brick are different, the method to rotate the brick is different for each brick and as such must be implement in in each individual subclass of the TetrisBrick. What you should notice is that there is different symmetry among the different bricks. For example, the square brick has only one appearance, no matter how you rotate it while the long brick has 2 appearances and the stack brick has 4 appearances. As such the rotation methods will be different.
So, when the up arrow key (VK_UP) is pressed in the dis- play class, the move code will indicate rotation. To rotate the sub brick, the rotate method in each sub brick (no argu- ments) will need to be unique to that sub brick. For example, the long brick has only two orientations; horizontal and verti- cal and as such only needs two configurations, which always pivot on segment 2. As such segment 2 does not change in the rotation. The same can be applied to the EssBrick and JayBrick.
8
The StackBrick, ElBrick and JayBrick have four orientations that must be rotated through.
For example, the StackBrick will rotate around segment 2, which is its center segment as shown to the right. The center segment does not change. The initial positions of each segment based on this center segment is shown to the left.
The un-rotate method is needed when the rotate method pro- duced an invalid move. An invalid move must be undone in the makeMove method.
For a brick with only two orientations, the un-rotate can be accomplished by simply rotating one more time. For a brick with all four orientation, simply rotate three more times to undo the rotation. Remember, you can call a method from the body of another method. Reuse your code, don’t reinvent!
The method shown here is only one way to do the rotation. You can also reference each segment from the center segment. It is your choice of how to do this implementation.
9
- Tetris Game Part 2
- Evaluation Criteria
- Transfer of color into back ground
- Moving left and right
- Rotation
- I NEED THIS BY TOMORROW
- Most European countries have nationalized their universities and colleges. Consider that some countries have also used the law to ban private colleges. Should higher education be classified as a natural monopoly in these European countries? Explain and ju
- MGT 325 Week 5 Final Paper - Peregrine Trucking Co.
- Macroeconomics
- 750 words
- the_ideas
- Environmental Health: Local to Global: Module 2 Quiz
- Calculates
- Schedules and budgets
- CJM 331 WEEK 3: Chapter 2: Corruption, Drug Trafficking, and Violence in Mexico