summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/jrummikub/control/turn/AbstractTurnControl.java34
-rw-r--r--src/jrummikub/control/turn/BaseAIControl.java31
-rw-r--r--src/jrummikub/control/turn/HumanTurnControl.java26
-rw-r--r--src/jrummikub/control/turn/ITurnControl.java5
4 files changed, 51 insertions, 45 deletions
diff --git a/src/jrummikub/control/turn/AbstractTurnControl.java b/src/jrummikub/control/turn/AbstractTurnControl.java
index 554db61..ae1e155 100644
--- a/src/jrummikub/control/turn/AbstractTurnControl.java
+++ b/src/jrummikub/control/turn/AbstractTurnControl.java
@@ -1,8 +1,15 @@
package jrummikub.control.turn;
+import java.util.ArrayList;
+import java.util.List;
+
+import jrummikub.control.ITurnTimer;
+import jrummikub.control.TurnTimer;
import jrummikub.model.GameSettings;
+import jrummikub.util.Connection;
import jrummikub.util.Event;
import jrummikub.util.IEvent;
+import jrummikub.util.IListener;
import jrummikub.view.IView;
/**
@@ -15,7 +22,10 @@ public abstract class AbstractTurnControl implements ITurnControl {
protected TurnInfo turnInfo;
protected GameSettings settings;
protected IView view;
-
+ protected ITurnTimer timer;
+ protected List<Connection> connections = new ArrayList<Connection>();
+
+
@Override
public IEvent getEndOfTurnEvent() {
return endOfTurnEvent;
@@ -25,12 +35,34 @@ public abstract class AbstractTurnControl implements ITurnControl {
public IEvent getRedealEvent() {
return redealEvent;
}
+
+ protected abstract void timeOut();
@Override
public void setup(TurnInfo info, GameSettings settings, IView view) {
turnInfo = info;
this.settings = settings;
this.view = view;
+ if (timer == null) {
+ timer = new TurnTimer(view);
+ }
+ connections.add(timer.getTimeRunOutEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ timeOut();
+ }
+ }));
+ }
+
+ protected void cleanUp() {
+ timer.stopTimer();
+ for (Connection c : connections) {
+ c.remove();
+ }
+ }
+
+ public void abortTurn() {
+ cleanUp();
}
} \ No newline at end of file
diff --git a/src/jrummikub/control/turn/BaseAIControl.java b/src/jrummikub/control/turn/BaseAIControl.java
index dabdc81..43ade18 100644
--- a/src/jrummikub/control/turn/BaseAIControl.java
+++ b/src/jrummikub/control/turn/BaseAIControl.java
@@ -7,14 +7,10 @@ import java.util.TreeMap;
import javax.swing.SwingUtilities;
import jrummikub.control.AIUtil;
-import jrummikub.control.ITurnTimer;
-import jrummikub.control.TurnTimer;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
import jrummikub.model.StoneSet;
-import jrummikub.util.Connection;
-import jrummikub.util.IListener;
import jrummikub.util.Pair;
/**
@@ -23,24 +19,12 @@ import jrummikub.util.Pair;
*/
public class BaseAIControl extends AbstractTurnControl {
private long startTime;
- private ITurnTimer turnTimer;
- private List<Connection> connections = new ArrayList<Connection>();
private Thread computeThread;
private volatile boolean stopRunning = false;
@Override
public void startTurn() {
- turnTimer = new TurnTimer(view);
- connections.add(turnTimer.getTimeRunOutEvent().add(new IListener() {
-
- @Override
- public void handle() {
- cleanUp();
- endOfTurnEvent.emit();
- }
- }));
-
computeThread = new Thread(new Runnable() {
@Override
public void run() {
@@ -49,16 +33,17 @@ public class BaseAIControl extends AbstractTurnControl {
});
startTime = System.currentTimeMillis();
- turnTimer.startTimer();
+ timer.startTimer();
computeThread.start();
}
+
+ protected void timeOut() {
+ cleanUp();
+ endOfTurnEvent.emit();
+ }
- private void cleanUp() {
- turnTimer.stopTimer();
-
- for (Connection c : connections) {
- c.remove();
- }
+ protected void cleanUp() {
+ super.cleanUp();
stopRunning = true;
}
diff --git a/src/jrummikub/control/turn/HumanTurnControl.java b/src/jrummikub/control/turn/HumanTurnControl.java
index 3bedc1a..02541d2 100644
--- a/src/jrummikub/control/turn/HumanTurnControl.java
+++ b/src/jrummikub/control/turn/HumanTurnControl.java
@@ -7,13 +7,11 @@ import java.util.LinkedList;
import java.util.List;
import jrummikub.control.ITurnTimer;
-import jrummikub.control.TurnTimer;
import jrummikub.model.Hand;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
import jrummikub.model.StoneSet;
-import jrummikub.util.Connection;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.util.IListener2;
@@ -24,12 +22,8 @@ import jrummikub.view.IView.BottomPanelType;
* Controller for a single turn made by a human player
*/
public class HumanTurnControl extends AbstractTurnControl {
- private ITurnTimer timer;
-
private List<Stone> selectedStones = new ArrayList<Stone>();
- private List<Connection> connections = new ArrayList<Connection>();
-
/**
* Create a new human player's turn control
*/
@@ -54,20 +48,13 @@ public class HumanTurnControl extends AbstractTurnControl {
HumanTurnControl(ITurnTimer testTimer) {
this.timer = testTimer;
}
+
+ protected void timeOut() {
+ endOfTurn(false);
+ }
@Override
public void startTurn() {
- if (this.timer == null) {
- this.timer = new TurnTimer(view);
- }
-
- connections.add(timer.getTimeRunOutEvent().add(new IListener() {
-
- @Override
- public void handle() {
- endOfTurn(false);
- }
- }));
addButtonHandlers();
addHandPanelHandlers();
@@ -453,15 +440,12 @@ public class HumanTurnControl extends AbstractTurnControl {
}
private void endOfTurn(boolean redeal) {
- timer.stopTimer();
+ cleanUp();
if (redeal) {
redealEvent.emit();
} else {
endOfTurnEvent.emit();
}
- for (Connection c : connections) {
- c.remove();
- }
view.setSelectedStones(new ArrayList<Stone>());
}
diff --git a/src/jrummikub/control/turn/ITurnControl.java b/src/jrummikub/control/turn/ITurnControl.java
index b8a39ce..5a3d550 100644
--- a/src/jrummikub/control/turn/ITurnControl.java
+++ b/src/jrummikub/control/turn/ITurnControl.java
@@ -108,4 +108,9 @@ public interface ITurnControl {
return turnMode;
}
}
+
+ /**
+ * Abort the turn
+ */
+ public void abortTurn();
} \ No newline at end of file