diff options
Diffstat (limited to 'src/jrummikub')
-rw-r--r-- | src/jrummikub/control/RoundControl.java | 2 | ||||
-rw-r--r-- | src/jrummikub/control/turn/AbstractTurnControl.java | 7 | ||||
-rw-r--r-- | src/jrummikub/control/turn/BaseAIControl.java | 59 | ||||
-rw-r--r-- | src/jrummikub/control/turn/ITurnControl.java | 5 |
4 files changed, 65 insertions, 8 deletions
diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java index bcef40e..5f46fd5 100644 --- a/src/jrummikub/control/RoundControl.java +++ b/src/jrummikub/control/RoundControl.java @@ -97,7 +97,7 @@ public class RoundControl { ITurnControl turnControl = TurnControlFactory.getFactory(roundState.getActivePlayer() .getPlayerSettings().getTurnControlType()).create(); - turnControl.setup(roundState.getActivePlayer().getHand(), clonedTable, + turnControl.setup(roundState.getActivePlayer(), clonedTable, view, inspectOnly, mayRedeal); turnControl.getEndOfTurnEvent().add(new IListener() { @Override diff --git a/src/jrummikub/control/turn/AbstractTurnControl.java b/src/jrummikub/control/turn/AbstractTurnControl.java index aeb6241..e493718 100644 --- a/src/jrummikub/control/turn/AbstractTurnControl.java +++ b/src/jrummikub/control/turn/AbstractTurnControl.java @@ -1,6 +1,7 @@ package jrummikub.control.turn; import jrummikub.model.IHand; +import jrummikub.model.IPlayer; import jrummikub.model.ITable; import jrummikub.util.Event; import jrummikub.util.IEvent; @@ -13,6 +14,7 @@ public abstract class AbstractTurnControl implements ITurnControl { protected Event endOfTurnEvent = new Event(); protected Event redealEvent = new Event(); + protected IPlayer player; protected IHand hand; protected ITable table; protected IView view; @@ -31,9 +33,10 @@ public abstract class AbstractTurnControl implements ITurnControl { } @Override - public void setup(IHand hand, ITable table, IView view, + public void setup(IPlayer player, ITable table, IView view, boolean inspectOnly, boolean mayRedeal) { - this.hand = hand; + this.player = player; + this.hand = player.getHand(); this.table = table; this.view = view; this.inspectOnly = inspectOnly; diff --git a/src/jrummikub/control/turn/BaseAIControl.java b/src/jrummikub/control/turn/BaseAIControl.java index 98dceb3..2ed4c6e 100644 --- a/src/jrummikub/control/turn/BaseAIControl.java +++ b/src/jrummikub/control/turn/BaseAIControl.java @@ -1,21 +1,74 @@ package jrummikub.control.turn; +import java.util.Date; + +import javax.swing.SwingUtilities; + import jrummikub.model.IHand; import jrummikub.model.ITable; import jrummikub.view.IView; public class BaseAIControl extends AbstractTurnControl { - + long startTime; @Override public void startTurn() { + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + compute(); + } + }); + startTime = System.currentTimeMillis(); + thread.start(); + + } + + private void compute() { if (mayRedeal) { - redealEvent.emit(); + emitRedeal(); } else { - endOfTurnEvent.emit(); + if (player.getLaidOut()) { + layOut(); + } else { + emitEndOfTurn(); + } } } + private void layOut() { + emitEndOfTurn(); + } + + private void emitRedeal() { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + redealEvent.emit(); + } + }); + } + + private void emitEndOfTurn() { + long timeElapsed = System.currentTimeMillis() - startTime; + long waitTime = 2000 - timeElapsed; + + if (waitTime > 0) { + try { + Thread.sleep(waitTime); + } catch (InterruptedException e) { + // This shouldn't happen + } + } + + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + endOfTurnEvent.emit(); + } + }); + } + static public TurnControlFactory getFactory() { return new TurnControlFactory() { @Override diff --git a/src/jrummikub/control/turn/ITurnControl.java b/src/jrummikub/control/turn/ITurnControl.java index 80e2b27..061cddd 100644 --- a/src/jrummikub/control/turn/ITurnControl.java +++ b/src/jrummikub/control/turn/ITurnControl.java @@ -1,6 +1,7 @@ package jrummikub.control.turn; import jrummikub.model.IHand; +import jrummikub.model.IPlayer; import jrummikub.model.ITable; import jrummikub.util.Event; import jrummikub.util.IEvent; @@ -21,8 +22,8 @@ public interface ITurnControl { * @param mayRedeal * true when the current player may decide to redeal */ - public abstract void setup(IHand hand, ITable table, IView view, - boolean inspectOnly, boolean mayRedeal); + public void setup(IPlayer player, ITable table, IView view, + boolean inspectOnly, boolean mayRedeal); /** * Get the event that is emitted when the turn is over |