import java.awt.*; import java.util.*; /** * * Name of Lab: Tetris II - board class Your Name: Jennette Shepard Your Class: Comp Sci 210 Date 4/28/08 //YES I FOLLOWED THE HONOR CODE. Represents a Tetris board -- essentially a 2-d grid of booleans. Supports tetris pieces and row clearning. Has an "undo" feature that allows clients to add and remove pieces efficiently. Does not do any drawing or have any idea of pixels. Intead, just represents the abtsract 2-d board. @author Nick Parlante @version 1.0, Mar 1, 2001 */ public final class Board { //grid width, height private int width; private int height; //grid private boolean[][] grid; //top item in column private int maxHeight; //displayed widths and heights of the board private int widths[]; private int heights[]; //if bGrid structures have been //moved to permanent grid private boolean committed = true; // backup data structures private boolean[][] bGrid; private int[] bWidths; private int[] bHeights; private int bMaxHeight; //if sanity check should run private boolean DEBUG = true; private static final int HEIGHT = 2; private static final int INIT_HIGH_VAL = 10; /** Creates an empty board of the given width and height measured in blocks. This is not quite done, you still need to initialize your backup data structures (and erase this comment!) */ public Board(int aWidth, int aHeight) { width = aWidth; height = aHeight + HEIGHT; //initializes grid, heights, widths grid = new boolean[width][height]; widths = new int[height]; heights = new int[width]; //if heights are zero to start, //then nothing will display maxHeight = INIT_HIGH_VAL; bMaxHeight = INIT_HIGH_VAL; //initially the grid matches bgrid committed = true; //sets heights for tallest object in a column for (int i = 0; i < width; i++) { heights[i] = 0; for (int j = 0; j < height; j++) { grid[i][j] = false; } } //sets widths for how many objects are in a row for (int i = 0; i < height; i++) { widths[i] = 0; } //initializes bGrid bGrid = new boolean[width][height]; for (int i = 0; i < width; i++) { heights[i] = 0; for (int j = 0; j < height; j++) { bGrid[i][j] = false; } } //equalizes backup widths and heights //to those of the actual widths and heights bWidths = widths; bHeights = heights; bMaxHeight = maxHeight; } /** Returns the width of the board in blocks. */ public int getWidth() { return width; } /** Returns the height of the board in blocks. */ public int getHeight() { return height; } /** Returns the max column height present in the board. For an empty board this is 0. */ public int getMaxHeight() { return maxHeight; } /** Checks the board for internal consistency -- used for debugging. */ public void sanityCheck() { if (DEBUG) { // consistency check the board state //prints grids so you can check them //against the actual displayed grid System.out.println("Grid = "); for(int i = getHeight()-1; i>0; i--){ for(int j = 0; j= 0; i--){ if(grid[x][i] == true){ return i+1; } } return 0; } /** Returns the number of filled blocks in the given row. */ public int getRowWidth(int y) { int count = 0; for(int i = 0; i = 0 && x <= getWidth()){ if(y>= 0 && y <= getHeight()){ if(grid[x][y] == true || bGrid[x][y] == true){ return true; } } } return false; } public static final int PLACE_OK = 0; public static final int PLACE_ROW_FILLED = 1; public static final int PLACE_OUT_BOUNDS = 2; public static final int PLACE_BAD = 3; /** Attempts to add the body of a piece to the board. Copies the piece blocks into the board grid. Returns PLACE_OK for a regular placement, or PLACE_ROW_FILLED for a regular placement that causes at least one row to be filled.

Error cases: If part of the piece would fall out of bounds, the placement does not change the board at all, and PLACE_OUT_BOUNDS is returned. If the placement is "bad" --interfering with existing blocks in the grid -- then the placement is halted partially complete and PLACE_BAD is returned. An undo() will remove the bad placement. */ public int place(Piece piece, int x, int y) { //a new piece is being dealt with //so board should be committed commit(); //checks for out of bounds if(x<0 || (x+piece.getWidth()) > getWidth()+2){ System.err.println("Out of Bounds"); return PLACE_OUT_BOUNDS; } //skirt, body, count variables int[] skirt = piece.getSkirt(); Point[] body = piece.getBody(); int count = 0; //checks if brick can land ok for(int i = 0; i< piece.getWidth()-1; i++){ if(!grid[x+ skirt[i]][y+i] == true){ count++; }else{ System.err.println("Interferes with other bricks"); return PLACE_BAD; } } //if the brick can land, sets piece, and //checks to see if that fills the row if(count == piece.getWidth()-1){ for(int i = 0; i< 4; i++){ int xOff = (int)body[i].getX(); int yOff = (int)body[i].getY(); bGrid[x+xOff][y+yOff] = true; } //new piece added to backup grid, so not committed committed=false; //prints the grids sanityCheck(); //checks if row has been filled for(int p = 0; p< piece.getHeight(); p++){ if(getRowWidth(x+p) == getWidth()){ return PLACE_ROW_FILLED; } } return PLACE_OK; } System.err.println("Bad Placement"); return PLACE_BAD; } /** Deletes rows that are filled all the way across, moving things above down. Returns true if any row clearing happened.

Implementation: This is complicated. Ideally, you want to copy each row down to its correct location in one pass. Note that more than one row may be filled. */ public boolean clearRows() { commit(); for(int i = 0; i < getHeight(); i++){ if(getRowWidth(i) == getWidth()){ int y = i; //clears full row for(int j =0; i< getWidth(); i ++){ grid[i][j] = false; } //shifts down for(int p=i; p< (getHeight()-HEIGHT-1); p++){ for(int k=0; k< getWidth(); k++){ if(grid[k][p+1] == true || bGrid[k][p+1] == true){ bGrid[k][p] = true; bGrid[k][p+1] = false; } } } //sets top empty row to empty for(int m=0; m< getWidth(); m++){ grid[m][getHeight()-HEIGHT] = false; } return true; } } sanityCheck(); return false; } /** If a place() happens, optionally followed by a clearRows(), a subsequent undo() reverts the board to its state before the place(). If the conditions for undo() are not met, such as calling undo() twice in a row, then the second undo() does nothing. See the overview docs. */ public void undo() { for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { bGrid[i][j] = false; } } committed = true; } /** Puts the board in the committed state. See the overview docs. */ public void commit() {//NOT DONE for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if(bGrid[i][j] == true){ grid[i][j] = true; bGrid[i][j] = false; } } } committed = true; } }