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.*; /* Name of Lab: Tetris- Piece Class Your Name: Jennette Shepard Your Class: Comp Sci 210 Date: 4/17/08 This class creates the points of the pieces, rotates the pieces, and creates the skirt, which shows the lowest bricks of the piece so that they can be colored yellow by the tester. It also checks to see if pieces are equals and has getters that return the values to the pieceTester. */ /** An immutable representation of a tetris piece in a particular rotation. Each piece is defined by the blocks that make up its body. See the Tetris-Architecture.html for an overview. This is the starter file version -- a few simple things are filled in already @author Nick Parlante @author Eric Chown @author Laura Toma @version November 30, 2007 */ public final class Piece { /* Implementation notes: -The starter code does out a few simple things for you -Store the body as a Point[] array -Do not assume there are 4 points in the body -- use array.length to keep the code general */ // 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; /** Defines a new piece given the Points that make up its body. Makes its own copy of the array and the Point inside it. Does not set up the rotations. This constructor is PRIVATE -- if a client wants a piece object, they must use Piece.getPieces(). getPieces() will therefore make all of the calls to the constructor. As with all constructors, your variables should be initialized here. This means you'll need to calculate width and height as well as setting up the skirt (doing these things once in the constructor means you don't have to do them on the fly during game play). The one exception to this is the "next" variable. You'll want to set that in the pieceRow method. */ //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(); } /** Returns the width of the piece measured in blocks. */ public int getWidth() { //System.out.println("Piece tester wanted the width."); return(width); } /** Returns the height of the piece measured in blocks. */ public int getHeight() { //System.out.println("Piece tester wanted the height."); return(height); } /** Returns a pointer to the piece's body. The caller should not modify this array. */ public Point[] getBody() { System.out.print("GetBody:"); printBody(); return(body); } public void printBody() { System.out.print("Body:"); for(int i= 0; i