Last change
on this file since 102 was
44,
checked in by mszubert, 15 years ago
|
cecj, framsticks and games packages imported
|
File size:
1.1 KB
|
Rev | Line | |
---|
[44] | 1 | package games.scenarios; |
---|
| 2 | |
---|
| 3 | import ec.util.MersenneTwisterFast; |
---|
| 4 | import games.BoardGame; |
---|
| 5 | import games.GameMove; |
---|
| 6 | import games.Player; |
---|
| 7 | |
---|
| 8 | import java.util.List; |
---|
| 9 | |
---|
| 10 | public 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.