コード例 #1
0
  @Test
  public void testAlphaBetaNonDebug() {
    // create the TicTacToe game. Only instantiate the proper class
    // that you want to play.
    StraightLogic logic = new StraightLogic();

    // 2-move lookahead, using the BoardEvaluation function as described in Nilsson.
    Player xPlayer = PlayerFactory.createPlayerWithPly(PlayerFactory.AlphaBeta, Player.XMARK, 2);
    xPlayer.logic(logic);
    xPlayer.score(new BoardEvaluation());

    // 2-move lookahead, for O.
    Player oPlayer = PlayerFactory.createPlayerWithPly(PlayerFactory.AlphaBeta, Player.OMARK, 2);
    oPlayer.logic(logic);
    oPlayer.score(new BoardEvaluation());

    TicTacToeBoard board = new TicTacToeBoard();
    TicTacToeState state = new TicTacToeState(board, logic);

    // pre-initialize board to 2.5 moves
    new PlaceMark(1, 1, (Player) xPlayer).execute(state);
    new PlaceMark(0, 0, (Player) oPlayer).execute(state);
    new PlaceMark(0, 2, (Player) xPlayer).execute(state);

    // two ply lookahead.
    algs.model.gametree.AlphaBetaEvaluation ae = new algs.model.gametree.AlphaBetaEvaluation(2);
    IGameMove move = ae.bestMove(state, oPlayer, xPlayer);
    System.out.println("best move:" + move);

    // we really know that this move is a placemark
    assertEquals(2, ((PlaceMark) move).getColumn());
    assertEquals(0, ((PlaceMark) move).getRow());
  }
コード例 #2
0
  @Test
  public void testAlphaBetaNoMove() {
    // create the TicTacToe game. Only instantiate the proper class
    // that you want to play.
    StraightLogic logic = new StraightLogic();

    // 2-move lookahead, using the BoardEvaluation function as described in Nilsson.
    Player xPlayer = PlayerFactory.createPlayerWithPly(PlayerFactory.AlphaBeta, Player.XMARK, 2);
    xPlayer.logic(logic);
    xPlayer.score(new BoardEvaluation());

    // 2-move lookahead, for O.
    Player oPlayer = PlayerFactory.createPlayerWithPly(PlayerFactory.AlphaBeta, Player.OMARK, 2);
    oPlayer.logic(logic);
    oPlayer.score(new BoardEvaluation());

    TicTacToeBoard board = new TicTacToeBoard();
    TicTacToeState state = new TicTacToeState(board, logic);

    // pre-initialize board to all full
    new PlaceMark(0, 0, (Player) xPlayer).execute(state);
    new PlaceMark(0, 1, (Player) oPlayer).execute(state);
    new PlaceMark(0, 2, (Player) xPlayer).execute(state);
    new PlaceMark(1, 0, (Player) oPlayer).execute(state);
    new PlaceMark(1, 1, (Player) xPlayer).execute(state);
    new PlaceMark(1, 2, (Player) xPlayer).execute(state);
    new PlaceMark(2, 0, (Player) oPlayer).execute(state);
    new PlaceMark(2, 1, (Player) xPlayer).execute(state);
    new PlaceMark(2, 2, (Player) oPlayer).execute(state);

    // two ply lookahead.
    algs.model.gametree.AlphaBetaEvaluation ae = new algs.model.gametree.AlphaBetaEvaluation(2);
    IGameMove move = ae.bestMove(state, oPlayer, xPlayer);
    assertTrue(move == null);
  }
コード例 #3
0
  protected void setup() {
    String gameType = applet.getSelectedGameType();
    Logic logic = applet.getGameLogic(gameType);
    if (logic == null) {
      applet.output("You must select a game type.");
      return;
    }

    controller = new GameController(logic);

    // Find out the X Player
    String xType = applet.getXPlayChoice();

    // Find out the O Player
    String oType = applet.getOPlayChoice();

    computerGoesFirst = false;
    computerPlaysSelf = true;

    boolean setOpponentO = false;
    boolean setOpponentX = false;

    Player p;
    if (oType.equals(TicTacToeApplet.Human)) {
      // Even though this is a human player, it will be involved in
      //
      p = new MousePlayer(Player.OMARK);
      p.score(new BoardEvaluation());

      computerPlaysSelf = false;
      humanPlayer = (MousePlayer) p;
    } else {
      p = PlayerFactory.createPlayer(oType, Player.OMARK);
      if (p == null) {
        int ply = 5; // default
        try {
          ply = Integer.valueOf(applet.getOPly().getText());
        } catch (Exception _e) {

        }
        p = PlayerFactory.createPlayerWithPly(oType, Player.OMARK, ply);
        setOpponentO = true;
      }
    }
    controller.setOPlayer(p);

    if (xType.equals(TicTacToeApplet.Human)) {
      p = new MousePlayer(Player.XMARK);
      p.score(new BoardEvaluation());

      computerPlaysSelf = false;
      humanPlayer = (MousePlayer) p;
    } else {
      computerGoesFirst = true;
      p = PlayerFactory.createPlayer(xType, Player.XMARK);
      if (p == null) {
        int ply = 5; // default
        try {
          ply = Integer.valueOf(applet.getXPly().getText());
        } catch (Exception _e) {

        }
        p = PlayerFactory.createPlayerWithPly(xType, Player.XMARK, ply);
        setOpponentX = true;
      }
    }
    controller.setXPlayer(p);

    // set logic.
    ((Player) controller.getXPlayer()).logic(logic);
    ((Player) controller.getOPlayer()).logic(logic);

    // set opponents.
    if (setOpponentX) {
      ((IntelligentAgent) controller.getXPlayer()).opponent(controller.getOPlayer());
    }
    if (setOpponentO) {
      ((IntelligentAgent) controller.getOPlayer()).opponent(controller.getXPlayer());
    }
  }