Implemented dummy AI
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@318 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
f3f52956f7
commit
15558d7138
5 changed files with 74 additions and 13 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
Reference in a new issue