diff options
author | Bennet Gerlach <bennet_gerlach@web.de> | 2011-05-30 19:29:20 +0200 |
---|---|---|
committer | Bennet Gerlach <bennet_gerlach@web.de> | 2011-05-30 19:29:20 +0200 |
commit | ad2f22636921ac5afe60f48bda08274eb00e079a (patch) | |
tree | 46da5ca6890fd4eb628320d0658d98ccb0deb76f | |
parent | ae9ce59d36eed2b133977d37ab5e73a844ef334c (diff) | |
download | JRummikub-ad2f22636921ac5afe60f48bda08274eb00e079a.tar JRummikub-ad2f22636921ac5afe60f48bda08274eb00e079a.zip |
Implemented timer in base AI class
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@326 72836036-5685-4462-b002-a69064685172
-rw-r--r-- | src/jrummikub/control/turn/BaseAIControl.java | 63 |
1 files changed, 54 insertions, 9 deletions
diff --git a/src/jrummikub/control/turn/BaseAIControl.java b/src/jrummikub/control/turn/BaseAIControl.java index 4e627c3..fc1b97d 100644 --- a/src/jrummikub/control/turn/BaseAIControl.java +++ b/src/jrummikub/control/turn/BaseAIControl.java @@ -1,23 +1,60 @@ package jrummikub.control.turn; +import java.util.ArrayList; +import java.util.List; + import javax.swing.SwingUtilities; +import jrummikub.control.ITurnTimer; +import jrummikub.control.TurnTimer; +import jrummikub.util.Connection; +import jrummikub.util.IListener; +/** + * Base class for AI players + * + */ public class BaseAIControl extends AbstractTurnControl { long startTime; + private ITurnTimer turnTimer; + private List<Connection> connections = new ArrayList<Connection>(); + private Thread computeThread; + + private volatile boolean stopRunning = false; + @Override public void startTurn() { - Thread thread = new Thread(new Runnable() { + 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() { compute(); } }); startTime = System.currentTimeMillis(); - thread.start(); - + + turnTimer.startTimer(); + computeThread.start(); } - + + private void cleanUp() { + turnTimer.stopTimer(); + + for (Connection c : connections) { + c.remove(); + } + stopRunning = true; + } + private void compute() { if (mayRedeal) { emitRedeal(); @@ -29,15 +66,16 @@ public class BaseAIControl extends AbstractTurnControl { } } } - + private void layOut() { emitEndOfTurn(); } - + private void emitRedeal() { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { + cleanUp(); redealEvent.emit(); } }); @@ -45,9 +83,10 @@ public class BaseAIControl extends AbstractTurnControl { private void emitEndOfTurn() { long timeElapsed = System.currentTimeMillis() - startTime; - long timeNeeded = Math.min((long)(1000 + Math.random() * hand.getSize() * 100), 50000); + long timeNeeded = Math.min((long) (1000 + Math.random() * hand.getSize() + * 100), 50000); long waitTime = timeNeeded - timeElapsed; - + if (waitTime > 0) { try { Thread.sleep(waitTime); @@ -55,15 +94,21 @@ public class BaseAIControl extends AbstractTurnControl { // This shouldn't happen } } - + SwingUtilities.invokeLater(new Runnable() { @Override public void run() { + cleanUp(); endOfTurnEvent.emit(); } }); } + /** + * Get the factory for the base AI control + * + * @return the factory + */ static public TurnControlFactory getFactory() { return new TurnControlFactory() { @Override |