Exemplo n.º 1
0
    private CheckersBoard createStartingState(
        PlayerLookup<CheckersPieceDescription> playerLookup,
        PieceLookup<CheckersPieceDescription> pieceLookup) {

      CheckersBoard startingState =
          new CheckersBoard(boardIdService.nextId(), startingTurn, pieceLookup);

      Player<CheckersPieceDescription> firstPlayer = playerLookup.getPlayer(startingTurn.getId());
      int player1PlacedPiece = 1;
      int player2PlacedPiece = 0;
      for (CheckersPieceDescription piece : pieceLookup.getAllPieces()) {
        Position position;
        if (piece.getType() == Type.STANDARD) {
          if (piece.getPlayer().equals(firstPlayer)) {
            int xCoord = (int) (player1PlacedPiece % startingState.getBoardExtremity().getXCoord());
            int yCoord = player1PlacedPiece / startingState.getBoardExtremity().getXCoord();
            if (yCoord == 1) {
              xCoord--;
            }
            position = new Position(xCoord, yCoord);

            player1PlacedPiece += 2;
          } else {
            int xCoord =
                (int) (player2PlacedPiece % (startingState.getBoardExtremity().getXCoord()));
            int yCoord =
                (startingState.getBoardExtremity().getYCoord() - 1)
                    - (player2PlacedPiece / (startingState.getBoardExtremity().getXCoord()));
            if (yCoord == 6) {
              xCoord++;
            }
            position = new Position(xCoord, yCoord);
            player2PlacedPiece += 2;
          }

          System.out.println(
              "adding position at: "
                  + position
                  + " for player "
                  + piece.getPlayer().getPlayerId()
                  + " player1PlacedPiece="
                  + player1PlacedPiece
                  + " player2PlacedPiece="
                  + player2PlacedPiece);
          CheckersPiece positionedPiece = new CheckersPiece(position, piece);

          startingState.addPiece(positionedPiece);
        }
      }

      return startingState;
    }
Exemplo n.º 2
0
  private CheckersPieceDescription upgradePieceIfRequired(
      CheckersPieceDescription piece, Position newPosition, CheckersBoard board) {
    if (piece.getType() == Type.STANDARD) {
      // no need to check if this is a move forward or back ward as this is a new move so the
      // direction is handled in the calling code
      if (newPosition.getYCoord() == board.getBoardExtremity().getYCoord() - 1
          || newPosition.getYCoord() == 0) {
        // upgrading to king as it is on the y border

        return pieceLookup.getKingForPiece(piece);
      }
    }
    return piece;
  }