📄 board.java
字号:
/* Author: <insert your name here> ugrad.cs.ubc.ca address: <insert your ugrad.cs.ubc.ca e-mail address here> Date: <insert date here> By submitting this file, I acknowledge that the person whose name appears above is the *only* author of the code required to solve this assignment.*/package cs111.tetris.data;/** * 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. * <p> * The place() method is provided to add the blocks of a piece into the * board. Once the blocks are in the board, they are not connected to each other * as a piece any more; they are just 4 adjacent blocks that will eventually * get separated by row-clearing. * * This doesn't actually implement game playing logic (i.e. moving pieces left, * right, down or dropping them in a correct way. It rather provides useful * methods to implement this functionality. The game playing logic itself * is implemented in the {@link Game} class. * <p> * <b>The Undo Feature:</b> * <p> * The Board provides a 1-deep <code>undo()</code> facility that allows a Board-user * to undo the most recent changes to the board state. * <p> * Here's how the undo facility works... * <ul> * <li> We'll designate the state of the board as "committed" -- a * state that can be returned to. * <li> From a committed state, the client can perform operations * <code>place()</code> and/or <code>clearRows()</code>. These * operations modify the board state. * <li> An <code>undo()</code> will remove the changes so the board is back at * the committed state. * <li> Calling <code>commit()</code> instead of undo will make the changes * permanent. In other words, the current state becomes the new committed state * and previous committed state can no longer be reached via * <code>undo()</code>. * </ul> * <p> * The <code>undo()</code> facility is useful for implementing * AI's. An AI can use it to simulate playing a piece "hypothecally", * evaluate the resulting board state and then undo the "hypothetical" move to * try another one. This way an AI can try many different moves before chosing * the best one to actually play. (See the cs111.tetris.brain package). * <p> * The undo facility can also be used to animate a piece moving in the board * like this: place() piece with y=15, pause, undo(), place() with y=14, pause, * undo(), place() with y=13, ... etc. * * @author Kris De Volder (Modified from the original code by Nick Parlante). */public class Board { private boolean[][] grid; //TODO: You may need additional fields? You can decide as you go // along implementing everything what you need. /** * Creates an empty board of the given width and height measured in blocks. * The board is initially empty. This empty board state is committed. */ public Board(int aWidth, int aHeight) { //TODO: implement this correctly } /** * Returns the width of the board in blocks. */ public int getWidth() { return 0; //TODO: implement this correctly } /** * Returns the height of the board in blocks. */ public int getHeight() { return 0; //TODO: implement this correctly } /** * Returns the max column height present in the board. For an empty board * this is 0. */ public int getMaxHeight() { return 0; //TODO: implement this correctly } /** * Given a piece and an x, returns the y value where the piece would come to * rest if it were dropped straight down (with its lower left corner) at * that x. * * More precisly: the x represents the lower left corner of the piece (i.e. the * (0,0) point in terms of the piece's own coordinate system). * * @return the y value (the coordinate on the board not the grid) of the bottom left * corner of the piece when the piece were to be dropped at x. */ public int dropHeight(Piece piece, int x) { //TODO: implement this correctly /* Implementation: use the skirt and the col heights to compute this */ return 0; } /** * Returns the height of the given column -- i.e. the y value of the highest * block + 1. The height is 0 if the column contains no blocks. */ public int getColumnHeight(int x) { return 0; //TODO: implement this correctly } /** * Returns the number of filled blocks in the given row. */ public int getRowWidth(int y) { return 0; //TODO: implement this correctly } /** * Returns true if the given block is filled in the board. Blocks outside of * the valid width/height area always return true. */ public final boolean getGrid(int x, int y) { return false; //TODO: implement this correctly } 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. * * <p> * 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) { return 0; //TODO: implement this correctly } /** * Deletes rows that are filled all the way across, moving things above * down. Returns true if any row clearing happened. */ public boolean clearRows() { /* 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. A less than ideal solution will also be accepted but * make sure it actually works! */ return false; //TODO: implement this correctly } /** * Revert the board to its previous committed state, undoing any state changes that * may have happened since the last time commit() was called. */ public void undo() { //TODO: implement this correctly //Notes: //The easiest implementation of undo (and commit). Uses a backup //datastructure. Undo copies the backed-up contents back into the //"live" data structure. } /** * Makes the current board state the committed state. All changes to * the board done upto now become permanent and can no longer be * undone. */ public void commit() { //TODO: implement this correctly //Notes: //The easiest implementation of undo (and commit). Uses a backup //datastructure. //A commit copies the current state in the "live" data structure //into the backup data structure. (So that undo can copy it back //later if needed) } /** * Create String representation for the board. This is useful for debuggin * purposes (to print out a board state on System.out for example). */ public String toString() { String brd = ""; for (int y = getHeight() - 1; y >= 0; y--) { for (int x = 0; x < getWidth(); x++) { brd += grid[x][y] ? "#" : "."; } brd += "\n"; } return brd; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -