Пример #1
0
  /** Gets all of the colors of the given row. */
  public synchronized Color[] getRowColors(int rowNum) {
    Color[] rowColors = new Color[GameUtil.BOARD_WIDTH];
    Square[] row = boardRows.get(rowNum);
    if (row != null) {
      assert rowColors.length == row.length;

      for (int i = 0; i < rowColors.length; i++) {
        // null array element means that square is not occupied
        // so set the corresponding color to GRAY (PIECE_COLORS[0])
        if (row[i] == null) {
          rowColors[i] = GameUtil.EMPTY;
        } else {
          rowColors[i] = row[i].color;
        }
      }
    }
    // fill in falling piece squares
    for (int i = 0; i < playerPieces.length; i++) {
      if (playerPieces[i] != null) {
        for (Square square : playerPieces[i].squares) {
          if (square.y == rowNum) {
            rowColors[GameUtil.modulo(square.x, GameUtil.BOARD_WIDTH)] = square.color;
          }
        }
      }
    }
    // fill gaps with gray
    for (int i = 0; i < rowColors.length; i++) {
      if (rowColors[i] == null) {
        rowColors[i] = GameUtil.EMPTY;
      }
    }
    return rowColors;
  }
Пример #2
0
 /**
  * Given a player, check that his/her current falling piece isn't colliding with any of the
  * squares already fixed or are out of the bounds of the board.
  */
 private synchronized boolean checkNoCollisionsWithSetSquares(int player) {
   if (playerPieces[player] == null) {
     return true;
   }
   for (Square square : playerPieces[player].squares) {
     Square[] row = boardRows.get(square.y);
     if (row != null) {
       // check there isn't already another square occupying that space
       if (row[GameUtil.modulo(square.x, GameUtil.BOARD_WIDTH)] != null) {
         return false;
       }
     }
   }
   if (playerPieces[player].hasHitBottom()) {
     return false;
   }
   return true;
 }
Пример #3
0
  public Board(int numPlayers) {
    this.numPlayers = numPlayers;
    this.sectionWidth = GameUtil.computeSectionWidth(this.numPlayers);

    boardRows = new HashMap<Integer, Square[]>();
    playerPieces = new Piece[numPlayers];
    for (int i = 0; i < playerPieces.length; i++) {
      playerPieces[i] = PieceFactory.generateNewPiece(i, sectionWidth);
    }
    playerUpcomingPieces = new ArrayList<Queue<Piece>>(numPlayers);
    for (int i = 0; i < numPlayers; i++) {
      Queue<Piece> nextQueue = new LinkedList<Piece>();
      for (int j = 0; j < PIECE_QUEUE_SIZE; j++) {
        nextQueue.add(PieceFactory.generateNewPiece(i, sectionWidth));
      }
      playerUpcomingPieces.add(nextQueue);
    }
    checkRep();
  }
Пример #4
0
 /**
  * Adds the given player's current falling piece to the squares that are already fixed at the
  * bottom of the board (private utility function; call when appropriate).
  */
 private synchronized void addToSetSquares(int player) {
   if (playerPieces[player] != null) {
     // Adds the given player's current piece into the pieces
     // that are no longer moving (hit the bottom).
     for (Square square : playerPieces[player].squares) {
       Square[] row = boardRows.get(square.y);
       if (row == null) {
         boardRows.put(square.y, new Square[GameUtil.BOARD_WIDTH]);
       }
       boardRows.get(square.y)[GameUtil.modulo(square.x, GameUtil.BOARD_WIDTH)] = square;
     }
     /*
      * Update the current player's falling piece by getting it from the
      * next piece in that player's queue. Then, generate a new piece and
      * add that to the queue.
      */
     playerPieces[player] = playerUpcomingPieces.get(player).remove();
     playerUpcomingPieces.get(player).add(PieceFactory.generateNewPiece(player, sectionWidth));
   }
 }