summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/jrummikub/control/RoundControl.java2
-rw-r--r--src/jrummikub/control/turn/AbstractTurnControl.java7
-rw-r--r--src/jrummikub/control/turn/BaseAIControl.java59
-rw-r--r--src/jrummikub/control/turn/ITurnControl.java5
-rw-r--r--test/jrummikub/control/turn/TurnControlTest.java14
5 files changed, 74 insertions, 13 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
diff --git a/test/jrummikub/control/turn/TurnControlTest.java b/test/jrummikub/control/turn/TurnControlTest.java
index 9140323..11c4c2b 100644
--- a/test/jrummikub/control/turn/TurnControlTest.java
+++ b/test/jrummikub/control/turn/TurnControlTest.java
@@ -21,6 +21,7 @@ import jrummikub.control.turn.HumanTurnControl;
import jrummikub.model.IHand;
import jrummikub.model.ITable;
import jrummikub.model.MockHand;
+import jrummikub.model.MockPlayer;
import jrummikub.model.MockTable;
import jrummikub.model.Position;
import jrummikub.model.Stone;
@@ -68,6 +69,7 @@ public class TurnControlTest {
}
HumanTurnControl testControl;
+ MockPlayer mockPlayer;
MockView mockView;
MockTimer mockTimer;
MockTable mockTable;
@@ -105,8 +107,10 @@ public class TurnControlTest {
mockTimer = new MockTimer();
mockTable = new MockTable();
mockHand = new MockHand();
+ mockPlayer = new MockPlayer(null, null);
+ mockPlayer.hand = mockHand;
testControl = new HumanTurnControl(mockTimer);
- testControl.setup(mockHand, mockTable, mockView, false, false);
+ testControl.setup(mockPlayer, mockTable, mockView, false, false);
}
/** */
@@ -131,7 +135,7 @@ public class TurnControlTest {
mockHand.iterable = stones;
testControl = new HumanTurnControl(mockTimer);
- testControl.setup(mockHand, mockTable, mockView, false, false);
+ testControl.setup(mockPlayer, mockTable, mockView, false, false);
testControl.startTurn();
int i = 0;
@@ -627,7 +631,7 @@ public class TurnControlTest {
public void testAddLeft() {
AccessibleTable table = new AccessibleTable();
HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
- turnControl.setup(mockHand, table, mockView, false, false);
+ turnControl.setup(mockPlayer, table, mockView, false, false);
turnControl.startTurn();
Stone blueOne = new Stone(1, BLUE);
Stone redOne = new Stone(1, RED);
@@ -744,7 +748,7 @@ public class TurnControlTest {
public void testAddRight() {
AccessibleTable table = new AccessibleTable();
HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
- turnControl.setup(mockHand, table, mockView, false, false);
+ turnControl.setup(mockPlayer, table, mockView, false, false);
turnControl.startTurn();
Stone blueOne = new Stone(1, BLUE);
Stone redOne = new Stone(1, RED);
@@ -861,7 +865,7 @@ public class TurnControlTest {
public void testAddNewSet() {
AccessibleTable table = new AccessibleTable();
HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
- turnControl.setup(mockHand, table, mockView, false, false);
+ turnControl.setup(mockPlayer, table, mockView, false, false);
turnControl.startTurn();
Stone blueOne = new Stone(1, BLUE);
Stone redOne = new Stone(1, RED);