Created dummy base AI control

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@311 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Jannis Harder 2011-05-29 20:22:47 +02:00
parent f6a3409ed5
commit 65d08ea450
7 changed files with 123 additions and 82 deletions

View file

@ -95,14 +95,11 @@ public class RoundControl {
view.getPlayerPanel().setEndTurnMode(inspectOnly, mayRedeal); view.getPlayerPanel().setEndTurnMode(inspectOnly, mayRedeal);
ITurnControl turnControl = roundState ITurnControl turnControl = roundState.getActivePlayer()
.getActivePlayer() .getPlayerSettings().getTurnControlFactory().create();
.getPlayerSettings() turnControl.setup(roundState.getActivePlayer().getHand(), clonedTable,
.getTurnControlFactory() view, inspectOnly, mayRedeal);
.create(roundState.getActivePlayer().getHand(), clonedTable,
view, inspectOnly);
turnControl.getEndOfTurnEvent().add(new IListener() { turnControl.getEndOfTurnEvent().add(new IListener() {
@Override @Override
public void handle() { public void handle() {
endOfTurn(); endOfTurn();
@ -115,7 +112,6 @@ public class RoundControl {
redeal(); redeal();
} }
}); });
turnControl.startTurn(); turnControl.startTurn();
} }

View file

@ -1,7 +1,10 @@
package jrummikub.control.turn; package jrummikub.control.turn;
import jrummikub.model.IHand;
import jrummikub.model.ITable;
import jrummikub.util.Event; import jrummikub.util.Event;
import jrummikub.util.IEvent; import jrummikub.util.IEvent;
import jrummikub.view.IView;
/** /**
* Abstract base class for TurnControls * Abstract base class for TurnControls
@ -10,9 +13,12 @@ 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 IHand hand;
@Override protected ITable table;
public abstract void startTurn(); protected IView view;
protected boolean inspectOnly;
protected boolean mayRedeal;
@Override @Override
public IEvent getEndOfTurnEvent() { public IEvent getEndOfTurnEvent() {
@ -23,5 +29,15 @@ public abstract class AbstractTurnControl implements ITurnControl {
public Event getRedealEvent() { public Event getRedealEvent() {
return redealEvent; return redealEvent;
} }
@Override
public void setup(IHand hand, ITable table, IView view,
boolean inspectOnly, boolean mayRedeal) {
this.hand = hand;
this.table = table;
this.view = view;
this.inspectOnly = inspectOnly;
this.mayRedeal = mayRedeal;
}
} }

View file

@ -0,0 +1,28 @@
package jrummikub.control.turn;
import jrummikub.model.IHand;
import jrummikub.model.ITable;
import jrummikub.view.IView;
public class BaseAIControl extends AbstractTurnControl {
@Override
public void startTurn() {
if (mayRedeal) {
redealEvent.emit();
} else {
endOfTurnEvent.emit();
}
}
static public TurnControlFactory getFactory() {
return new TurnControlFactory() {
@Override
public ITurnControl create() {
return new BaseAIControl();
}
};
}
}

View file

@ -26,37 +26,16 @@ import jrummikub.view.IView;
* Controller for a single turn made by a human player * Controller for a single turn made by a human player
*/ */
public class HumanTurnControl extends AbstractTurnControl { public class HumanTurnControl extends AbstractTurnControl {
private IHand hand;
private ITable table;
private ITurnTimer timer; private ITurnTimer timer;
private IView view;
private List<Stone> selectedStones = new ArrayList<Stone>(); private List<Stone> selectedStones = new ArrayList<Stone>();
private List<Connection> connections = new ArrayList<Connection>(); private List<Connection> connections = new ArrayList<Connection>();
private boolean inspectOnly;
/** /**
* Create a new TurnControl using a given hand (of the active player), a * Create a new human player's turn control
* given table and a given view for user interaction.
*
* @param hand
* active player's hand
* @param table
* current table
* @param view
* view for user interaction.
* @param inspectOnly
* the current turn doesn't allow any table manipulation
*/ */
public HumanTurnControl(IHand hand, ITable table, IView view, public HumanTurnControl() {
boolean inspectOnly) {
this.hand = hand;
this.table = table;
this.view = view;
this.inspectOnly = inspectOnly;
this.timer = new TurnTimer(view);
} }
/** /**
@ -67,25 +46,22 @@ public class HumanTurnControl extends AbstractTurnControl {
public static TurnControlFactory getFactory() { public static TurnControlFactory getFactory() {
return new TurnControlFactory() { return new TurnControlFactory() {
@Override @Override
public ITurnControl create(IHand hand, ITable table, IView view, public ITurnControl create() {
boolean inspectOnly) { return new HumanTurnControl();
return new HumanTurnControl(hand, table, view, inspectOnly);
} }
}; };
} }
/** Test only constructor **/ /** Test only constructor **/
HumanTurnControl(IHand hand, ITable table, IView view, ITurnTimer testTimer) { HumanTurnControl(ITurnTimer testTimer) {
this.hand = hand;
this.table = table;
this.view = view;
this.timer = testTimer; this.timer = testTimer;
this.inspectOnly = false;
} }
@Override @Override
public void startTurn() { public void startTurn() {
if (this.timer == null) {
this.timer = new TurnTimer(view);
}
IListener endOfTurnListener = new IListener() { IListener endOfTurnListener = new IListener() {
@Override @Override
@ -565,5 +541,4 @@ public class HumanTurnControl extends AbstractTurnControl {
} }
} }
} }
} }

View file

@ -1,14 +1,28 @@
package jrummikub.control.turn; package jrummikub.control.turn;
import jrummikub.model.IHand;
import jrummikub.model.ITable;
import jrummikub.util.Event; import jrummikub.util.Event;
import jrummikub.util.IEvent; import jrummikub.util.IEvent;
import jrummikub.view.IView;
public interface ITurnControl { public interface ITurnControl {
/** /**
* Start the turn * Start the turn
*
* @param hand
* active player's hand
* @param table
* current table
* @param view
* view for user interaction.
* @param inspectOnly
* the current turn doesn't allow any table manipulation
* @param mayRedeal
* true when the current player may decide to redeal
*/ */
public abstract void startTurn(); public abstract void setup(IHand hand, ITable table, IView view,
boolean inspectOnly, boolean mayRedeal);
/** /**
* Get the event that is emitted when the turn is over * Get the event that is emitted when the turn is over
@ -24,4 +38,9 @@ public interface ITurnControl {
*/ */
public abstract Event getRedealEvent(); public abstract Event getRedealEvent();
/**
* Start the turn
*/
public abstract void startTurn();
} }

View file

@ -5,5 +5,5 @@ import jrummikub.model.ITable;
import jrummikub.view.IView; import jrummikub.view.IView;
public interface TurnControlFactory { public interface TurnControlFactory {
public ITurnControl create(IHand hand, ITable table, IView view, boolean inspectOnly); public ITurnControl create();
} }

View file

@ -105,7 +105,8 @@ public class TurnControlTest {
mockTimer = new MockTimer(); mockTimer = new MockTimer();
mockTable = new MockTable(); mockTable = new MockTable();
mockHand = new MockHand(); mockHand = new MockHand();
testControl = new HumanTurnControl(mockHand, mockTable, mockView, mockTimer); testControl = new HumanTurnControl(mockTimer);
testControl.setup(mockHand, mockTable, mockView, false, false);
} }
/** */ /** */
@ -122,14 +123,15 @@ public class TurnControlTest {
public void showInitialHand() { public void showInitialHand() {
mockView.displayStartTurnPanel = true; mockView.displayStartTurnPanel = true;
List<Pair<Stone, Position>> stones = Arrays.asList( List<Pair<Stone, Position>> stones = Arrays
new Pair<Stone, Position>(new Stone(RED), new Position(0, 0)), .asList(new Pair<Stone, Position>(new Stone(RED), new Position(
new Pair<Stone, Position>(new Stone(BLACK), new Position(1, 0))); 0, 0)), new Pair<Stone, Position>(new Stone(BLACK),
new Position(1, 0)));
mockHand.iterable = stones; mockHand.iterable = stones;
testControl = new HumanTurnControl(mockHand, mockTable, mockView, mockTimer); testControl = new HumanTurnControl(mockTimer);
testControl.setup(mockHand, mockTable, mockView, false, false);
testControl.startTurn(); testControl.startTurn();
int i = 0; int i = 0;
@ -256,8 +258,8 @@ public class TurnControlTest {
mockView.handPanel.stoneClickEvent.emit(firstStone, true); mockView.handPanel.stoneClickEvent.emit(firstStone, true);
mockView.handPanel.stoneClickEvent.emit(secondStone, true); mockView.handPanel.stoneClickEvent.emit(secondStone, true);
mockView.tablePanel.stoneCollectionPanel.stoneClickEvent.emit(firstStone, mockView.tablePanel.stoneCollectionPanel.stoneClickEvent.emit(
false); firstStone, false);
assertCollection(Arrays.asList(secondStone)); assertCollection(Arrays.asList(secondStone));
} }
@ -273,8 +275,8 @@ public class TurnControlTest {
mockView.handPanel.stoneClickEvent.emit(firstStone, true); mockView.handPanel.stoneClickEvent.emit(firstStone, true);
mockView.handPanel.stoneClickEvent.emit(secondStone, true); mockView.handPanel.stoneClickEvent.emit(secondStone, true);
mockView.tablePanel.stoneCollectionPanel.stoneClickEvent.emit(firstStone, mockView.tablePanel.stoneCollectionPanel.stoneClickEvent.emit(
true); firstStone, true);
assertCollection(Arrays.asList(secondStone, firstStone)); assertCollection(Arrays.asList(secondStone, firstStone));
} }
@ -290,8 +292,8 @@ public class TurnControlTest {
mockView.handPanel.stoneClickEvent.emit(firstStone, true); mockView.handPanel.stoneClickEvent.emit(firstStone, true);
mockView.handPanel.stoneClickEvent.emit(secondStone, true); mockView.handPanel.stoneClickEvent.emit(secondStone, true);
mockView.tablePanel.stoneCollectionPanel.stoneClickEvent.emit(firstStone, mockView.tablePanel.stoneCollectionPanel.stoneClickEvent.emit(
true); firstStone, true);
mockView.tablePanel.stoneCollectionPanel.setClickEvent.emit(firstStone, mockView.tablePanel.stoneCollectionPanel.setClickEvent.emit(firstStone,
true); true);
@ -398,7 +400,8 @@ public class TurnControlTest {
Stone stone2 = new Stone(2, StoneColor.RED); Stone stone2 = new Stone(2, StoneColor.RED);
Stone stone3 = new Stone(3, StoneColor.RED); Stone stone3 = new Stone(3, StoneColor.RED);
Stone stone4 = new Stone(4, StoneColor.RED); Stone stone4 = new Stone(4, StoneColor.RED);
StoneSet set1 = new StoneSet(Arrays.asList(stone1, stone2, stone3, stone4)); StoneSet set1 = new StoneSet(Arrays.asList(stone1, stone2, stone3,
stone4));
mockTable.findStoneSet.put(stone1, set1); mockTable.findStoneSet.put(stone1, set1);
mockTable.findStoneSet.put(stone3, set1); mockTable.findStoneSet.put(stone3, set1);
@ -419,7 +422,8 @@ public class TurnControlTest {
Stone stone2 = new Stone(2, StoneColor.RED); Stone stone2 = new Stone(2, StoneColor.RED);
Stone stone3 = new Stone(3, StoneColor.RED); Stone stone3 = new Stone(3, StoneColor.RED);
Stone stone4 = new Stone(4, StoneColor.RED); Stone stone4 = new Stone(4, StoneColor.RED);
StoneSet set1 = new StoneSet(Arrays.asList(stone1, stone2, stone3, stone4)); StoneSet set1 = new StoneSet(Arrays.asList(stone1, stone2, stone3,
stone4));
mockTable.findStoneSet.put(stone1, set1); mockTable.findStoneSet.put(stone1, set1);
mockTable.findStoneSet.put(stone3, set1); mockTable.findStoneSet.put(stone3, set1);
@ -442,7 +446,8 @@ public class TurnControlTest {
Stone stone2 = new Stone(2, StoneColor.RED); Stone stone2 = new Stone(2, StoneColor.RED);
Stone stone3 = new Stone(3, StoneColor.RED); Stone stone3 = new Stone(3, StoneColor.RED);
Stone stone4 = new Stone(4, StoneColor.RED); Stone stone4 = new Stone(4, StoneColor.RED);
StoneSet set1 = new StoneSet(Arrays.asList(stone1, stone2, stone3, stone4)); StoneSet set1 = new StoneSet(Arrays.asList(stone1, stone2, stone3,
stone4));
mockTable.findStoneSet.put(stone1, set1); mockTable.findStoneSet.put(stone1, set1);
mockTable.findStoneSet.put(stone3, set1); mockTable.findStoneSet.put(stone3, set1);
@ -621,8 +626,8 @@ public class TurnControlTest {
@Test @Test
public void testAddLeft() { public void testAddLeft() {
AccessibleTable table = new AccessibleTable(); AccessibleTable table = new AccessibleTable();
HumanTurnControl turnControl = new HumanTurnControl(mockHand, table, mockView, HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
mockTimer); turnControl.setup(mockHand, 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);
@ -637,10 +642,10 @@ public class TurnControlTest {
Stone blackThree = new Stone(3, BLACK); Stone blackThree = new Stone(3, BLACK);
Stone blackFour = new Stone(4, BLACK); Stone blackFour = new Stone(4, BLACK);
Stone blackFive = new Stone(5, BLACK); Stone blackFive = new Stone(5, BLACK);
StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne, blackOne, StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne,
redTwo, redThree, redFour, blackTwo, blackThree)); blackOne, redTwo, redThree, redFour, blackTwo, blackThree));
StoneSet oldSet2 = new StoneSet( StoneSet oldSet2 = new StoneSet(Arrays.asList(blueTwo, blackFour,
Arrays.asList(blueTwo, blackFour, blackFive)); blackFive));
table.drop(oldSet1, new Position(0, 0)); table.drop(oldSet1, new Position(0, 0));
table.drop(oldSet2, new Position(0, 0)); table.drop(oldSet2, new Position(0, 0));
mockHand.drop(blueThree, new Position(0, 0)); mockHand.drop(blueThree, new Position(0, 0));
@ -738,8 +743,8 @@ public class TurnControlTest {
@Test @Test
public void testAddRight() { public void testAddRight() {
AccessibleTable table = new AccessibleTable(); AccessibleTable table = new AccessibleTable();
HumanTurnControl turnControl = new HumanTurnControl(mockHand, table, mockView, HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
mockTimer); turnControl.setup(mockHand, 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);
@ -754,10 +759,10 @@ public class TurnControlTest {
Stone blackThree = new Stone(3, BLACK); Stone blackThree = new Stone(3, BLACK);
Stone blackFour = new Stone(4, BLACK); Stone blackFour = new Stone(4, BLACK);
Stone blackFive = new Stone(5, BLACK); Stone blackFive = new Stone(5, BLACK);
StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne, blackOne, StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne,
redTwo, redThree, redFour, blackTwo, blackThree)); blackOne, redTwo, redThree, redFour, blackTwo, blackThree));
StoneSet oldSet2 = new StoneSet( StoneSet oldSet2 = new StoneSet(Arrays.asList(blueTwo, blackFour,
Arrays.asList(blueTwo, blackFour, blackFive)); blackFive));
table.drop(oldSet1, new Position(0, 0)); table.drop(oldSet1, new Position(0, 0));
table.drop(oldSet2, new Position(0, 0)); table.drop(oldSet2, new Position(0, 0));
mockHand.drop(blueThree, new Position(0, 0)); mockHand.drop(blueThree, new Position(0, 0));
@ -855,8 +860,8 @@ public class TurnControlTest {
@Test @Test
public void testAddNewSet() { public void testAddNewSet() {
AccessibleTable table = new AccessibleTable(); AccessibleTable table = new AccessibleTable();
HumanTurnControl turnControl = new HumanTurnControl(mockHand, table, mockView, HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
mockTimer); turnControl.setup(mockHand, 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);
@ -871,10 +876,10 @@ public class TurnControlTest {
Stone blackThree = new Stone(3, BLACK); Stone blackThree = new Stone(3, BLACK);
Stone blackFour = new Stone(4, BLACK); Stone blackFour = new Stone(4, BLACK);
Stone blackFive = new Stone(5, BLACK); Stone blackFive = new Stone(5, BLACK);
StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne, blackOne, StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne,
redTwo, redThree, redFour, blackTwo, blackThree)); blackOne, redTwo, redThree, redFour, blackTwo, blackThree));
StoneSet oldSet2 = new StoneSet( StoneSet oldSet2 = new StoneSet(Arrays.asList(blueTwo, blackFour,
Arrays.asList(blueTwo, blackFour, blackFive)); blackFive));
table.drop(oldSet1, new Position(0, 0)); table.drop(oldSet1, new Position(0, 0));
table.drop(oldSet2, new Position(0, 0)); table.drop(oldSet2, new Position(0, 0));
mockHand.drop(blueThree, new Position(0, 0)); mockHand.drop(blueThree, new Position(0, 0));
@ -978,7 +983,8 @@ public class TurnControlTest {
List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>( List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>(
mockHand.stones); mockHand.stones);
Collections.sort(stones, new HumanTurnControl.HandStonePositionComparator()); Collections.sort(stones,
new HumanTurnControl.HandStonePositionComparator());
assertEquals(stones.size(), 13); assertEquals(stones.size(), 13);
@ -1039,7 +1045,8 @@ public class TurnControlTest {
List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>( List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>(
mockHand.stones); mockHand.stones);
Collections.sort(stones, new HumanTurnControl.HandStonePositionComparator()); Collections.sort(stones,
new HumanTurnControl.HandStonePositionComparator());
assertEquals(stones.size(), 13); assertEquals(stones.size(), 13);