From 67d084d48bceed9e8db46877fc4bb17fed8126cd Mon Sep 17 00:00:00 2001 From: Jannis Harder Date: Wed, 8 Jun 2011 15:59:59 +0200 Subject: Moved timer code to AbstractTurnControl git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@383 72836036-5685-4462-b002-a69064685172 --- .../control/turn/AbstractTurnControl.java | 34 +++++++++++++++++++++- src/jrummikub/control/turn/BaseAIControl.java | 31 +++++--------------- src/jrummikub/control/turn/HumanTurnControl.java | 26 ++++------------- src/jrummikub/control/turn/ITurnControl.java | 5 ++++ 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 connections = new ArrayList(); + + @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 connections = new ArrayList(); 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 selectedStones = new ArrayList(); - private List connections = new ArrayList(); - /** * 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()); } 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 -- cgit v1.2.3