SlideShare a Scribd company logo
1 of 19
Download to read offline
Hi there I am having difficulty in finalizing my Tetris game , below I posted the codes for the
classes and I need the rest six pieces to be done. Can anybody help me here. It is a java code and
must use the code I posted bellow. Thanks,
Completing Tetris
[Description: shapes]
Overview
You will be completing the game Tetris. The features needed are
1. Adding the other 6 pieces
2. Adding the ability to rotate the pieces
Concepts
The purpose of this assignment is to gain experience with the following new concepts:
inheritance/class hierarchy
dynamic dispatch
algorithms
If you were not able to complete homework 1, please come see me to get a working solution.
Implementation Requirements
OTHER PIECES: The first step in the assignment is to add the other six pieces. Lets think a little
about the planning though; we don't want the Game class to have to know about all 7 different
piece types (we wouldn't want Game to have 7 different instance variables, 1 for each type. How
would it keep track of which one was current?) Instead, Game should know about 1 type (a super
type) and let dynamic dispatch do the work for us.
So to start, we need to redesign (or refactor) the current code. To do this, we want a super class
that contains everything common to all pieces; then sub classes for the individual pieces and
their individual needs. So what is the same about all the pieces?
the state currently handled by LPiece is common to all pieces
all the behaviors currently in LPiece are common to all pieces (except the constructor of course)
What is different about each piece:
How each individual playing piece is constructed
The implementation of how each rotates (the second feature for this assignment)
1) Therefore, start by breaking up the LPiece.java class into a super class and sub class. At this
point, test your program. It should run as it did before.
2) Now its time to add the other game pieces. You will need to figure out how to initialize 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 from a previous shape and modifying it. The
pieces should be initialized in the following orientations:
[Description: shapes]
In the Game class, the piece instance variable must have a new type. What do you think it should
be?
You'll need to modify the Game class so that it doesn't always instantiate an LPiece piece, but
randomly chooses which shape to instantiate. (I strongly recommend creating 1 new shape at a
time and testing.)
ROTATION: The current tetris piece needs to be able to rotate in quarter turns clockwise each
time the 'r' key is pressed. As an example see the figure below illustrating how the L-shaped
piece rotates:
[Description: Lrotate]
Here is where dynamic dispatch will come in handy since each shape class must have its own
rotate implementation.
Update the existing classes (super and sub classes) to enable dynamic dispatch to work properly.
Modify the EventController class to react to the 'r' key.
Rules for Rotation: A piece should only rotate if
the grid squares that it would pass through during rotation are empty, and
the grid squares that it would occupy after rotating are all empty, and
all the squares are within the boundaries of the grid as the piece rotates.
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.
Square 1 must "move through" the upper left square (row 0, col 0) before it stops in its final
location (row 0, col 1)
[Description: sampleRotation]
Square 2 is chosen as the pivot Square because it is used as the "base" square when the LPiece
is constructed. If you look at the constructor for the LPiece, the (r, c) parameters define the
position of the middle square on the long side. That is the square rotated around. For each shape,
you need to determine which square is closest to the "middle" and rotate around that.
For most pieces, the square to rotate around is the one establish by the (r,c) parameters. For the
square shape, though, it makes sense to consider the geometric center as the center of rotation
instead. This makes the rotation a trivial solution. (This is not true for the straight piece).
If a piece is rotated 4 times, it should be back in its original orientation and its original location
on the grid (assuming no other movement). In other words, no "drifting" over the grid.
Here's an example of the sort of thing that boundary checking on rotation should prevent:
[Description: boundary]
The other consideration when implementing this feature is "how to check if the rotate is legal?
Are the appropriate squares on the grid empty?" As we've discussed in class, implementation is
left as a developer's choice, but there are some guidelines you must follow:
Notice that in the move() method of a piece, the piece queries the individual squares if they can
move, and then tells them to move. At no time does the piece ever query the Grid directly.
Implement rotate the same way. Find out if the individual squares can move as needed, then
move them. You may add more methods to the Square public interface. In some rotations, a
Square actually has to move up. You may want to add an UP constant (to the appropriate class)
and modify the appropriate methods/comments in the Square class accordingly.
Written Report
Planning: How did you plan and organize your code?
Testing: How did you test your code? What sort of bugs did you encounter? Are there any
unresolved problems in the code? Be sure to list any ways in which the program falls short of the
specification.
Evaluate this project. What did you learn from it, or what did it help you remember? Was it
worth the effort? This could include things you learned about specifications and interfaces,
design issues, Java language issues, debugging, etc.
Please bring a printout of your written report to class the day after the due date.
Grading
/10 execution (does it work properly)
/4 design
/4 documentation (both internal and external), style
/2 written report
The codes are:
import java.awt.*;
/**
* Manages the game Tetris. Keeps track of the current piece and the grid.
* Updates the display whenever the state of the game has changed.
*
* @author Anthony Bladek
* @version 01/23/2017
*/
public class Game
{
private Grid theGrid; // the grid that makes up the Tetris board
private Tetris theDisplay; // the visual for the Tetris game
private LPiece piece; // the current piece that is dropping
private boolean isOver; // has the game finished?
// possible move directions
public static final int LEFT = 0;
public static final int RIGHT = 1;
public static final int DOWN = 2;
/**
* Create a Tetris game
* @param Tetris the display
*/
public Game(Tetris display)
{
theGrid = new Grid();
theDisplay = display;
piece = new LPiece(1, Grid.WIDTH/2 -1, theGrid);
isOver = false;
}
/** Draw the current state of the game
* @param g the Graphics context on which to draw
*/
public void draw(Graphics g)
{
theGrid.draw(g);
if (piece != null)
piece.draw(g);
}
/** Move the piece in the given direction
* @param the direction to move
* @throws IllegalArgumentException if direction is not legal
*/
public void movePiece(int direction){
if (piece != null)
piece.move(direction);
updatePiece();
theDisplay.update();
theGrid.checkRows();
}
/**
* Returns true if the game is over
*/
public boolean isGameOver() {
// game is over if the piece occupies the same space as some non-empty
// part of the grid. Usually happens when a new piece is made
if (piece == null)
return false;
// check if game is already over
if (isOver)
return true;
// check every part of the piece
Point[] p = piece.getLocations();
for (int i = 0; i = WIDTH || col < 0 || col >= HEIGHT
*/
public void set(int row, int col, Color c) {
board[row][col].setColor(c);
}
/**
* Check for and remove all solid rows of squares
* If a solid row is found and removed, all rows above
* it are moved down and the top row set to empty
*/
public void checkRows() {
boolean rowFull = true;
int i = HEIGHT - 1;
while (i > 0)
{
rowFull = true;
for(int j = 0; j< WIDTH;j++)
{
if(!isSet(i,j))// colum is empty don't need to remove a row
{
rowFull = false;
i --; // only go to the next row if it is not full;
break;
}
}
if(rowFull)
{
System.out.println("Found a full row. row: " + i );
// redraw the grid with the every row above the one found drawn with the value above it.
shiftRows(i);
setRowEmpty(0);
}
}
}
/* Shift all the Rows above the input down one row
* private method so it does not have to have public comments
* input parameter is integer value of the row that was found to be full.
*/
private void shiftRows(int fullRow)
{
for(int m = fullRow; m > 1; m--)
{
for(int n = 0; n < WIDTH;n++)
{
set(m,n,board[m-1][n].getColor());
}
}
}
/*Set one row to empty
Input is the row to set to empty.
*/
private void setRowEmpty(int rowNum)
{
for(int n = 0;n < WIDTH; n++)
set(rowNum,n,EMPTY);
}
/**
* Draw the grid on the given Graphics context
* @param Graphics you are drawing with.
*/
public void draw(Graphics g){
// draw the edges as rectangles: left, right in blue then bottom in red
g.setColor(Color.blue);
g.fillRect(LEFT - BORDER, TOP, BORDER, HEIGHT * Square.HEIGHT);
g.fillRect(LEFT + WIDTH * Square.WIDTH, TOP, BORDER, HEIGHT * Square.HEIGHT);
g.setColor(Color.red);
g.fillRect(LEFT - BORDER, TOP + HEIGHT * Square.HEIGHT,
WIDTH * Square.WIDTH + 2 * BORDER, BORDER);
// draw all the squares in the grid
for (int r = 0; r < HEIGHT; r++)
for(int c = 0; c < WIDTH; c++)
board[r][c].draw(g);
}
}
import java.awt.*;
/**
* An LPiece item in the Tetris Game.
*
* This piece is made up of 4 squares in the following configuration
* Sq
* Sq
* Sq Sq
*
* The game piece "floats above" the Grid. The (row, col) coordinates
* are the location of the middle Square on the side within the Grid
*
* @author CSC 143
*/
public class LPiece
{
private boolean ableToMove; // can this piece move
private Square[] square; // the Squares that make up this piece
// Made up of PIECE_COUNT squares
private Grid theGrid; // the board this piece is on
// number of squares in 1 Tetris game piece
private static final int PIECE_COUNT = 4;
/**
* Create an L-Shape piece. See class description for actual location
* of r and c
* @param r row location for this piece
* @param c column location for this piece
* @param g the grid for this game piece
*
*/
public LPiece(int r, int c, Grid g)
{
theGrid = g;
square = new Square[PIECE_COUNT];
ableToMove = true;
// Create the squares
square[0] = new Square(g, r - 1, c, Color.magenta, true);
square[1] = new Square(g, r, c , Color.magenta, true);
square[2] = new Square(g, r + 1, c, Color.magenta, true);
square[3] = new Square(g, r + 1, c + 1, Color.magenta, true);
}
/**
* Draw the piece on the given Graphics context
*/
public void draw(Graphics g){
for (int i = 0; i < PIECE_COUNT; i++)
square[i].draw(g);
}
/**
* Move the piece if possible
* Freeze the piece if it cannot move down anymore
* @param direction the direction to move
* @throws IllegalArgumentException if direction is not Square.DOWN,
* Square.LEFT, or Square.RIGHT
*/
public void move(int direction){
if (canMove(direction)){
for (int i = 0; i < PIECE_COUNT; i++)
square[i].move(direction);
}
// if we couldn't move, see if because we're at the bottom
else if (direction == Game.DOWN){
ableToMove = false;
}
}
/** Return the (row,col) grid coordinates occupied by this Piece
* @return an Array of (row,col) Points
*/
public Point[] getLocations(){
Point[] points = new Point[PIECE_COUNT];
for(int i = 0; i < PIECE_COUNT; i++) {
points[i] = new Point(square[i].getRow(), square[i].getCol());
}
return points;
}
/**
* Return the color of this piece
*/
public Color getColor() {
// all squares of this piece have the same color
return square[0].getColor();
}
/**
* Returns if this piece can move in the given direction
* @throws IllegalArgumentException if direction is not Square.DOWN,
* Square.LEFT, or Square.RIGHT
*/
public boolean canMove(int direction) {
if (!ableToMove)
return false;
//Each square must be able to move in that direction
boolean answer = true;
for (int i = 0; i < PIECE_COUNT; i++) {
answer = answer && square[i].canMove(direction);
}
return answer;
}
}
import java.awt.*;
/**
* One Square on our Tetris Grid or one square in our Tetris game piece
*
* @author Anthony Bladek
* @version 01/23/2017
*/
public class Square
{
private Grid theGrid; // the environment where this Square is
private int row, col; // the grid location of this Square
private boolean ableToMove; // true if this Square can move
private Color color; // the color of this Square
// possible move directions are defined by the Game class
// dimensions of a Square
public static final int WIDTH = 20;
public static final int HEIGHT = 20;
/**
* Constructor for objects of class Square
* @param g the Grid for this Square
* @param row the row of this Square in the Grid
* @param col the column of this Square in the Grid
* @param c the Color of this Square
* @param mobile true if this Square can move
*
* @throws IllegalArgumentException if row and col not within the Grid
*/
public Square(Grid g, int row, int col, Color c, boolean mobile){
if (row < 0 || row > g.HEIGHT-1)
throw new IllegalArgumentException("Invalid row =" + row);
if (col < 0 || col > g.WIDTH-1)
throw new IllegalArgumentException("Invalid column = " + col);
// initialize instance variables
theGrid = g;
this.row = row;
this.col = col;
color = c;
ableToMove = mobile;
}
/**
* Return the row for this Square
*/
public int getRow() {
return row;
}
/**
* Return the column for this Square
*/
public int getCol() {
return col;
}
/**
* Return true if this Square can move 1 spot in direction d
* @param direction the direction to test for possible move
* @throws IllegalArgumentException if direction is not valid
*/
public boolean canMove(int direction){
if (!ableToMove)
return false;
boolean move = true;
// if the given direction is blocked, we can't move
// remember to check the edges of the grid
switch(direction) {
case Game.DOWN:
if (row == (theGrid.HEIGHT -1) || theGrid.isSet(row + 1, col))
move = false;
break;
case Game.LEFT: // check conditions for left motion.
// stop motion if off the grid or another block is in the way.
if ((col == 0) || (theGrid.isSet(row, col -1)))
move = false;
break;
case Game.RIGHT: // check the conditions for right motion
// stop the motion if off the grid or another block is in the way.
if(col == (theGrid.WIDTH -1 ) || theGrid.isSet(row, col + 1))
move = false;
break;
default:
throw new IllegalArgumentException("Bad direction to Square.canMove()");
}
return move;
}
/** move Square in the given direction if possible
* Square will not move if direction is blocked, or Square is unable to move
* If attempt to move DOWN and it can't, the Square is frozen
* and cannot move anymore
* @param direction the direction to move
*/
public void move(int direction) {
if (canMove(direction)) {
switch(direction) {
case Game.DOWN:
row = row + 1;
break;
case Game.LEFT:
col = col - 1;
break;
case Game.RIGHT:
col = col + 1;
}
}
}
/** Change the color of the Square
* @param c the new color
*/
public void setColor(Color c) {
color = c;
}
/** Get the color of this Square
*/
public Color getColor() {
return color;
}
/**
* Draw this square on the given Graphics context
*/
public void draw(Graphics g) {
// calculate the upper left (x,y) coordinate of this square
int actualX = theGrid.LEFT + col * WIDTH;
int actualY = theGrid.TOP + row * HEIGHT;
g.setColor(color);
g.fillRect(actualX, actualY, WIDTH, HEIGHT);
}
}
/**
* Create and control the game Tetris.
*
* NOTE: when putting a Tetris object in a top-level container, make sure the Tetris
* object has the focus (use requestFocus())
*
* @author CSC 143 Based on a C++ version written by the
* University of Washington CSE department
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Tetris extends JPanel
{
private Game theGame;
/** Set up the parts for the Tetris game, display and user control
*/
public Tetris()
{
theGame = new Game(this);
GameController ec = new GameController(theGame);
addKeyListener(ec);
setBackground(Color.yellow);
}
/**
* Update the display
*/
public void update() {
repaint();
}
/**
* Paint the current state of the game
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
theGame.draw(g);
if (theGame.isGameOver() ) {
g.setFont(new Font("Palatino", Font.BOLD, 40));
g.setColor(Color.BLACK);
g.drawString("GAME OVER", 80, 300);
}
}
/* Create a game and play it*/
public static void play()
{
JFrame f = new JFrame("The Tetris Game");
Tetris t = new Tetris();
f.getContentPane().add(t);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,600);
f.validate();
f.setVisible(true);
t.requestFocus(); // so that Tetris (the JPanel) fires the keyboard events.
}
/**
* To be able to run from the command line or desktop
*/
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
play();
}
});
}
}
And also the class diagram if possible
Thanks
Solution
import java.awt.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.awt.event.*;
import java.io.*;
public final class Piece {
// The body of the piece, each point is a coordinate specifying a block
private static Point[] body;
// Each element specifies how high the piece will land in the corresponding column
private int[] skirt;
private int width; // The width of the piece for the current rotation
private int height; // The height of the piece for the current rotation
private static Piece next; // The "next" rotation - note this is how a "piece" is really a list
static private Piece[] pieces; // singleton array of first rotations
private static final int INITIAL_HIGH_VAL = 10;
private static final int INITIAL_ZERO = 0;
private static final int NUMBER_OF_BLOCKS = 4;
//set height and width
//set skirt
private Piece(Point[] points) {
System.out.print("Creating a new piece");
//create the body
//body = new Point[points.length];
//for (int i=0; i< points.length; i++)
//body[i] = points[i];
printBody();
//initial value for height and width
height = width = INITIAL_HIGH_VAL;
//sets holders equal to actual points
for(int i = 0; i < body.length; i++){
int x = (int)(body[i].getX());
int y = (int)(body[i].getY());
//set height to greatest found value of y
if(height == INITIAL_HIGH_VAL || height <= y) height = y+1;
//set width to greatest found value of x
if(width == INITIAL_HIGH_VAL || width <= x) width = x+1;
}
System.out.print (" Height = " + height + " Width = " + width);
//creates the skirt of size width
skirt = new int[width];
for (int j=0; j< width; j++) skirt[j]=INITIAL_HIGH_VAL;
for(int j = 0; j < width; j++){
for(int i = 0; i < body.length; i++){
//if the x value is equal to the "x-value"
//of the skirt, then put the y-value for that
//x-value into the skirt, if its the lowest
if((int)(body[i].getX()) == j){
int y = (int)(body[i].getY());
if(y <= skirt[j]) skirt[j] = y;
}
}
}
for (int i=0; i< body.length; i++) {
if (!(body[i].getX() < width)) System.out.println("BAD");
}
System.out.println();
}
public int getWidth() {
//System.out.println("Piece tester wanted the width.");
return(width);
}
public int getHeight() {
//System.out.println("Piece tester wanted the height.");
return(height);
}
public Point[] getBody() {
System.out.print("GetBody:");
printBody();
return(body);
}
public void printBody() {
System.out.print("Body:");
for(int i= 0; i

More Related Content

Similar to Complete Java Tetris game with inheritance

14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).pptyasser3omr
 
Enterprise Tic-Tac-Toe
Enterprise Tic-Tac-ToeEnterprise Tic-Tac-Toe
Enterprise Tic-Tac-ToeScott Wlaschin
 
I have another assignment due for an advance java programming class .pdf
I have another assignment due for an advance java programming class .pdfI have another assignment due for an advance java programming class .pdf
I have another assignment due for an advance java programming class .pdfFORTUNE2505
 
Presentation on Template Method Design Pattern
Presentation on Template Method Design PatternPresentation on Template Method Design Pattern
Presentation on Template Method Design PatternAsif Tayef
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxAbhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxGauravPandey43518
 
Write a C++ program which generates and displays a random walk acros.pdf
Write a C++ program which generates and displays a random walk acros.pdfWrite a C++ program which generates and displays a random walk acros.pdf
Write a C++ program which generates and displays a random walk acros.pdfmckenziecast21211
 
Introduction to Coding
Introduction to CodingIntroduction to Coding
Introduction to CodingFabio506452
 
Im looking for coding help I dont really need this to be explained.pdf
Im looking for coding help I dont really need this to be explained.pdfIm looking for coding help I dont really need this to be explained.pdf
Im looking for coding help I dont really need this to be explained.pdfcontact41
 
Exercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdf
Exercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdfExercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdf
Exercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdffms12345
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfRahul04August
 
Read carefully and follow exactly Java You are to write a Breakou.pdf
Read carefully and follow exactly Java You are to write a Breakou.pdfRead carefully and follow exactly Java You are to write a Breakou.pdf
Read carefully and follow exactly Java You are to write a Breakou.pdfrupeshmehta151
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsHoang Nguyen
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsTony Nguyen
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsFraboni Ec
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJames Wong
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsYoung Alista
 

Similar to Complete Java Tetris game with inheritance (20)

14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
Enterprise Tic-Tac-Toe
Enterprise Tic-Tac-ToeEnterprise Tic-Tac-Toe
Enterprise Tic-Tac-Toe
 
I have another assignment due for an advance java programming class .pdf
I have another assignment due for an advance java programming class .pdfI have another assignment due for an advance java programming class .pdf
I have another assignment due for an advance java programming class .pdf
 
Presentation on Template Method Design Pattern
Presentation on Template Method Design PatternPresentation on Template Method Design Pattern
Presentation on Template Method Design Pattern
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
Write a C++ program which generates and displays a random walk acros.pdf
Write a C++ program which generates and displays a random walk acros.pdfWrite a C++ program which generates and displays a random walk acros.pdf
Write a C++ program which generates and displays a random walk acros.pdf
 
Introduction to Coding
Introduction to CodingIntroduction to Coding
Introduction to Coding
 
Darkonoid
DarkonoidDarkonoid
Darkonoid
 
Im looking for coding help I dont really need this to be explained.pdf
Im looking for coding help I dont really need this to be explained.pdfIm looking for coding help I dont really need this to be explained.pdf
Im looking for coding help I dont really need this to be explained.pdf
 
Exercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdf
Exercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdfExercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdf
Exercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdf
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
Read carefully and follow exactly Java You are to write a Breakou.pdf
Read carefully and follow exactly Java You are to write a Breakou.pdfRead carefully and follow exactly Java You are to write a Breakou.pdf
Read carefully and follow exactly Java You are to write a Breakou.pdf
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 

More from fonecomp

write a program that prompts the user to enter the center of a recta.pdf
write a program that prompts the user to enter the center of a recta.pdfwrite a program that prompts the user to enter the center of a recta.pdf
write a program that prompts the user to enter the center of a recta.pdffonecomp
 
write 3 3 slide on China and Germany. Individual work (1) Choose a c.pdf
write 3 3 slide on China and Germany. Individual work (1) Choose a c.pdfwrite 3 3 slide on China and Germany. Individual work (1) Choose a c.pdf
write 3 3 slide on China and Germany. Individual work (1) Choose a c.pdffonecomp
 
Why do negotiations fail O Conflicts are boring O Conflicts are co.pdf
Why do negotiations fail O Conflicts are boring O Conflicts are co.pdfWhy do negotiations fail O Conflicts are boring O Conflicts are co.pdf
Why do negotiations fail O Conflicts are boring O Conflicts are co.pdffonecomp
 
What was the court-packing plan, and why is it sig- nificant to a.pdf
What was the court-packing plan, and why is it sig- nificant to a.pdfWhat was the court-packing plan, and why is it sig- nificant to a.pdf
What was the court-packing plan, and why is it sig- nificant to a.pdffonecomp
 
Who are the major stakeholders that Sony must consider when developi.pdf
Who are the major stakeholders that Sony must consider when developi.pdfWho are the major stakeholders that Sony must consider when developi.pdf
Who are the major stakeholders that Sony must consider when developi.pdffonecomp
 
What sort of prevention techniques would be useful when dealing with.pdf
What sort of prevention techniques would be useful when dealing with.pdfWhat sort of prevention techniques would be useful when dealing with.pdf
What sort of prevention techniques would be useful when dealing with.pdffonecomp
 
What are the main three types of organizational buyers How are they.pdf
What are the main three types of organizational buyers How are they.pdfWhat are the main three types of organizational buyers How are they.pdf
What are the main three types of organizational buyers How are they.pdffonecomp
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdffonecomp
 
Sharks are able to maintain their fluids hypertonic to the ocean env.pdf
Sharks are able to maintain their fluids hypertonic to the ocean env.pdfSharks are able to maintain their fluids hypertonic to the ocean env.pdf
Sharks are able to maintain their fluids hypertonic to the ocean env.pdffonecomp
 
Quantum Bank Inc. is a regional bank with branches throughout the so.pdf
Quantum Bank Inc. is a regional bank with branches throughout the so.pdfQuantum Bank Inc. is a regional bank with branches throughout the so.pdf
Quantum Bank Inc. is a regional bank with branches throughout the so.pdffonecomp
 
Neeb Corporation manufactures and sells a single product. The com.pdf
Neeb Corporation manufactures and sells a single product. The com.pdfNeeb Corporation manufactures and sells a single product. The com.pdf
Neeb Corporation manufactures and sells a single product. The com.pdffonecomp
 
Describe the primary, secondary, and tertiary structure of DNASo.pdf
Describe the primary, secondary, and tertiary structure of DNASo.pdfDescribe the primary, secondary, and tertiary structure of DNASo.pdf
Describe the primary, secondary, and tertiary structure of DNASo.pdffonecomp
 
I need help with this program for java.The program you are given t.pdf
I need help with this program for java.The program you are given t.pdfI need help with this program for java.The program you are given t.pdf
I need help with this program for java.The program you are given t.pdffonecomp
 
Harrison works in a cubicle at a window next to Karen Ravenwoods cu.pdf
Harrison works in a cubicle at a window next to Karen Ravenwoods cu.pdfHarrison works in a cubicle at a window next to Karen Ravenwoods cu.pdf
Harrison works in a cubicle at a window next to Karen Ravenwoods cu.pdffonecomp
 
I need help creating a parametized JUnit test case for the following.pdf
I need help creating a parametized JUnit test case for the following.pdfI need help creating a parametized JUnit test case for the following.pdf
I need help creating a parametized JUnit test case for the following.pdffonecomp
 
How do the genomes of Archaea and Bacteria compare Drag and drop th.pdf
How do the genomes of Archaea and Bacteria compare Drag and drop th.pdfHow do the genomes of Archaea and Bacteria compare Drag and drop th.pdf
How do the genomes of Archaea and Bacteria compare Drag and drop th.pdffonecomp
 
General question How would the technology affect who lives and who .pdf
General question How would the technology affect who lives and who .pdfGeneral question How would the technology affect who lives and who .pdf
General question How would the technology affect who lives and who .pdffonecomp
 
Describe how partial diploids can be produced in E. coli.Solutio.pdf
Describe how partial diploids can be produced in E. coli.Solutio.pdfDescribe how partial diploids can be produced in E. coli.Solutio.pdf
Describe how partial diploids can be produced in E. coli.Solutio.pdffonecomp
 
4.2Why is there waiting in an infinite-source queuing systemmul.pdf
4.2Why is there waiting in an infinite-source queuing systemmul.pdf4.2Why is there waiting in an infinite-source queuing systemmul.pdf
4.2Why is there waiting in an infinite-source queuing systemmul.pdffonecomp
 
A single layer of gold atoms lies on a table. The radius of each gol.pdf
A single layer of gold atoms lies on a table. The radius of each gol.pdfA single layer of gold atoms lies on a table. The radius of each gol.pdf
A single layer of gold atoms lies on a table. The radius of each gol.pdffonecomp
 

More from fonecomp (20)

write a program that prompts the user to enter the center of a recta.pdf
write a program that prompts the user to enter the center of a recta.pdfwrite a program that prompts the user to enter the center of a recta.pdf
write a program that prompts the user to enter the center of a recta.pdf
 
write 3 3 slide on China and Germany. Individual work (1) Choose a c.pdf
write 3 3 slide on China and Germany. Individual work (1) Choose a c.pdfwrite 3 3 slide on China and Germany. Individual work (1) Choose a c.pdf
write 3 3 slide on China and Germany. Individual work (1) Choose a c.pdf
 
Why do negotiations fail O Conflicts are boring O Conflicts are co.pdf
Why do negotiations fail O Conflicts are boring O Conflicts are co.pdfWhy do negotiations fail O Conflicts are boring O Conflicts are co.pdf
Why do negotiations fail O Conflicts are boring O Conflicts are co.pdf
 
What was the court-packing plan, and why is it sig- nificant to a.pdf
What was the court-packing plan, and why is it sig- nificant to a.pdfWhat was the court-packing plan, and why is it sig- nificant to a.pdf
What was the court-packing plan, and why is it sig- nificant to a.pdf
 
Who are the major stakeholders that Sony must consider when developi.pdf
Who are the major stakeholders that Sony must consider when developi.pdfWho are the major stakeholders that Sony must consider when developi.pdf
Who are the major stakeholders that Sony must consider when developi.pdf
 
What sort of prevention techniques would be useful when dealing with.pdf
What sort of prevention techniques would be useful when dealing with.pdfWhat sort of prevention techniques would be useful when dealing with.pdf
What sort of prevention techniques would be useful when dealing with.pdf
 
What are the main three types of organizational buyers How are they.pdf
What are the main three types of organizational buyers How are they.pdfWhat are the main three types of organizational buyers How are they.pdf
What are the main three types of organizational buyers How are they.pdf
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
 
Sharks are able to maintain their fluids hypertonic to the ocean env.pdf
Sharks are able to maintain their fluids hypertonic to the ocean env.pdfSharks are able to maintain their fluids hypertonic to the ocean env.pdf
Sharks are able to maintain their fluids hypertonic to the ocean env.pdf
 
Quantum Bank Inc. is a regional bank with branches throughout the so.pdf
Quantum Bank Inc. is a regional bank with branches throughout the so.pdfQuantum Bank Inc. is a regional bank with branches throughout the so.pdf
Quantum Bank Inc. is a regional bank with branches throughout the so.pdf
 
Neeb Corporation manufactures and sells a single product. The com.pdf
Neeb Corporation manufactures and sells a single product. The com.pdfNeeb Corporation manufactures and sells a single product. The com.pdf
Neeb Corporation manufactures and sells a single product. The com.pdf
 
Describe the primary, secondary, and tertiary structure of DNASo.pdf
Describe the primary, secondary, and tertiary structure of DNASo.pdfDescribe the primary, secondary, and tertiary structure of DNASo.pdf
Describe the primary, secondary, and tertiary structure of DNASo.pdf
 
I need help with this program for java.The program you are given t.pdf
I need help with this program for java.The program you are given t.pdfI need help with this program for java.The program you are given t.pdf
I need help with this program for java.The program you are given t.pdf
 
Harrison works in a cubicle at a window next to Karen Ravenwoods cu.pdf
Harrison works in a cubicle at a window next to Karen Ravenwoods cu.pdfHarrison works in a cubicle at a window next to Karen Ravenwoods cu.pdf
Harrison works in a cubicle at a window next to Karen Ravenwoods cu.pdf
 
I need help creating a parametized JUnit test case for the following.pdf
I need help creating a parametized JUnit test case for the following.pdfI need help creating a parametized JUnit test case for the following.pdf
I need help creating a parametized JUnit test case for the following.pdf
 
How do the genomes of Archaea and Bacteria compare Drag and drop th.pdf
How do the genomes of Archaea and Bacteria compare Drag and drop th.pdfHow do the genomes of Archaea and Bacteria compare Drag and drop th.pdf
How do the genomes of Archaea and Bacteria compare Drag and drop th.pdf
 
General question How would the technology affect who lives and who .pdf
General question How would the technology affect who lives and who .pdfGeneral question How would the technology affect who lives and who .pdf
General question How would the technology affect who lives and who .pdf
 
Describe how partial diploids can be produced in E. coli.Solutio.pdf
Describe how partial diploids can be produced in E. coli.Solutio.pdfDescribe how partial diploids can be produced in E. coli.Solutio.pdf
Describe how partial diploids can be produced in E. coli.Solutio.pdf
 
4.2Why is there waiting in an infinite-source queuing systemmul.pdf
4.2Why is there waiting in an infinite-source queuing systemmul.pdf4.2Why is there waiting in an infinite-source queuing systemmul.pdf
4.2Why is there waiting in an infinite-source queuing systemmul.pdf
 
A single layer of gold atoms lies on a table. The radius of each gol.pdf
A single layer of gold atoms lies on a table. The radius of each gol.pdfA single layer of gold atoms lies on a table. The radius of each gol.pdf
A single layer of gold atoms lies on a table. The radius of each gol.pdf
 

Recently uploaded

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 

Recently uploaded (20)

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 

Complete Java Tetris game with inheritance

  • 1. Hi there I am having difficulty in finalizing my Tetris game , below I posted the codes for the classes and I need the rest six pieces to be done. Can anybody help me here. It is a java code and must use the code I posted bellow. Thanks, Completing Tetris [Description: shapes] Overview You will be completing the game Tetris. The features needed are 1. Adding the other 6 pieces 2. Adding the ability to rotate the pieces Concepts The purpose of this assignment is to gain experience with the following new concepts: inheritance/class hierarchy dynamic dispatch algorithms If you were not able to complete homework 1, please come see me to get a working solution. Implementation Requirements OTHER PIECES: The first step in the assignment is to add the other six pieces. Lets think a little about the planning though; we don't want the Game class to have to know about all 7 different piece types (we wouldn't want Game to have 7 different instance variables, 1 for each type. How would it keep track of which one was current?) Instead, Game should know about 1 type (a super type) and let dynamic dispatch do the work for us. So to start, we need to redesign (or refactor) the current code. To do this, we want a super class that contains everything common to all pieces; then sub classes for the individual pieces and their individual needs. So what is the same about all the pieces? the state currently handled by LPiece is common to all pieces all the behaviors currently in LPiece are common to all pieces (except the constructor of course) What is different about each piece: How each individual playing piece is constructed The implementation of how each rotates (the second feature for this assignment) 1) Therefore, start by breaking up the LPiece.java class into a super class and sub class. At this point, test your program. It should run as it did before. 2) Now its time to add the other game pieces. You will need to figure out how to initialize 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 from a previous shape and modifying it. The pieces should be initialized in the following orientations:
  • 2. [Description: shapes] In the Game class, the piece instance variable must have a new type. What do you think it should be? You'll need to modify the Game class so that it doesn't always instantiate an LPiece piece, but randomly chooses which shape to instantiate. (I strongly recommend creating 1 new shape at a time and testing.) ROTATION: The current tetris piece needs to be able to rotate in quarter turns clockwise each time the 'r' key is pressed. As an example see the figure below illustrating how the L-shaped piece rotates: [Description: Lrotate] Here is where dynamic dispatch will come in handy since each shape class must have its own rotate implementation. Update the existing classes (super and sub classes) to enable dynamic dispatch to work properly. Modify the EventController class to react to the 'r' key. Rules for Rotation: A piece should only rotate if the grid squares that it would pass through during rotation are empty, and the grid squares that it would occupy after rotating are all empty, and all the squares are within the boundaries of the grid as the piece rotates. 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. Square 1 must "move through" the upper left square (row 0, col 0) before it stops in its final location (row 0, col 1) [Description: sampleRotation] Square 2 is chosen as the pivot Square because it is used as the "base" square when the LPiece is constructed. If you look at the constructor for the LPiece, the (r, c) parameters define the position of the middle square on the long side. That is the square rotated around. For each shape, you need to determine which square is closest to the "middle" and rotate around that. For most pieces, the square to rotate around is the one establish by the (r,c) parameters. For the square shape, though, it makes sense to consider the geometric center as the center of rotation instead. This makes the rotation a trivial solution. (This is not true for the straight piece). If a piece is rotated 4 times, it should be back in its original orientation and its original location
  • 3. on the grid (assuming no other movement). In other words, no "drifting" over the grid. Here's an example of the sort of thing that boundary checking on rotation should prevent: [Description: boundary] The other consideration when implementing this feature is "how to check if the rotate is legal? Are the appropriate squares on the grid empty?" As we've discussed in class, implementation is left as a developer's choice, but there are some guidelines you must follow: Notice that in the move() method of a piece, the piece queries the individual squares if they can move, and then tells them to move. At no time does the piece ever query the Grid directly. Implement rotate the same way. Find out if the individual squares can move as needed, then move them. You may add more methods to the Square public interface. In some rotations, a Square actually has to move up. You may want to add an UP constant (to the appropriate class) and modify the appropriate methods/comments in the Square class accordingly. Written Report Planning: How did you plan and organize your code? Testing: How did you test your code? What sort of bugs did you encounter? Are there any unresolved problems in the code? Be sure to list any ways in which the program falls short of the specification. Evaluate this project. What did you learn from it, or what did it help you remember? Was it worth the effort? This could include things you learned about specifications and interfaces, design issues, Java language issues, debugging, etc. Please bring a printout of your written report to class the day after the due date. Grading /10 execution (does it work properly) /4 design /4 documentation (both internal and external), style /2 written report The codes are: import java.awt.*; /** * Manages the game Tetris. Keeps track of the current piece and the grid. * Updates the display whenever the state of the game has changed. * * @author Anthony Bladek * @version 01/23/2017
  • 4. */ public class Game { private Grid theGrid; // the grid that makes up the Tetris board private Tetris theDisplay; // the visual for the Tetris game private LPiece piece; // the current piece that is dropping private boolean isOver; // has the game finished? // possible move directions public static final int LEFT = 0; public static final int RIGHT = 1; public static final int DOWN = 2; /** * Create a Tetris game * @param Tetris the display */ public Game(Tetris display) { theGrid = new Grid(); theDisplay = display; piece = new LPiece(1, Grid.WIDTH/2 -1, theGrid); isOver = false; } /** Draw the current state of the game * @param g the Graphics context on which to draw */ public void draw(Graphics g) { theGrid.draw(g); if (piece != null) piece.draw(g); } /** Move the piece in the given direction
  • 5. * @param the direction to move * @throws IllegalArgumentException if direction is not legal */ public void movePiece(int direction){ if (piece != null) piece.move(direction); updatePiece(); theDisplay.update(); theGrid.checkRows(); } /** * Returns true if the game is over */ public boolean isGameOver() { // game is over if the piece occupies the same space as some non-empty // part of the grid. Usually happens when a new piece is made if (piece == null) return false; // check if game is already over if (isOver) return true; // check every part of the piece Point[] p = piece.getLocations(); for (int i = 0; i = WIDTH || col < 0 || col >= HEIGHT */ public void set(int row, int col, Color c) { board[row][col].setColor(c); } /** * Check for and remove all solid rows of squares * If a solid row is found and removed, all rows above * it are moved down and the top row set to empty
  • 6. */ public void checkRows() { boolean rowFull = true; int i = HEIGHT - 1; while (i > 0) { rowFull = true; for(int j = 0; j< WIDTH;j++) { if(!isSet(i,j))// colum is empty don't need to remove a row { rowFull = false; i --; // only go to the next row if it is not full; break; } } if(rowFull) { System.out.println("Found a full row. row: " + i ); // redraw the grid with the every row above the one found drawn with the value above it. shiftRows(i); setRowEmpty(0); } } } /* Shift all the Rows above the input down one row * private method so it does not have to have public comments * input parameter is integer value of the row that was found to be full. */ private void shiftRows(int fullRow) { for(int m = fullRow; m > 1; m--) { for(int n = 0; n < WIDTH;n++)
  • 7. { set(m,n,board[m-1][n].getColor()); } } } /*Set one row to empty Input is the row to set to empty. */ private void setRowEmpty(int rowNum) { for(int n = 0;n < WIDTH; n++) set(rowNum,n,EMPTY); } /** * Draw the grid on the given Graphics context * @param Graphics you are drawing with. */ public void draw(Graphics g){ // draw the edges as rectangles: left, right in blue then bottom in red g.setColor(Color.blue); g.fillRect(LEFT - BORDER, TOP, BORDER, HEIGHT * Square.HEIGHT); g.fillRect(LEFT + WIDTH * Square.WIDTH, TOP, BORDER, HEIGHT * Square.HEIGHT); g.setColor(Color.red); g.fillRect(LEFT - BORDER, TOP + HEIGHT * Square.HEIGHT, WIDTH * Square.WIDTH + 2 * BORDER, BORDER); // draw all the squares in the grid for (int r = 0; r < HEIGHT; r++) for(int c = 0; c < WIDTH; c++) board[r][c].draw(g); } } import java.awt.*; /**
  • 8. * An LPiece item in the Tetris Game. * * This piece is made up of 4 squares in the following configuration * Sq * Sq * Sq Sq * * The game piece "floats above" the Grid. The (row, col) coordinates * are the location of the middle Square on the side within the Grid * * @author CSC 143 */ public class LPiece { private boolean ableToMove; // can this piece move private Square[] square; // the Squares that make up this piece // Made up of PIECE_COUNT squares private Grid theGrid; // the board this piece is on // number of squares in 1 Tetris game piece private static final int PIECE_COUNT = 4; /** * Create an L-Shape piece. See class description for actual location * of r and c * @param r row location for this piece * @param c column location for this piece * @param g the grid for this game piece * */ public LPiece(int r, int c, Grid g) {
  • 9. theGrid = g; square = new Square[PIECE_COUNT]; ableToMove = true; // Create the squares square[0] = new Square(g, r - 1, c, Color.magenta, true); square[1] = new Square(g, r, c , Color.magenta, true); square[2] = new Square(g, r + 1, c, Color.magenta, true); square[3] = new Square(g, r + 1, c + 1, Color.magenta, true); } /** * Draw the piece on the given Graphics context */ public void draw(Graphics g){ for (int i = 0; i < PIECE_COUNT; i++) square[i].draw(g); } /** * Move the piece if possible * Freeze the piece if it cannot move down anymore * @param direction the direction to move * @throws IllegalArgumentException if direction is not Square.DOWN, * Square.LEFT, or Square.RIGHT */ public void move(int direction){ if (canMove(direction)){ for (int i = 0; i < PIECE_COUNT; i++) square[i].move(direction); } // if we couldn't move, see if because we're at the bottom else if (direction == Game.DOWN){ ableToMove = false; } }
  • 10. /** Return the (row,col) grid coordinates occupied by this Piece * @return an Array of (row,col) Points */ public Point[] getLocations(){ Point[] points = new Point[PIECE_COUNT]; for(int i = 0; i < PIECE_COUNT; i++) { points[i] = new Point(square[i].getRow(), square[i].getCol()); } return points; } /** * Return the color of this piece */ public Color getColor() { // all squares of this piece have the same color return square[0].getColor(); } /** * Returns if this piece can move in the given direction * @throws IllegalArgumentException if direction is not Square.DOWN, * Square.LEFT, or Square.RIGHT */ public boolean canMove(int direction) { if (!ableToMove) return false; //Each square must be able to move in that direction boolean answer = true; for (int i = 0; i < PIECE_COUNT; i++) { answer = answer && square[i].canMove(direction); } return answer; }
  • 11. } import java.awt.*; /** * One Square on our Tetris Grid or one square in our Tetris game piece * * @author Anthony Bladek * @version 01/23/2017 */ public class Square { private Grid theGrid; // the environment where this Square is private int row, col; // the grid location of this Square private boolean ableToMove; // true if this Square can move private Color color; // the color of this Square // possible move directions are defined by the Game class // dimensions of a Square public static final int WIDTH = 20; public static final int HEIGHT = 20; /** * Constructor for objects of class Square * @param g the Grid for this Square * @param row the row of this Square in the Grid * @param col the column of this Square in the Grid * @param c the Color of this Square * @param mobile true if this Square can move * * @throws IllegalArgumentException if row and col not within the Grid */ public Square(Grid g, int row, int col, Color c, boolean mobile){ if (row < 0 || row > g.HEIGHT-1) throw new IllegalArgumentException("Invalid row =" + row); if (col < 0 || col > g.WIDTH-1) throw new IllegalArgumentException("Invalid column = " + col);
  • 12. // initialize instance variables theGrid = g; this.row = row; this.col = col; color = c; ableToMove = mobile; } /** * Return the row for this Square */ public int getRow() { return row; } /** * Return the column for this Square */ public int getCol() { return col; } /** * Return true if this Square can move 1 spot in direction d * @param direction the direction to test for possible move * @throws IllegalArgumentException if direction is not valid */ public boolean canMove(int direction){ if (!ableToMove) return false; boolean move = true; // if the given direction is blocked, we can't move // remember to check the edges of the grid switch(direction) { case Game.DOWN:
  • 13. if (row == (theGrid.HEIGHT -1) || theGrid.isSet(row + 1, col)) move = false; break; case Game.LEFT: // check conditions for left motion. // stop motion if off the grid or another block is in the way. if ((col == 0) || (theGrid.isSet(row, col -1))) move = false; break; case Game.RIGHT: // check the conditions for right motion // stop the motion if off the grid or another block is in the way. if(col == (theGrid.WIDTH -1 ) || theGrid.isSet(row, col + 1)) move = false; break; default: throw new IllegalArgumentException("Bad direction to Square.canMove()"); } return move; } /** move Square in the given direction if possible * Square will not move if direction is blocked, or Square is unable to move * If attempt to move DOWN and it can't, the Square is frozen * and cannot move anymore * @param direction the direction to move */ public void move(int direction) { if (canMove(direction)) { switch(direction) { case Game.DOWN: row = row + 1; break; case Game.LEFT: col = col - 1; break; case Game.RIGHT: col = col + 1;
  • 14. } } } /** Change the color of the Square * @param c the new color */ public void setColor(Color c) { color = c; } /** Get the color of this Square */ public Color getColor() { return color; } /** * Draw this square on the given Graphics context */ public void draw(Graphics g) { // calculate the upper left (x,y) coordinate of this square int actualX = theGrid.LEFT + col * WIDTH; int actualY = theGrid.TOP + row * HEIGHT; g.setColor(color); g.fillRect(actualX, actualY, WIDTH, HEIGHT); } } /** * Create and control the game Tetris. * * NOTE: when putting a Tetris object in a top-level container, make sure the Tetris * object has the focus (use requestFocus()) *
  • 15. * @author CSC 143 Based on a C++ version written by the * University of Washington CSE department */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Tetris extends JPanel { private Game theGame; /** Set up the parts for the Tetris game, display and user control */ public Tetris() { theGame = new Game(this); GameController ec = new GameController(theGame); addKeyListener(ec); setBackground(Color.yellow); } /** * Update the display */ public void update() { repaint(); } /** * Paint the current state of the game */ public void paintComponent(Graphics g) { super.paintComponent(g); theGame.draw(g); if (theGame.isGameOver() ) {
  • 16. g.setFont(new Font("Palatino", Font.BOLD, 40)); g.setColor(Color.BLACK); g.drawString("GAME OVER", 80, 300); } } /* Create a game and play it*/ public static void play() { JFrame f = new JFrame("The Tetris Game"); Tetris t = new Tetris(); f.getContentPane().add(t); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(400,600); f.validate(); f.setVisible(true); t.requestFocus(); // so that Tetris (the JPanel) fires the keyboard events. } /** * To be able to run from the command line or desktop */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { play(); } }); } } And also the class diagram if possible Thanks Solution
  • 17. import java.awt.*; import java.util.*; import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.util.*; import java.awt.event.*; import java.io.*; public final class Piece { // The body of the piece, each point is a coordinate specifying a block private static Point[] body; // Each element specifies how high the piece will land in the corresponding column private int[] skirt; private int width; // The width of the piece for the current rotation private int height; // The height of the piece for the current rotation private static Piece next; // The "next" rotation - note this is how a "piece" is really a list static private Piece[] pieces; // singleton array of first rotations private static final int INITIAL_HIGH_VAL = 10; private static final int INITIAL_ZERO = 0; private static final int NUMBER_OF_BLOCKS = 4; //set height and width //set skirt private Piece(Point[] points) { System.out.print("Creating a new piece"); //create the body //body = new Point[points.length]; //for (int i=0; i< points.length; i++)
  • 18. //body[i] = points[i]; printBody(); //initial value for height and width height = width = INITIAL_HIGH_VAL; //sets holders equal to actual points for(int i = 0; i < body.length; i++){ int x = (int)(body[i].getX()); int y = (int)(body[i].getY()); //set height to greatest found value of y if(height == INITIAL_HIGH_VAL || height <= y) height = y+1; //set width to greatest found value of x if(width == INITIAL_HIGH_VAL || width <= x) width = x+1; } System.out.print (" Height = " + height + " Width = " + width); //creates the skirt of size width skirt = new int[width]; for (int j=0; j< width; j++) skirt[j]=INITIAL_HIGH_VAL; for(int j = 0; j < width; j++){ for(int i = 0; i < body.length; i++){ //if the x value is equal to the "x-value" //of the skirt, then put the y-value for that //x-value into the skirt, if its the lowest if((int)(body[i].getX()) == j){ int y = (int)(body[i].getY()); if(y <= skirt[j]) skirt[j] = y; } } } for (int i=0; i< body.length; i++) { if (!(body[i].getX() < width)) System.out.println("BAD");
  • 19. } System.out.println(); } public int getWidth() { //System.out.println("Piece tester wanted the width."); return(width); } public int getHeight() { //System.out.println("Piece tester wanted the height."); return(height); } public Point[] getBody() { System.out.print("GetBody:"); printBody(); return(body); } public void printBody() { System.out.print("Body:"); for(int i= 0; i