Question

public static final int SQUARE = 0,
                        LINE  = 1,
                        S_FIGURE = 2,
                        Z_FIGURE = 3,
                        RIGHT_ANGLE_FIGURE = 4,
                        LEFT_ANGLE_FIGURE = 5,
                        TRIANGLE = 6;

Here above is my Tetris midlet/J2ME code for the block. But i don't know how to explain these parameters. can anyone give me advice? Let me know if you need the full code to work.

this is my full code:

public class Block {
public static final int SQUARE = 0,
                        LINE  = 1,
                        S_FIGURE = 2,
                        Z_FIGURE = 3,
                        RIGHT_ANGLE_FIGURE = 4,
                        LEFT_ANGLE_FIGURE = 5,
                        TRIANGLE = 6;
public int kind;
public int color;
/**
 * The horizontal figure position on the board. This value has no
 * meaning when the figure is not attached to a square board.
 */
public int xPos = 0;

/**
 * The vertical figure position on the board. This value has no
 * meaning when the figure is not attached to a square board.
 */
public int yPos = 0;

/**
 * The figure orientation (or rotation). This value is normally
 * between 0 and 3, but must also be less than the maxOrientation
 * value.
 *
 * @see #maxOrientation
 */
private int orientation = 0;

public int maxOrientation;

public int[] shapeX, shapeY;

private GameCanvas board = null;

public Block(int k) {
    shapeX = new int[4];
    shapeY = new int[4];
    initialize(k);
    this.kind = k;
}

private void initialize(int kind) {
    switch (kind) {
        case SQUARE :
            maxOrientation = 1;
            color = 0x0000ff; // blau
            shapeX[0] = -1;
            shapeY[0] = 0;
            shapeX[1] = 0;
            shapeY[1] = 0;
            shapeX[2] = -1;
            shapeY[2] = 1;
            shapeX[3] = 0;
            shapeY[3] = 1;
            break;
        case LINE :
            maxOrientation = 2;
            color = 0xff0000; // rot
            shapeX[0] = -2;
            shapeY[0] = 0;
            shapeX[1] = -1;
            shapeY[1] = 0;
            shapeX[2] = 0;
            shapeY[2] = 0;
            shapeX[3] = 1;
            shapeY[3] = 0;
            break;
        case S_FIGURE :
            maxOrientation = 2;
            color = 0xff00ff; // lila
            shapeX[0] = 0;
            shapeY[0] = 0;
            shapeX[1] = 1;
            shapeY[1] = 0;
            shapeX[2] = -1;
            shapeY[2] = 1;
            shapeX[3] = 0;
            shapeY[3] = 1;
            break;
        case Z_FIGURE :
            maxOrientation = 2;
            color = 0x008000; // grün
            shapeX[0] = -1;
            shapeY[0] = 0;
            shapeX[1] = 0;
            shapeY[1] = 0;
            shapeX[2] = 0;
            shapeY[2] = 1;
            shapeX[3] = 1;
            shapeY[3] = 1;
            break;
        case RIGHT_ANGLE_FIGURE :
            maxOrientation = 4;
            color = 0x800080; // violett
            shapeX[0] = -1;
            shapeY[0] = 0;
            shapeX[1] = 0;
            shapeY[1] = 0;
            shapeX[2] = 1;
            shapeY[2] = 0;
            shapeX[3] = 1;
            shapeY[3] = 1;
            break;
        case LEFT_ANGLE_FIGURE :
            maxOrientation = 4;
            color = 0x000000; // schwarz
            shapeX[0] = -1;
            shapeY[0] = 0;
            shapeX[1] = 0;
            shapeY[1] = 0;
            shapeX[2] = 1;
            shapeY[2] = 0;
            shapeX[3] = -1;
            shapeY[3] = 1;
            break;
        case TRIANGLE :
            maxOrientation = 4; // gelb
            color = 0xCECE00;
            shapeX[0] = -1;
            shapeY[0] = 0;
            shapeX[1] = 0;
            shapeY[1] = 0;
            shapeX[2] = 1;
            shapeY[2] = 0;
            shapeX[3] = 0;
            shapeY[3] = 1;
            break;
    }
}

/**
 * Checks if this figure is attached to a square board.
 *
 * @return true if the figure is already attached, or
 *         false otherwise
 */
public boolean isAttached() {
    return board != null;
}

public boolean attach(GameCanvas board, boolean center) {
    int  newX;
    int  newY;
    int  i;

    // Check for previous attachment
    if (isAttached()) {
        detach();
    }

    // Reset position (for correct controls)
    xPos = 0;
    yPos = 0;

    // Calculate position
    newX = board.getBoardWidth() / 2;
    if (center) {
        newY = board.getBoardHeight() / 2;
    } else {
        newY = 0;
        for (i = 0; i < shapeX.length; i++) {
            if (getRelativeY(i, orientation) < 0 && getRelativeY(i, orientation)<newY) {
                newY = Math.abs(getRelativeY(i, orientation))+1;
            }
        }

    }

    // Check position
    this.board = board;
    if (!canMoveTo(newX, newY, orientation)) {
        this.board = null;
        return false;
    }

    // Draw figure
    xPos = newX;
    yPos = newY;
    paint(color);
    board.repaint();

    return true;
}

/**
 * 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;
}

/**
 * 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 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(-1);
        xPos--;
        paint(color);
        board.repaint();
    }
}

/**
 * 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(-1);
        xPos++;
        paint(color);
        board.repaint();
    }
}

/**
 * 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(-1);
        yPos++;
        paint(color);
        board.repaint();
    }
}

/**
 * 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(-1);
        yPos = y;
        paint(color);
        board.repaint();
    }
}

/**
 * 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(-1);
        orientation = newOrientation;
        paint(color);
        board.repaint();
    }
}

/**
 * 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
 */
public 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
 */
public 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 -1 for clearing
 */
private void paint(int 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);
    }
}

}

Was it helpful?

Solution

First of all, I think what you're looking for is an enum:

public enum Tetrimonos{
    SQUARE , LINE, S_FIGURE, Z_FIGURE ,
    RIGHT_ANGLE_FIGURE, LEFT_ANGLE_FIGURE , TRIANGLE 
}

Using constants is a bad idea, relic from old languages.

Secondly, what do you mean 'explain these parameters'? Explain the tetris structure of each thing you named? In this case you should go to the relevant literature: a tetris piece is called tetrimono, and each one is called by a letter. Take a look here: http://en.wikipedia.org/wiki/Tetris#Gameplay

You might like to link to this in your documentation as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top