Esempio n. 1
0
    public <T extends Enum<T> & Turn> CheckersGameBuilder(
        PlayerLookup<CheckersPieceDescription> playerLookup,
        BoardIdService boardIdService,
        Class<T> turnType) {

      if (playerLookup.getAllPlayers().size() != 2) {
        throw new IllegalArgumentException("2 players are required for checkers");
      }
      this.playerLookup = playerLookup;

      // defaults
      checkersGoalService = new CheckersGoalService(playerLookup);
      this.checkersCostFunctionService =
          new CheckersCostFunctionService(playerLookup, checkersGoalService);
      this.useCaching = true;
      this.useAlphaBetaPrunning = true;
      this.depthLimit = DEFAULT_DEPTH_LIMIT;
      this.boardIdService = boardIdService;

      // random starting turn

      EnumSet<T> turns = EnumSet.allOf(turnType);

      startingTurn = Iterators.get(turns.iterator(), (int) (Math.random() * turns.size()));
    }
Esempio n. 2
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;
    }