From 92bea552513a449adf940559fe6b8ff34b6d53d2 Mon Sep 17 00:00:00 2001 From: Bennet Gerlach Date: Sat, 18 Jun 2011 23:06:06 +0200 Subject: Pausing now possible during AI turns git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@469 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/control/turn/AIControl.java | 92 ++++++++++++++-------- .../control/turn/AbstractTurnControl.java | 4 +- 2 files changed, 61 insertions(+), 35 deletions(-) diff --git a/src/jrummikub/control/turn/AIControl.java b/src/jrummikub/control/turn/AIControl.java index 7784d7f..6dac4fe 100644 --- a/src/jrummikub/control/turn/AIControl.java +++ b/src/jrummikub/control/turn/AIControl.java @@ -21,10 +21,13 @@ import jrummikub.util.Pair; */ public class AIControl extends AbstractTurnControl { private TurnLogic logic; - private boolean turnDone = false; boolean useBackgroundThread = true; long startTime; + private boolean isPaused = false; + private boolean turnDone = false; + private boolean readyToEmit = false; + @Override public void startTurn() { timer.startTimer(); @@ -32,10 +35,26 @@ public class AIControl extends AbstractTurnControl { compute(); } + @Override protected void timeOut() { executeTurn(); } + @Override + protected void pauseTurn() { + super.pauseTurn(); + isPaused = true; + } + + @Override + protected void resumeTurn() { + super.resumeTurn(); + isPaused = false; + if (readyToEmit) { + emitEndOfTurn(); + } + } + @Override protected void cleanUp() { if (logic != null) @@ -44,36 +63,13 @@ public class AIControl extends AbstractTurnControl { super.cleanUp(); } - private void emitEndOfTurn() { - turnDone = true; - long turnLength = System.currentTimeMillis() - startTime; - - if (useBackgroundThread) { - Timer timer = new Timer(Math.max(0, - (int) (1000 + Math.random() * 2000 - turnLength)), - new ActionListener() { - @Override - public void actionPerformed(ActionEvent event) { - cleanUp(); - endOfTurnEvent.emit(); - } - }); - timer.setRepeats(false); - timer.start(); - } else { - cleanUp(); - endOfTurnEvent.emit(); - } - - } - private void compute() { switch (turnInfo.getTurnMode()) { case MAY_REDEAL: - emitEndOfTurn(); + endTurn(); break; case INSPECT_ONLY: - emitEndOfTurn(); + endTurn(); break; case NORMAL_TURN: turn(); @@ -157,24 +153,23 @@ public class AIControl extends AbstractTurnControl { for (StoneSet set : result) { turnInfo.getTable().drop( set, - new Position(10 * (Math.random() * 2 - 1), 5 * (Math - .random() * 2 - 1))); + new Position(10 * (Math.random() * 2 - 1), + 5 * (Math.random() * 2 - 1))); for (Stone stone : set) { turnInfo.getHand().pickUp(stone); } } } - emitEndOfTurn(); + endTurn(); } private void doNotMoveExistingSets(List result) { - outerLoop: for (Iterator> it = turnInfo - .getTable().iterator(); it.hasNext();) { + outerLoop: for (Iterator> it = turnInfo.getTable() + .iterator(); it.hasNext();) { Pair pair = it.next(); - setSearch: for (Iterator it2 = result.iterator(); it2 - .hasNext();) { + setSearch: for (Iterator it2 = result.iterator(); it2.hasNext();) { StoneSet set = it2.next(); if (set.getSize() != pair.getFirst().getSize()) { continue; @@ -191,6 +186,37 @@ public class AIControl extends AbstractTurnControl { } } + private void endTurn() { + turnDone = true; + + long turnLength = System.currentTimeMillis() - startTime; + + if (useBackgroundThread) { + Timer timer = new Timer(Math.max(0, + (int) (1000 + Math.random() * 2000 - turnLength)), + new ActionListener() { + @Override + public void actionPerformed(ActionEvent event) { + emitEndOfTurn(); + } + }); + timer.setRepeats(false); + timer.start(); + } else { + emitEndOfTurn(); + } + + } + + private void emitEndOfTurn() { + readyToEmit = true; + if (isPaused) { + return; + } + cleanUp(); + endOfTurnEvent.emit(); + } + /** * Get the factory for the base AI control * diff --git a/src/jrummikub/control/turn/AbstractTurnControl.java b/src/jrummikub/control/turn/AbstractTurnControl.java index e5a7067..d6d0271 100644 --- a/src/jrummikub/control/turn/AbstractTurnControl.java +++ b/src/jrummikub/control/turn/AbstractTurnControl.java @@ -34,12 +34,12 @@ public abstract class AbstractTurnControl implements ITurnControl { return redealEvent; } - private void pauseTurn() { + protected void pauseTurn() { timer.stopTimer(); view.enablePauseMode(true); } - private void resumeTurn() { + protected void resumeTurn() { timer.startTimer(); view.enablePauseMode(false); } -- cgit v1.2.3