2015-02-17 11:14:50 -08:00
|
|
|
package magic.headless;
|
|
|
|
|
|
|
|
import magic.ai.MagicAI;
|
2020-01-15 12:02:42 -08:00
|
|
|
import magic.model.IGameController;
|
2015-02-17 11:14:50 -08:00
|
|
|
import magic.model.MagicGame;
|
|
|
|
import magic.model.MagicPlayer;
|
|
|
|
import magic.model.event.MagicEvent;
|
|
|
|
|
2015-02-17 20:50:58 -08:00
|
|
|
public class HeadlessGameController implements IGameController {
|
2015-02-17 11:14:50 -08:00
|
|
|
|
2015-02-17 20:54:33 -08:00
|
|
|
private final long maxDuration;
|
2015-02-17 11:14:50 -08:00
|
|
|
private final MagicGame game;
|
2015-02-17 22:12:50 -08:00
|
|
|
private boolean running;
|
2015-12-31 02:54:52 -08:00
|
|
|
|
2015-02-17 11:14:50 -08:00
|
|
|
/** Fully artificial test game. */
|
2015-02-17 20:54:33 -08:00
|
|
|
public HeadlessGameController(final MagicGame aGame, final long duration) {
|
2015-02-17 11:14:50 -08:00
|
|
|
game = aGame;
|
2015-02-17 20:54:33 -08:00
|
|
|
maxDuration = duration;
|
2015-02-17 11:14:50 -08:00
|
|
|
}
|
|
|
|
|
2015-02-17 22:12:50 -08:00
|
|
|
@Override
|
|
|
|
public void haltGame() {
|
|
|
|
running = false;
|
2015-02-17 11:14:50 -08:00
|
|
|
}
|
2015-12-31 02:54:52 -08:00
|
|
|
|
2015-02-17 19:12:32 -08:00
|
|
|
@Override
|
2015-02-17 11:14:50 -08:00
|
|
|
public void runGame() {
|
2015-02-17 22:12:50 -08:00
|
|
|
final long startTime = System.currentTimeMillis();
|
2015-12-31 02:54:52 -08:00
|
|
|
|
2015-02-17 22:12:50 -08:00
|
|
|
running = true;
|
2015-02-18 19:01:22 -08:00
|
|
|
while (running && game.advanceToNextEventWithChoice() && System.currentTimeMillis() - startTime <= maxDuration) {
|
|
|
|
final MagicEvent event = game.getNextEvent();
|
|
|
|
final Object[] result = getAIChoiceResults(event);
|
|
|
|
game.executeNextEvent(result);
|
2015-02-17 22:12:50 -08:00
|
|
|
}
|
2015-12-31 02:54:52 -08:00
|
|
|
|
2015-02-17 22:12:50 -08:00
|
|
|
if (game.isFinished()) {
|
|
|
|
game.advanceDuel();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (System.currentTimeMillis() - startTime > maxDuration) {
|
|
|
|
System.err.println("WARNING. Max time for AI game exceeded");
|
2015-02-17 11:14:50 -08:00
|
|
|
}
|
2015-02-17 19:12:32 -08:00
|
|
|
}
|
2015-02-17 11:14:50 -08:00
|
|
|
|
2015-02-18 00:53:15 -08:00
|
|
|
private Object[] getAIChoiceResults(final MagicEvent event) {
|
2015-02-17 22:12:50 -08:00
|
|
|
//dynamically get the AI based on the player's index
|
|
|
|
final MagicPlayer player = event.getPlayer();
|
2015-04-04 06:45:41 -07:00
|
|
|
final MagicAI ai = player.getAiProfile().getAiType().getAI();
|
2015-02-17 22:12:50 -08:00
|
|
|
return ai.findNextEventChoiceResults(game, player);
|
|
|
|
}
|
2015-02-17 11:14:50 -08:00
|
|
|
}
|