2013-04-12 19:32:25 -07:00
|
|
|
package magic.ai;
|
|
|
|
|
|
|
|
import magic.model.MagicGame;
|
|
|
|
import magic.model.event.MagicEvent;
|
|
|
|
|
2015-06-07 20:18:02 -07:00
|
|
|
public class VegasWorker implements Runnable {
|
2013-04-12 19:32:25 -07:00
|
|
|
|
|
|
|
private static final int MAIN_PHASES=6;
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
private final MagicGame sourceGame;
|
|
|
|
private final VegasScore score;
|
|
|
|
private final Object[] choiceResults;
|
|
|
|
private final long slice;
|
|
|
|
private final boolean CHEAT;
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2015-03-08 05:44:07 -07:00
|
|
|
VegasWorker(final boolean cheat, final MagicGame sourceGame, final VegasScore score,final long slice) {
|
2013-04-12 19:32:25 -07:00
|
|
|
this.CHEAT = cheat;
|
|
|
|
this.sourceGame=sourceGame;
|
|
|
|
this.score=score;
|
|
|
|
this.choiceResults=score.getChoiceResults();
|
|
|
|
this.slice=slice;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Play game until number of main phases are completed or until the game is finished. */
|
|
|
|
private void runGame(final MagicGame game) {
|
2015-02-18 21:12:59 -08:00
|
|
|
while (game.advanceToNextEventWithChoice()) {
|
|
|
|
final MagicEvent event = game.getNextEvent();
|
|
|
|
final Object[] result = event.getSimulationChoiceResult(game);
|
|
|
|
game.executeNextEvent(result);
|
2013-06-23 18:33:35 -07:00
|
|
|
}
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
@Override
|
2015-06-07 20:18:02 -07:00
|
|
|
public void run() {
|
2013-04-12 19:32:25 -07:00
|
|
|
final long endTime = System.nanoTime() + slice;
|
|
|
|
while (System.nanoTime() < endTime) {
|
|
|
|
final MagicGame game = new MagicGame(sourceGame, sourceGame.getScorePlayer());
|
|
|
|
if (!CHEAT) {
|
|
|
|
game.showRandomizedHiddenCards();
|
|
|
|
}
|
|
|
|
game.setMainPhases(MAIN_PHASES);
|
|
|
|
game.executeNextEvent(game.map(choiceResults));
|
|
|
|
runGame(game);
|
|
|
|
score.incrementScore(game.getScore());
|
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
}
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|