source: java/ecj/games/scenarios/RandomizedTwoPlayersGameScenario.java @ 44

Last change on this file since 44 was 44, checked in by mszubert, 14 years ago

cecj, framsticks and games packages imported

File size: 1.1 KB
Line 
1package games.scenarios;
2
3import ec.util.MersenneTwisterFast;
4import games.BoardGame;
5import games.GameMove;
6import games.Player;
7
8import java.util.List;
9
10public class RandomizedTwoPlayersGameScenario implements GameScenario {
11
12        private Player[] players;
13        private double[] prob;
14        private MersenneTwisterFast random;
15       
16        public RandomizedTwoPlayersGameScenario(MersenneTwisterFast random, Player[] players, double[] prob) {
17                this.players = players;
18                this.prob = prob;
19                this.random = random;
20        }
21       
22        public int play(BoardGame game) {
23                while (!game.endOfGame()) {
24                        List<? extends GameMove> moves = game.findMoves();
25                        if (!moves.isEmpty()) {
26                                GameMove bestMove = null;
27                                if (random.nextBoolean(prob[game.getCurrentPlayer()])) {
28                                        bestMove = moves.get(random.nextInt(moves.size()));
29                                } else {
30                                        double bestEval = Double.NEGATIVE_INFINITY;
31                                        for (GameMove move : moves) {
32                                                double eval = game.evalMove(players[game.getCurrentPlayer()], move);
33                                                if (eval > bestEval) {
34                                                        bestEval = eval;
35                                                        bestMove = move;
36                                                }
37                                        }
38                                }
39                                game.makeMove(bestMove);
40                        }
41                        game.switchPlayer();
42                }
43                return game.getOutcome();
44        }
45}
Note: See TracBrowser for help on using the repository browser.