summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/control')
-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);
}