📄 figure.java
字号:
this.board = board; if (!canMoveTo(newX, newY, orientation)) { this.board = null; return false; } // Draw figure xPos = newX; yPos = newY; paint(color); board.update(); return true; } /** * Detaches this figure from its square board. The figure will not * be removed from the board by this operation, resulting in the * figure being left intact. */ public void detach() { board = null; } /** * Checks if the figure is fully visible on the square board. If * the figure isn't attached to a board, false will be returned. * * @return true if the figure is fully visible, or * false otherwise */ public boolean isAllVisible() { if (!isAttached()) { return false; } for (int i = 0; i < shapeX.length; i++) { if (yPos + getRelativeY(i, orientation) < 0) { return false; } } return true; } /** * Checks if the figure has landed. If this method returns true, * the moveDown() or the moveAllWayDown() methods should have no * effect. If no square board is attached, this method will return * true. * * @return true if the figure has landed, or false otherwise */ public boolean hasLanded() { return !isAttached() || !canMoveTo(xPos, yPos + 1, orientation); } /** * Moves the figure one step to the left. If such a move is not * possible with respect to the square board, nothing is done. The * square board will be changed as the figure moves, clearing the * previous cells. If no square board is attached, nothing is * done. */ public void moveLeft() { if (isAttached() && canMoveTo(xPos - 1, yPos, orientation)) { paint(null); xPos--; paint(color); board.update(); } } /** * Moves the figure one step to the right. If such a move is not * possible with respect to the square board, nothing is done. The * square board will be changed as the figure moves, clearing the * previous cells. If no square board is attached, nothing is * done. */ public void moveRight() { if (isAttached() && canMoveTo(xPos + 1, yPos, orientation)) { paint(null); xPos++; paint(color); board.update(); } } /** * Moves the figure one step down. If such a move is not possible * with respect to the square board, nothing is done. The square * board will be changed as the figure moves, clearing the * previous cells. If no square board is attached, nothing is * done. */ public void moveDown() { if (isAttached() && canMoveTo(xPos, yPos + 1, orientation)) { paint(null); yPos++; paint(color); board.update(); } } /** * Moves the figure all the way down. The limits of the move are * either the square board bottom, or squares not being empty. If * no move is possible with respect to the square board, nothing * is done. The square board will be changed as the figure moves, * clearing the previous cells. If no square board is attached, * nothing is done. */ public void moveAllWayDown() { int y = yPos; // Check for board if (!isAttached()) { return; } // Find lowest position while (canMoveTo(xPos, y + 1, orientation)) { y++; } // Update if (y != yPos) { paint(null); yPos = y; paint(color); board.update(); } } /** * Returns the current figure rotation (orientation). * * @return the current figure rotation */ public int getRotation() { return orientation; } /** * Sets the figure rotation (orientation). If the desired rotation * is not possible with respect to the square board, nothing is * done. The square board will be changed as the figure moves, * clearing the previous cells. If no square board is attached, * the rotation is performed directly. * * @param rotation the new figure orientation */ public void setRotation(int rotation) { int newOrientation; // Set new orientation newOrientation = rotation % maxOrientation; // Check new position if (!isAttached()) { orientation = newOrientation; } else if (canMoveTo(xPos, yPos, newOrientation)) { paint(null); orientation = newOrientation; paint(color); board.update(); } } /** * Rotates the figure randomly. If such a rotation is not * possible with respect to the square board, nothing is done. * The square board will be changed as the figure moves, * clearing the previous cells. If no square board is attached, * the rotation is performed directly. */ public void rotateRandom() { setRotation((int) (Math.random() * 4.0) % maxOrientation); } /** * Rotates the figure clockwise. If such a rotation is not * possible with respect to the square board, nothing is done. * The square board will be changed as the figure moves, * clearing the previous cells. If no square board is attached, * the rotation is performed directly. */ public void rotateClockwise() { if (maxOrientation == 1) { return; } else { setRotation((orientation + 1) % maxOrientation); } } /** * Rotates the figure counter-clockwise. If such a rotation * is not possible with respect to the square board, nothing * is done. The square board will be changed as the figure * moves, clearing the previous cells. If no square board is * attached, the rotation is performed directly. */ public void rotateCounterClockwise() { if (maxOrientation == 1) { return; } else { setRotation((orientation + 3) % 4); } } /** * Checks if a specified pair of (square) coordinates are inside * the figure, or not. * * @param x the horizontal position * @param y the vertical position * * @return true if the coordinates are inside the figure, or * false otherwise */ private boolean isInside(int x, int y) { for (int i = 0; i < shapeX.length; i++) { if (x == xPos + getRelativeX(i, orientation) && y == yPos + getRelativeY(i, orientation)) { return true; } } return false; } /** * Checks if the figure can move to a new position. The current * figure position is taken into account when checking for * collisions. If a collision is detected, this method will return * false. * * @param newX the new horizontal position * @param newY the new vertical position * @param newOrientation the new orientation (rotation) * * @return true if the figure can be moved, or * false otherwise */ private boolean canMoveTo(int newX, int newY, int newOrientation) { int x; int y; for (int i = 0; i < 4; i++) { x = newX + getRelativeX(i, newOrientation); y = newY + getRelativeY(i, newOrientation); if (!isInside(x, y) && !board.isSquareEmpty(x, y)) { return false; } } return true; } /** * Returns the relative horizontal position of a specified square. * The square will be rotated according to the specified * orientation. * * @param square the square to rotate (0-3) * @param orientation the orientation to use (0-3) * * @return the rotated relative horizontal position */ private int getRelativeX(int square, int orientation) { switch (orientation % 4) { case 0 : return shapeX[square]; case 1 : return -shapeY[square]; case 2 : return -shapeX[square]; case 3 : return shapeY[square]; default: return 0; // Should never occur } } /** * Rotates the relative vertical position of a specified square. * The square will be rotated according to the specified * orientation. * * @param square the square to rotate (0-3) * @param orientation the orientation to use (0-3) * * @return the rotated relative vertical position */ private int getRelativeY(int square, int orientation) { switch (orientation % 4) { case 0 : return shapeY[square]; case 1 : return shapeX[square]; case 2 : return -shapeY[square]; case 3 : return -shapeX[square]; default: return 0; // Should never occur } } /** * Paints the figure on the board with the specified color. * * @param color the color to paint with, or null for clearing */ private void paint(Color color) { int x, y; for (int i = 0; i < shapeX.length; i++) { x = xPos + getRelativeX(i, orientation); y = yPos + getRelativeY(i, orientation); board.setSquareColor(x, y, color); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -