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