Hi there I am having difficulty in finalizing my Tetris game , below I posted th
ID: 3783928 • Letter: H
Question
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 <p.length; i++)
if (theGrid.isSet((int) p[i].getX(), (int) p[i].getY()) ) {
isOver = true;
return true;
}
return false;
}
// Update the piece
private void updatePiece() {
if (piece == null) {
piece = new LPiece(1, Grid.WIDTH/2 -1, theGrid); // add new piece to the game.
}
// set Grid positions corresponding to frozen piece
// and then release the piece
else if (!piece.canMove(Game.DOWN)) {
Point [] p = piece.getLocations();
Color c = piece.getColor();
for (int i =0; i < p.length; i++){
theGrid.set((int)p[i].getX(), (int)p[i].getY(), c);
}
piece = null;
}
}
}
/**
* Handles events for the Tetris Game. User events (key strokes) as well as periodic timer
* events.
*
* @author Anthony Bladek
* @version 01/23/2017
*/
import java.awt.event.*;
import javax.swing.*;
public class GameController extends KeyAdapter implements ActionListener
{
private Game theGame;
private Timer timer;
private static final double PIECE_MOVE_TIME = 0.8; //controls time between
//piece moving down
//increase to slow it down
private boolean gameOver;
/**
* Constructor for objects of class EventController
* @param g the game this is controlling
*/
public GameController(Game g) {
theGame = g;
gameOver = false;
double delay = 1000 * PIECE_MOVE_TIME; // in milliseconds
timer = new Timer((int)delay, this);
timer.setCoalesce(true); // if multiple events pending, bunch them to 1 event
timer.start();
}
/*
* Respond to special keys being pressed
* Currently just responds to the space key
* @param KeyEvent e Event from the keyboard.
*/
public void keyPressed(KeyEvent e) {
if (!gameOver) {
switch(e.getKeyCode()) {
case KeyEvent.VK_SPACE :
case KeyEvent.VK_DOWN :
handleMove(Game.DOWN);
break;
// HANDLE other keystrokes here
case KeyEvent.VK_RIGHT :
handleMove(Game.RIGHT);
break;
case KeyEvent.VK_LEFT:
handleMove(Game.LEFT);
break;
}
}
}
/** Update the game periodically based on a timer event
* @param ActionEvent e Action from the keyboard
*/
public void actionPerformed(ActionEvent e) {
handleMove(Game.DOWN);
}
/** Update the game by moving in the given direction
* @param direction direction of motion based upon constants set above
*/
private void handleMove(int direction){
theGame.movePiece(direction);
gameOver = theGame.isGameOver();
if (gameOver)
timer.stop();
}
}
import java.awt.*;
/**
* This is the Tetris board represented by a (HEIGHT - by - WIDTH) matrix of Squares
* The upper left Square is at (0,0) The lower right Square is at (HEIGHT -1, WIDTH -1)
* Given a Square at (x,y) the square to the left is at (x-1, y)
* the square below is at (x, y+1)
*
* Each Square has a color. A white Square is EMPTY; any other color means that spot is
* occupied (i.e. a piece cannot move over/to an occupied square). A grid will also remove
* completely full rows.
*
* @author Anthony Bladek
* @version 01/23/2017
*/
public class Grid
{
private Square[][] board;
// Width and Height of Grid in number of squares
public static final int HEIGHT = 20;
public static final int WIDTH = 10;
private static final int BORDER = 5;
public static final int LEFT = 100; // pixel position of left of grid
public static final int TOP = 75; // pixel position of top of grid
private static final Color EMPTY = Color.white;
/**
* Create the Grid
*/
public Grid()
{
board = new Square[HEIGHT][WIDTH];
//put squares in the board
for (int row = 0; row < HEIGHT; row++)
for(int col = 0; col < WIDTH; col++)
board[row][col] = new Square(this, row, col, EMPTY, false);
}
/**
* Returns true if the location (row, col) on the grid is occupied
* @param row the row in the grid
* @param col the column in the grid
*/
public boolean isSet( int row, int col){
boolean isEmpty = board[row][col].getColor().equals(EMPTY);
return !isEmpty;
}
/**
* Change the color of the Square at the given location
* @param row the row of the Square in the Grid
* @param col the column of the Square in the Grid
* @param c the color to set the Square
* @throws IndexOutOfBoundsException if row < 0 || row>= 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<br>
* Sq<br>
* Sq Sq<br>
*
* 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
Explanation / Answer
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<body.length; i++){
System.out.print("(" + i + ", " + body[i] + ") ");
}
System.out.println();
}
public int[] getSkirt() {
System.out.print("GetSkirt:");
System.out.print(" width=" + width + ", " );
for(int i= 0; i<width; i++){
System.out.print("(" + i + ", " + skirt[i] + ") ");
}
System.out.println();
return(skirt);
}
public Piece nextRotation() {
System.out.println("Piece tester has called for the Next piece.");
return next;
}
public boolean equals(Piece other) {
//variable that counts to see if
//one piece equals the other
int countTrue = INITIAL_ZERO;
System.out.println("Equals has been asked for");
//if bodies have different sizes, then they cannot be equal
if (body.length != other.getBody().length) return false;
//looks at all of the points and
//checks if they match or not
for(int i = 0; i< body.length; i++){
//for every point
for(int j = 0; j< other.getBody().length; j++){
if(body[i] == other.getBody()[j]){
countTrue++;
//System.out.println("CountTrue = " + countTrue);
}
}
}
//System.out.println((body.length) + " = length & countTrue = " + countTrue);
//if all four points have matched,
//then it is true
if(countTrue == body.length){
System.out.println("the two pieces are equal");
return true;
}else{
System.out.println("They are not equal");
return false;
}
}
Piece pieceRow(Piece starter) {
public static
System.out.println("STARTER = ");
for(int i = 0; i<NUMBER_OF_BLOCKS; i ++){
System.out.println(starter.getBody()[i]);
}
System.out.println();
Piece nextRotation = rotate(starter.getBody());
Piece prev = starter;
//while the temporary body doesnt equal the orignial
while(!starter.equals(nextRotation.getBody())){
prev.next = nextRotation;
prev = nextRotation;
nextRotation = rotate(nextRotation.getBody());
}
if(!starter.equals(nextRotation.getBody())){
System.out.println("They are equal!!");
}
//link last to starter
prev.next = starter;
}
*/
starter.next = starter;
return starter;
}
public static Piece rotate(Point[] pieceBody){
String tempPoints = "";
//how far the piece is off from
//the origin
int offVal = INITIAL_ZERO;
//place holders for x and y
int y = INITIAL_ZERO;
int x = INITIAL_ZERO;
//calculates the offVal by looking at the
//new y value that is furthest from the origin
for(int i = 0; i< NUMBER_OF_BLOCKS; i++){
y = -(int) pieceBody[i].getX();
if(y < offVal){
offVal = y;
}
}
//creates the tempbody with new points
for(int i = 0; i< NUMBER_OF_BLOCKS; i++){
x = (int) pieceBody[i].getY();
y = -(int) pieceBody[i].getX();
tempPoints = tempPoints + x +" " + (y-offVal) + " ";
}
//returns that point
return new Piece((parsePoints(tempPoints)));
}
public static Piece[] getPieces() {
// Makes seven calls to pieceRow for each of the seven standard Tetris pieces.
//Places the results of each call into an array and returns the array.
pieces = new Piece[] {
pieceRow(new Piece(parsePoints("0 0 0 1 0 2 0 3"))), // 0
pieceRow(new Piece(parsePoints("0 0 0 1 0 2 1 0"))), // 1
pieceRow(new Piece(parsePoints("0 0 1 0 1 1 1 2"))), // 2
pieceRow(new Piece(parsePoints("0 0 1 0 1 1 2 1"))), // 3
pieceRow(new Piece(parsePoints("0 1 1 1 1 0 2 0"))), // 4
pieceRow(new Piece(parsePoints("0 0 0 1 1 0 1 1"))), // 5
pieceRow(new Piece(parsePoints("0 0 1 0 1 1 2 0"))), // 6
};
return pieces;
}
private static Point[] parsePoints(String string) {
Vector points = new Vector();
StringTokenizer tok = new StringTokenizer(string);
try {
while(tok.hasMoreTokens()) {
int x = Integer.parseInt(tok.nextToken());
int y = Integer.parseInt(tok.nextToken());
points.addElement(new Point(x, y));
}
}
catch (NumberFormatException e) {
throw new RuntimeException("Could not parse x,y string:" + string);
// cheap way to do assert
}
// Make an array out of the Vector
Point[] array = new Point[points.size()];
points.copyInto(array);
body = array;
return(array);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.