Example #1
0
 public static double getValue(BoardGame game, Player player) {
   if (game.endOfGame()) {
     int result;
     if (game.getOutcome() > 0) {
       result = 1;
     } else if (game.getOutcome() < 0) {
       result = -1;
     } else {
       result = 0;
     }
     return result;
   } else {
     return Math.tanh(player.evaluate(game.getBoard()));
   }
 }
Example #2
0
  public static GameMove chooseBestMove(
      BoardGame game, Player player, List<? extends GameMove> moves) {
    double bestEval = Float.NEGATIVE_INFINITY;
    List<GameMove> bestMoves = new ArrayList<GameMove>();

    for (GameMove move : moves) {
      double eval = game.evalMove(player, move);
      if (eval == bestEval) {
        bestMoves.add(move);
      } else if (eval > bestEval) {
        bestEval = eval;
        bestMoves.clear();
        bestMoves.add(move);
      }
    }

    return bestMoves.get((int) (Math.random() * bestMoves.size()));
  }
Example #3
0
 public int play(BoardGame game) {
   while (!game.endOfGame()) {
     List<? extends GameMove> moves = game.findMoves();
     if (!moves.isEmpty()) {
       GameMove bestMove = null;
       if (Math.random() < prob[game.getCurrentPlayer()]) {
         bestMove = moves.get((int) (Math.random() * moves.size()));
       } else {
         bestMove = chooseBestMove(game, players[game.getCurrentPlayer()], moves);
       }
       game.makeMove(bestMove);
     } else {
       game.pass();
     }
   }
   return game.getOutcome();
 }