summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBennet Gerlach <bennet_gerlach@web.de>2011-06-18 23:06:06 +0200
committerBennet Gerlach <bennet_gerlach@web.de>2011-06-18 23:06:06 +0200
commit92bea552513a449adf940559fe6b8ff34b6d53d2 (patch)
tree53a0d211b52d7bf75f57341e1489a17284ddf4a5
parent1bddf11ce0d69e789bbb0fca421e00520dcd8559 (diff)
downloadJRummikub-92bea552513a449adf940559fe6b8ff34b6d53d2.tar
JRummikub-92bea552513a449adf940559fe6b8ff34b6d53d2.zip
Pausing now possible during AI turns
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@469 72836036-5685-4462-b002-a69064685172
-rw-r--r--src/jrummikub/control/turn/AIControl.java92
-rw-r--r--src/jrummikub/control/turn/AbstractTurnControl.java4
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,11 +35,27 @@ 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)
logic.abort();
@@ -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<StoneSet> result) {
- outerLoop: for (Iterator<Pair<StoneSet, Position>> it = turnInfo
- .getTable().iterator(); it.hasNext();) {
+ outerLoop: for (Iterator<Pair<StoneSet, Position>> it = turnInfo.getTable()
+ .iterator(); it.hasNext();) {
Pair<StoneSet, Position> pair = it.next();
- setSearch: for (Iterator<StoneSet> it2 = result.iterator(); it2
- .hasNext();) {
+ setSearch: for (Iterator<StoneSet> 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);
}