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()
|
ITurnControl turnControl = TurnControlFactory.getFactory(roundState.getActivePlayer()
|
||||||
.getPlayerSettings().getTurnControlType()).create();
|
.getPlayerSettings().getTurnControlType()).create();
|
||||||
turnControl.setup(roundState.getActivePlayer().getHand(), clonedTable,
|
turnControl.setup(roundState.getActivePlayer(), clonedTable,
|
||||||
view, inspectOnly, mayRedeal);
|
view, inspectOnly, mayRedeal);
|
||||||
turnControl.getEndOfTurnEvent().add(new IListener() {
|
turnControl.getEndOfTurnEvent().add(new IListener() {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package jrummikub.control.turn;
|
package jrummikub.control.turn;
|
||||||
|
|
||||||
import jrummikub.model.IHand;
|
import jrummikub.model.IHand;
|
||||||
|
import jrummikub.model.IPlayer;
|
||||||
import jrummikub.model.ITable;
|
import jrummikub.model.ITable;
|
||||||
import jrummikub.util.Event;
|
import jrummikub.util.Event;
|
||||||
import jrummikub.util.IEvent;
|
import jrummikub.util.IEvent;
|
||||||
|
@ -13,6 +14,7 @@ public abstract class AbstractTurnControl implements ITurnControl {
|
||||||
|
|
||||||
protected Event endOfTurnEvent = new Event();
|
protected Event endOfTurnEvent = new Event();
|
||||||
protected Event redealEvent = new Event();
|
protected Event redealEvent = new Event();
|
||||||
|
protected IPlayer player;
|
||||||
protected IHand hand;
|
protected IHand hand;
|
||||||
protected ITable table;
|
protected ITable table;
|
||||||
protected IView view;
|
protected IView view;
|
||||||
|
@ -31,9 +33,10 @@ public abstract class AbstractTurnControl implements ITurnControl {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setup(IHand hand, ITable table, IView view,
|
public void setup(IPlayer player, ITable table, IView view,
|
||||||
boolean inspectOnly, boolean mayRedeal) {
|
boolean inspectOnly, boolean mayRedeal) {
|
||||||
this.hand = hand;
|
this.player = player;
|
||||||
|
this.hand = player.getHand();
|
||||||
this.table = table;
|
this.table = table;
|
||||||
this.view = view;
|
this.view = view;
|
||||||
this.inspectOnly = inspectOnly;
|
this.inspectOnly = inspectOnly;
|
||||||
|
|
|
@ -1,19 +1,72 @@
|
||||||
package jrummikub.control.turn;
|
package jrummikub.control.turn;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
import jrummikub.model.IHand;
|
import jrummikub.model.IHand;
|
||||||
import jrummikub.model.ITable;
|
import jrummikub.model.ITable;
|
||||||
import jrummikub.view.IView;
|
import jrummikub.view.IView;
|
||||||
|
|
||||||
|
|
||||||
public class BaseAIControl extends AbstractTurnControl {
|
public class BaseAIControl extends AbstractTurnControl {
|
||||||
|
long startTime;
|
||||||
@Override
|
@Override
|
||||||
public void startTurn() {
|
public void startTurn() {
|
||||||
|
Thread thread = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
compute();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
startTime = System.currentTimeMillis();
|
||||||
|
thread.start();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void compute() {
|
||||||
if (mayRedeal) {
|
if (mayRedeal) {
|
||||||
redealEvent.emit();
|
emitRedeal();
|
||||||
} else {
|
} else {
|
||||||
|
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();
|
endOfTurnEvent.emit();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static public TurnControlFactory getFactory() {
|
static public TurnControlFactory getFactory() {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package jrummikub.control.turn;
|
package jrummikub.control.turn;
|
||||||
|
|
||||||
import jrummikub.model.IHand;
|
import jrummikub.model.IHand;
|
||||||
|
import jrummikub.model.IPlayer;
|
||||||
import jrummikub.model.ITable;
|
import jrummikub.model.ITable;
|
||||||
import jrummikub.util.Event;
|
import jrummikub.util.Event;
|
||||||
import jrummikub.util.IEvent;
|
import jrummikub.util.IEvent;
|
||||||
|
@ -21,7 +22,7 @@ public interface ITurnControl {
|
||||||
* @param mayRedeal
|
* @param mayRedeal
|
||||||
* true when the current player may decide to redeal
|
* true when the current player may decide to redeal
|
||||||
*/
|
*/
|
||||||
public abstract void setup(IHand hand, ITable table, IView view,
|
public void setup(IPlayer player, ITable table, IView view,
|
||||||
boolean inspectOnly, boolean mayRedeal);
|
boolean inspectOnly, boolean mayRedeal);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -21,6 +21,7 @@ import jrummikub.control.turn.HumanTurnControl;
|
||||||
import jrummikub.model.IHand;
|
import jrummikub.model.IHand;
|
||||||
import jrummikub.model.ITable;
|
import jrummikub.model.ITable;
|
||||||
import jrummikub.model.MockHand;
|
import jrummikub.model.MockHand;
|
||||||
|
import jrummikub.model.MockPlayer;
|
||||||
import jrummikub.model.MockTable;
|
import jrummikub.model.MockTable;
|
||||||
import jrummikub.model.Position;
|
import jrummikub.model.Position;
|
||||||
import jrummikub.model.Stone;
|
import jrummikub.model.Stone;
|
||||||
|
@ -68,6 +69,7 @@ public class TurnControlTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
HumanTurnControl testControl;
|
HumanTurnControl testControl;
|
||||||
|
MockPlayer mockPlayer;
|
||||||
MockView mockView;
|
MockView mockView;
|
||||||
MockTimer mockTimer;
|
MockTimer mockTimer;
|
||||||
MockTable mockTable;
|
MockTable mockTable;
|
||||||
|
@ -105,8 +107,10 @@ public class TurnControlTest {
|
||||||
mockTimer = new MockTimer();
|
mockTimer = new MockTimer();
|
||||||
mockTable = new MockTable();
|
mockTable = new MockTable();
|
||||||
mockHand = new MockHand();
|
mockHand = new MockHand();
|
||||||
|
mockPlayer = new MockPlayer(null, null);
|
||||||
|
mockPlayer.hand = mockHand;
|
||||||
testControl = new HumanTurnControl(mockTimer);
|
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;
|
mockHand.iterable = stones;
|
||||||
|
|
||||||
testControl = new HumanTurnControl(mockTimer);
|
testControl = new HumanTurnControl(mockTimer);
|
||||||
testControl.setup(mockHand, mockTable, mockView, false, false);
|
testControl.setup(mockPlayer, mockTable, mockView, false, false);
|
||||||
testControl.startTurn();
|
testControl.startTurn();
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
@ -627,7 +631,7 @@ public class TurnControlTest {
|
||||||
public void testAddLeft() {
|
public void testAddLeft() {
|
||||||
AccessibleTable table = new AccessibleTable();
|
AccessibleTable table = new AccessibleTable();
|
||||||
HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
|
HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
|
||||||
turnControl.setup(mockHand, table, mockView, false, false);
|
turnControl.setup(mockPlayer, table, mockView, false, false);
|
||||||
turnControl.startTurn();
|
turnControl.startTurn();
|
||||||
Stone blueOne = new Stone(1, BLUE);
|
Stone blueOne = new Stone(1, BLUE);
|
||||||
Stone redOne = new Stone(1, RED);
|
Stone redOne = new Stone(1, RED);
|
||||||
|
@ -744,7 +748,7 @@ public class TurnControlTest {
|
||||||
public void testAddRight() {
|
public void testAddRight() {
|
||||||
AccessibleTable table = new AccessibleTable();
|
AccessibleTable table = new AccessibleTable();
|
||||||
HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
|
HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
|
||||||
turnControl.setup(mockHand, table, mockView, false, false);
|
turnControl.setup(mockPlayer, table, mockView, false, false);
|
||||||
turnControl.startTurn();
|
turnControl.startTurn();
|
||||||
Stone blueOne = new Stone(1, BLUE);
|
Stone blueOne = new Stone(1, BLUE);
|
||||||
Stone redOne = new Stone(1, RED);
|
Stone redOne = new Stone(1, RED);
|
||||||
|
@ -861,7 +865,7 @@ public class TurnControlTest {
|
||||||
public void testAddNewSet() {
|
public void testAddNewSet() {
|
||||||
AccessibleTable table = new AccessibleTable();
|
AccessibleTable table = new AccessibleTable();
|
||||||
HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
|
HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
|
||||||
turnControl.setup(mockHand, table, mockView, false, false);
|
turnControl.setup(mockPlayer, table, mockView, false, false);
|
||||||
turnControl.startTurn();
|
turnControl.startTurn();
|
||||||
Stone blueOne = new Stone(1, BLUE);
|
Stone blueOne = new Stone(1, BLUE);
|
||||||
Stone redOne = new Stone(1, RED);
|
Stone redOne = new Stone(1, RED);
|
||||||
|
|
Reference in a new issue