Ejemplo n.º 1
0
  /** Updates the matrix with current board and current pentomino */
  public void updateMatrix() {
    int[][] tmpBoard = new int[gameBoard.getBoard().length][gameBoard.getBoard()[0].length];
    // Copy the old board to the tmp
    for (int i = 0; i < gameBoard.getBoard().length; i++) {
      System.arraycopy(gameBoard.getBoard()[i], 0, tmpBoard[i], 0, gameBoard.getBoard()[i].length);
    }
    // Add the pentomino to the tmpBoard
    for (Point p : activePentomino.getLocation()) {
      int newX = (int) (pentominoLocation.getX() + p.getX());
      int newY = (int) (pentominoLocation.getY() + p.getY());
      if (newY >= 0) tmpBoard[newX][newY] = activePentomino.getID();
    }

    matrix = tmpBoard;
  }
Ejemplo n.º 2
0
 /**
  * Checks if the falling pentomino can fall further.
  *
  * @param pentomino The pentomino piece which is currently falling.
  * @param board The board in which the pentomino currently is.
  */
 public boolean nextDropLegal(Pentomino pentomino, int[][] board, Point location) {
   boolean legalMove = true;
   // Self-explanatory. Just checks the next block to 'fall' to for all blocks of the pentomino
   for (Point p : pentomino.getLocation()) {
     int newX = (int) (p.getX() + location.getX());
     int newY = (int) (p.getY() + location.getY() + 1);
     if (newY >= 0 && (newY >= board[0].length || board[newX][newY] != -1)) legalMove = false;
   }
   return legalMove;
 }
Ejemplo n.º 3
0
  /**
   * Handles the events for keypresses
   *
   * @param e the pressed key
   */
  public void keyPressed(KeyEvent e) {
    if (activePentomino != null) {
      // Keys for moveing the pentomino sideways
      if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT) {
        // Initialize direction to 0 to make sure we don't get null pointer exception
        // or do something weird with the movement
        int direction = 0;
        if (e.getKeyCode() == KeyEvent.VK_LEFT) direction = -1;
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) direction = 1;
        // +1 right, -1 left

        // Check if we are allowed to actually move the pentomino to the new position
        boolean legalMove = true;
        for (Point p : activePentomino.getLocation()) {
          int newX = (int) (p.getX() + pentominoLocation.getX() + direction);
          int newY = (int) (p.getY() + pentominoLocation.getY());

          // Check only if the pentomino is not above the board
          if (newY >= 0) {
            // Check that we are horizontally within the board and not trying to overlap another
            // piece
            if (newX < 0
                || newX >= gameBoard.getBoard().length
                || gameBoard.getBoard()[newX][newY] != -1) legalMove = false;
          }
        }
        // Check passed so we do the actual movement of the pentomino
        if (legalMove) {
          pentominoLocation.setLocation(
              (pentominoLocation.getX() + direction), pentominoLocation.getY());
          predictDrop();
          updateMatrix();
        }
      }

      // Key for rotating the pentomino
      if (e.getKeyCode() == KeyEvent.VK_UP) {
        // Create a temporary pentomino to check if the rotated position free
        Pentomino tmpPentomino = activePentomino.copy();
        tmpPentomino.rotate();
        boolean legalMove = true;
        // For reach block of the position check that it's free and within the grid
        // ignoring the check if we're above the grid
        for (Point p : tmpPentomino.getLocation()) {
          int newX = (int) (p.getX() + pentominoLocation.getX());
          int newY = (int) (p.getY() + pentominoLocation.getY());
          if (newY > 0) {
            if (newY >= gameBoard.getBoard()[0].length
                || newX < 0
                || newX >= gameBoard.getBoard().length
                || gameBoard.getBoard()[newX][newY] != -1) {
              legalMove = false;
            }
          }
        }
        // If the check passed then simply do the rotation
        if (legalMove) {
          activePentomino.rotate();
          predictDrop();
        }
        // Update the matrix so we don't have lag in display
        updateMatrix();
      }

      // Key for speeding up the fall
      if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        // If next position is available, put the current pentomino to it
        if (nextDropLegal(activePentomino, gameBoard.getBoard(), pentominoLocation)) {
          pentominoLocation.setLocation(pentominoLocation.getX(), pentominoLocation.getY() + 1);
        }
        // Update to avoid lag
        updateMatrix();
      }

      // Key for entirely dropping the pentomino
      if (e.getKeyCode() == KeyEvent.VK_ENTER) {
        // We make the pentomino fall as many times as it can
        while (nextDropLegal(activePentomino, gameBoard.getBoard(), pentominoLocation)) {
          pentominoLocation.setLocation(pentominoLocation.getX(), pentominoLocation.getY() + 1);
        }
        // Update to avoid lag
        updateMatrix();
      }

      // Key for flipping the pentomino
      if (e.getKeyCode() == KeyEvent.VK_SPACE) {
        // Create a temporary pentomino for checking the flipped position
        Pentomino tmpPentomino = activePentomino.copy();
        tmpPentomino.reflect();
        // Check that each position in the temporary pentomino is available and within the matrix
        boolean legalMove = true;
        for (Point p : tmpPentomino.getLocation()) {
          int newX = (int) (p.getX() + pentominoLocation.getX());
          int newY = (int) (p.getY() + pentominoLocation.getY());
          if (newY < 0
              || newY >= gameBoard.getBoard()[0].length
              || newX < 0
              || newX >= gameBoard.getBoard().length
              || gameBoard.getBoard()[newX][newY] != -1) {
            legalMove = false;
          }
        }
        // Flip the pentomino if check is passed
        if (legalMove) {
          activePentomino.reflect();
          predictDrop();
        }
        // Update to avoid lag
        updateMatrix();
      }

      // Key for storing the currently falling pentomino
      if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
        // If we haven't stored after fixing the pentomino to the grid
        if (storageable) {
          // Change the active and stored pentomino and set the location to the top of the screen
          pentominoLocation = new Point(gameBoard.getWidth() / 2, 0);
          Pentomino tmp = activePentomino;
          activePentomino = storagedPentomino;
          storagedPentomino = tmp;
          storageable = false;
          // Get new prediction
          predictDrop();
          // Update to avoid lag
          updateMatrix();
        }
      }

      // Key for a chect for testing purposes
      if (e.getKeyCode() == KeyEvent.VK_X) {
        // If pressed enough, add score
        cheat++;
        if (cheat > 10) {
          cheat = 0;
          addScore(12);
        }
      }
    }
  }