Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package edu.uab.cs; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; import java.util.Random; /** * This component draws tetrominos. */ public class TetrisComponent extends JComponent { /** *

image text in transcribed

package edu.uab.cs;

import java.awt.Graphics;

import java.awt.Graphics2D;

import javax.swing.JComponent;

import java.util.Random;

/**

* This component draws tetrominos.

*/

public class TetrisComponent extends JComponent

{

/**

* Method to draw all tetromino shapes on the screen.

*

* @param g a graphics object

*/

public void paintComponent(Graphics g)

{

// tetromino must be set before we can paint it.

assert tetromino != null;

Graphics2D g2 = (Graphics2D) g;

tetromino.draw(g2);

}

/**

* Sets the current tetromino.

*

* @param piece a valid tetromino

*/

public void setPiece(Tetromino piece)

{

tetromino = piece;

}

/** The component's tetromino. */

private Tetromino tetromino;

}

package edu.uab.cs;

import java.util.Random;

import javax.swing.JFrame;

/** A simple tetromino viewer. */

public class TetrisViewer

{

/** size of frame. */

private static final int FRAMESZ = Tetromino.BLOCK * (Tetromino.COUNT+1);

/** maximum number of rotations. */

private static final int MAXROTATE = 4;

/**

* The main method.

*

* @param args command line arguments; currently ignored.

*/

public static void main(String[] args)

{

// create random tetromino

Random rand = new Random();

Tetromino piece = new Tetromino(rand.nextInt(Tetromino.COUNT));

// set tetromino's position randomly

piece.setX(rand.nextInt(FRAMESZ - Tetromino.BLOCK));

piece.setY(rand.nextInt(FRAMESZ - Tetromino.BLOCK));

// rotate tetromino a random number of times

piece.rotateCCW90(rand.nextInt(MAXROTATE));

// create component and set tetromino

TetrisComponent field = new TetrisComponent();

field.setPiece(piece);

// create and setup frame

JFrame frame = new JFrame();

frame.setSize(FRAMESZ, FRAMESZ);

frame.setTitle("Tetromino");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(field);

frame.setVisible(true);

}

}

package edu.uab.cs;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.Color;

/** Models an immutable tetromino. */

public class Tetromino

{

/** Number of squares. */

static public final int SQUARES = 4;

/** Size of a tetromino square. */

static public final int SIZE = 20;

/** Size of the whole tetromino. */

static public final int BLOCK = SQUARES * SIZE;

/** Standard I sprite. */

static public final int I = 0;

/** Standard J sprite. */

static public final int J = 1;

/** Standard L sprite. */

static public final int L = 2;

/** Standard S sprite. */

static public final int S = 3;

/** Standard Z sprite. */

static public final int Z = 4;

/** Standard O sprite. */

static public final int O = 5;

/** Standard T sprite. */

static public final int T = 6;

/** number of sprites. */

static public final int COUNT = T+1;

/* Private constructor, because nobody should ever use an empty Tetromino! */

private Tetromino() {}

/**

* Constructor to create a new tetromino.

*

* @param kind indicates the sprite to be constructed

*/

public Tetromino(int kind)

{

// validate the input

assert kind >= I && kind

// set default position

x = y = 0;

// set shape and color according to kind

switch (kind)

{

case I:

color = Color.CYAN;

sprite = new int[][] { {0, 0, 0, 0},

{0, 0, 0, 0},

{1, 1, 1, 1},

{0, 0, 0, 0}

};

break;

case J:

color = Color.ORANGE;

sprite = new int[][] { {0, 0, 0, 0},

{1, 0, 0, 0},

{1, 1, 1, 0},

{0, 0, 0, 0}

};

break;

case L:

color = Color.BLUE;

sprite = new int[][] { {0, 0, 0, 0},

{0, 0, 1, 0},

{1, 1, 1, 0},

{0, 0, 0, 0}

};

break;

case S:

color = Color.GREEN;

sprite = new int[][] { {0, 0, 1, 0},

{0, 1, 1, 0},

{0, 1, 0, 0},

{0, 0, 0, 0}

};

break;

case Z:

color = Color.RED;

sprite = new int[][] { {0, 1, 0, 0},

{0, 1, 1, 0},

{0, 0, 1, 0},

{0, 0, 0, 0}

};

break;

case O:

color = Color.YELLOW;

sprite = new int[][] { {0, 0, 0, 0},

{0, 1, 1, 0},

{0, 1, 1, 0},

{0, 0, 0, 0}

};

break;

case T:

color = Color.MAGENTA;

sprite = new int[][] { {0, 0, 0, 0},

{0, 1, 0, 0},

{1, 1, 1, 0},

{0, 0, 0, 0}

};

break;

default:

System.out.println("unexpected Tetromino");

assert false;

}

// validate the object. after the constructor has run,

// the tetromino must be in a valid state and ready for use.

// color must be a valid object

assert color != null;

// validate that sprite is a SQUARESxSQUARES array.

assert sprite != null && sprite.length == SQUARES && sprite[0].length == SQUARES;

}

/**

* Computes the original x coordinate of a rotated square at (x,y).

*

* @param x x-coordinate of the destination

* @param y y-coordinate of the destination

* @return an integer indicating the x coordinate of the source

*/

private static int rotateX(int x, int y)

{

return 3-y;

}

/**

* Computes the original y coordinate of a rotated square at (x,y).

*

* @param x x-coordinate of the destination

* @param y y-coordinate of the destination

* @return an integer indicating the y coordinate of the source

*/

private static int rotateY(int x, int y)

{

return x;

}

/**

* Rotates this tetromino counterclockwise by 90 degrees.

*/

public void rotateCCW90()

{

int[][] rotated = new int[SQUARES][SQUARES];

for (int i = 0; i

for (int j = 0; j

rotated[i][j] = sprite[rotateX(i,j)][rotateY(i,j)];

sprite = rotated;

}

/**

* Convenience method to rotate a tetromino n number of times.

*

* @param n number of tetromino rotations

*

* @todo make code more efficient, by computing transformation once

* instead of n steps.

*/

public void rotateCCW90(int n)

{

assert n >= 0 && n

if (n == 0) return; // no rotation needed

rotateCCW90(); // rotate once

rotateCCW90(n-1); // rotate n-1 times

}

/**

* Draws this tetromino at the given x and y coordinate.

*

* @param x x coordinate of the base point

* @param y y coordinate of the base point

* @param g2 graphics object

*/

public void draw(Graphics2D g2)

{

for (int i = 0; i

for (int j = 0; j

if (sprite[i][j] != 0)

{

Rectangle filled = new Rectangle(x+i*SIZE+1, y+j*SIZE+1, SIZE-1, SIZE-1);

Rectangle border = new Rectangle(x+i*SIZE, y+j*SIZE, SIZE, SIZE);

g2.setColor(color);

g2.fill(filled);

g2.setColor(Color.BLACK);

g2.draw(border);

}

}

/**

* Sets x coordinate o tetromino.

*

* @param x new x-coordinate

*/

public void setX(int x) { this.x = x; }

/**

* Sets y coordinate o tetromino.

*

* @param y new y-coordinate

*/

public void setY(int y) { this.y = y; }

/** A matrix representing the tetromino's sprite. */

private int[][] sprite;

/** x coordinate. */

private int x;

/** y coordinate. */

private int y;

/** The tetromino's color. */

private Color color;

}

Assignment 6 Tetris Game Note: You can work on this assignment individually or in a team of two Objectives: Graphics, interfaces, anonymous classes, event handling due on October 28 Tetris is a game where players need to match falling tetrominos in a 10x20 grid.1. A player can navigate the falling tetromino with four keys. Arrows left and right, to move a tetromino left or right, arrow up to rotate a tetromino, and arrow down (this is optional) to let the teromino fall in its current orientation and column. Implement a tetris game based on Java graphics and event handling. You can either choose to implement a complete game, or a short version. If the problem statement is unclear, use the Canvas forum to ask for clarifications. Turn in a zip file named blazerid hw6.zip. The file should contain an exported Eclipse project? with the following items. . All files needed to compile and run your solution . Your tests . A document (or text file) that describes your design decisions, your tests, any difficulties you had. you would like to get a graded version on paper, add a "paper copy requested" If you received help from somebody else in class, please give credit to those students. If note at the top of the report saying . If you worked with a partner, a short statement explaining who worked on which parts of the assignnent Grading (50 pts max) (10pts) Lab attendanee (Week of Oct 22 e (10pts) Assignment report (see paragraph above) . (10pts) Turned in classes compile . (10pts) Code quality (e.g., class design, use of anonymous or inner classes and/or lambd functions), and documentation quality (e.g. javadoc) (10pts) A tetromino can be rotated and moved until it hits the ground. A falling tetromino can be moved to the border of the grid but not beyond. Rotations and moves that are invalid should be ignored. . (10pts) Short version: the game ends after the first tetromino has reached the bottom. (10pts) Full version: when the tetromino hits the bottom, its blocks are integrated into the grid and a new enter the grid structure tetromino enters the game. The game continues until no more tetromino can Assignment 6 Tetris Game Note: You can work on this assignment individually or in a team of two Objectives: Graphics, interfaces, anonymous classes, event handling due on October 28 Tetris is a game where players need to match falling tetrominos in a 10x20 grid.1. A player can navigate the falling tetromino with four keys. Arrows left and right, to move a tetromino left or right, arrow up to rotate a tetromino, and arrow down (this is optional) to let the teromino fall in its current orientation and column. Implement a tetris game based on Java graphics and event handling. You can either choose to implement a complete game, or a short version. If the problem statement is unclear, use the Canvas forum to ask for clarifications. Turn in a zip file named blazerid hw6.zip. The file should contain an exported Eclipse project? with the following items. . All files needed to compile and run your solution . Your tests . A document (or text file) that describes your design decisions, your tests, any difficulties you had. you would like to get a graded version on paper, add a "paper copy requested" If you received help from somebody else in class, please give credit to those students. If note at the top of the report saying . If you worked with a partner, a short statement explaining who worked on which parts of the assignnent Grading (50 pts max) (10pts) Lab attendanee (Week of Oct 22 e (10pts) Assignment report (see paragraph above) . (10pts) Turned in classes compile . (10pts) Code quality (e.g., class design, use of anonymous or inner classes and/or lambd functions), and documentation quality (e.g. javadoc) (10pts) A tetromino can be rotated and moved until it hits the ground. A falling tetromino can be moved to the border of the grid but not beyond. Rotations and moves that are invalid should be ignored. . (10pts) Short version: the game ends after the first tetromino has reached the bottom. (10pts) Full version: when the tetromino hits the bottom, its blocks are integrated into the grid and a new enter the grid structure tetromino enters the game. The game continues until no more tetromino can

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

More Books

Students also viewed these Databases questions