Added test and implementation for an inspection turn before the first

turn

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@273 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Jannis Harder 2011-05-25 17:10:43 +02:00
parent 157bd4f606
commit a1c0cb89f6
7 changed files with 140 additions and 19 deletions

View file

@ -22,8 +22,10 @@ public class MockRoundState implements IRoundState {
public GameSettings gameSettings;
/** */
public IPlayer lastPlayer;
/** */
public int turnNumber;
/** */
public MockRoundState() {
table = new MockTable();
players = new ArrayList<MockPlayer>();
@ -39,6 +41,7 @@ public class MockRoundState implements IRoundState {
gameHeap = new StoneHeap();
gameSettings = new GameSettings();
gameSettings.setInitialMeldThreshold(30);
turnNumber = -3;
}
@Override
@ -113,4 +116,13 @@ public class MockRoundState implements IRoundState {
}
return players.get(j);
}
public int getTurnNumber() {
return turnNumber;
}
@Override
public void nextTurn() {
turnNumber++;
}
}

View file

@ -7,6 +7,7 @@ import jrummikub.util.Event1;
import jrummikub.util.Event2;
import jrummikub.util.IEvent1;
import jrummikub.util.IEvent2;
import jrummikub.util.MockEvent1;
import jrummikub.util.Pair;
/**
@ -21,7 +22,7 @@ public class MockTablePanel implements ITablePanel {
/** */
public Event2<Stone, Boolean> rangeClickEvent = new Event2<Stone, Boolean>();
/** */
public Event1<Position> clickEvent = new Event1<Position>();
public MockEvent1<Position> clickEvent = new MockEvent1<Position>();
/** */
public Event1<StoneSet> leftConnectorClickEvent = new Event1<StoneSet>();
/** */

View file

@ -5,6 +5,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import jrummikub.model.GameState;
import jrummikub.model.Hand;
import jrummikub.model.IHand;
import jrummikub.model.IPlayer;
@ -87,7 +88,7 @@ public class RoundControl {
private void startTurn() {
TurnControl turnControl = new TurnControl(roundState.getActivePlayer()
.getHand(), clonedTable, view);
.getHand(), clonedTable, view, roundState.getTurnNumber() < 1);
connections.add(turnControl.getEndOfTurnEvent().add(new IListener() {
@Override
@ -126,19 +127,24 @@ public class RoundControl {
}
private void endOfTurn() {
checkTurn();
if (roundState.getTurnNumber() >= 1) {
checkTurn();
}
if (roundState.getLastPlayer() == null) {
if (roundState.getGameHeap().getSize() == 0) {
roundState.setLastPlayer(roundState.getNthNextPlayer(-1));
roundState.nextTurn();
} else {
roundState.nextPlayer();
roundState.nextTurn();
}
} else {
if (roundState.getActivePlayer() == roundState.getLastPlayer()) {
endOfRound();
} else {
roundState.nextPlayer();
roundState.nextTurn();
}
}
if (!roundFinished) {

View file

@ -35,22 +35,27 @@ public class TurnControl {
private Event endOfTurnEvent = new Event();
private List<Connection> connections = new ArrayList<Connection>();
private boolean inspectOnly;
/**
* Create a new TurnControl using a given hand (of the active player), a given
* table and a given view for user interaction.
* Create a new TurnControl using a given hand (of the active player), a
* given table and a given view for user interaction.
*
* @param hand
* active player's hand
* active player's hand
* @param table
* current table
* current table
* @param view
* view for user interaction.
* view for user interaction.
* @param inspectOnly
* the current turn doesn't allow any table manipulation
*/
public TurnControl(IHand hand, ITable table, IView view) {
public TurnControl(IHand hand, ITable table, IView view, boolean inspectOnly) {
this.hand = hand;
this.table = table;
this.view = view;
this.inspectOnly = inspectOnly;
this.timer = new TurnTimer(view);
}
@ -60,6 +65,7 @@ public class TurnControl {
this.table = table;
this.view = view;
this.timer = testTimer;
this.inspectOnly = false;
}
/**
@ -80,7 +86,8 @@ public class TurnControl {
addHandPanelHandlers();
addStoneCollectionHandlers();
addTablePanelHandlers();
if (!inspectOnly)
addTablePanelHandlers();
connections.add(view.getPlayerPanel().getSortByGroupsEvent()
.add(new IListener() {
@ -419,15 +426,12 @@ public class TurnControl {
table.drop(joinedSet, newPos);
} else {
StoneSet joinedSet = new StoneSet(selectedStones).join(newSet);
table.drop(joinedSet,
new Position(newPos.getX() - selectedStones.size(), newPos.getY()));
table.drop(joinedSet, new Position(newPos.getX()
- selectedStones.size(), newPos.getY()));
}
} else {
table.drop(
new StoneSet(selectedStones),
new Position(
pos.getX() + (set.size() - selectedStones.size()) * 0.5f, pos
.getY()));
table.drop(new StoneSet(selectedStones), new Position(pos.getX()
+ (set.size() - selectedStones.size()) * 0.5f, pos.getY()));
}
selectedStones.clear();
@ -527,7 +531,8 @@ public class TurnControl {
static class HandStonePositionComparator implements
Comparator<Pair<Stone, Position>> {
@Override
public int compare(Pair<Stone, Position> pair1, Pair<Stone, Position> pair2) {
public int compare(Pair<Stone, Position> pair1,
Pair<Stone, Position> pair2) {
Position pos1 = pair1.getSecond(), pos2 = pair2.getSecond();
if (pos1.getY() < pos2.getY()) {
return -1;

View file

@ -94,4 +94,18 @@ public interface IRoundState {
*/
public void setActivePlayerNumber(int i);
/**
* Gets the number of the current turn. Numbers smaller than one indicate
* hand inspection turns
*
* @return current turn number
*/
public abstract int getTurnNumber();
/**
* Increments the turn number
*
*/
public void nextTurn();
}

View file

@ -12,6 +12,7 @@ public class RoundState implements IRoundState {
private int activePlayer;
private StoneHeap gameHeap;
private IPlayer lastPlayer;
private int turnNumber;
/**
* Create a new RoundState with an empty table
@ -29,6 +30,8 @@ public class RoundState implements IRoundState {
players.add(new Player(playerSettings, gameSettings));
}
turnNumber = 1-gameSettings.getPlayerList().size();
activePlayer = 0;
gameHeap = new StoneHeap();
}
@ -104,4 +107,14 @@ public class RoundState implements IRoundState {
public IPlayer getLastPlayer() {
return lastPlayer;
}
@Override
public int getTurnNumber() {
return turnNumber;
}
@Override
public void nextTurn() {
turnNumber++;
}
}

View file

@ -131,6 +131,10 @@ public class RoundControlTest {
@Test
public void laidOutValidTooFew() {
roundControl.startRound();
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
view.playerPanel.endTurnEvent.emit();
}
view.startTurnEvent.emit();
Stone blueOne = new Stone(1, BLUE);
@ -161,6 +165,10 @@ public class RoundControlTest {
@Test
public void laidOutValidUnchanged() {
roundControl.startRound();
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
view.playerPanel.endTurnEvent.emit();
}
view.startTurnEvent.emit();
IHand hand = roundState.getActivePlayer().getHand();
@ -178,6 +186,10 @@ public class RoundControlTest {
@Test
public void laidOutInvalidEnough() {
roundControl.startRound();
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
view.playerPanel.endTurnEvent.emit();
}
view.startTurnEvent.emit();
Stone blueOne = new Stone(1, BLUE);
@ -222,6 +234,10 @@ public class RoundControlTest {
@Test
public void laidOutTooFewChangedTable() {
roundControl.startRound();
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
view.playerPanel.endTurnEvent.emit();
}
view.startTurnEvent.emit();
// Fake Turn to put stones on the table
Stone blueSeven = new Stone(7, BLUE);
@ -289,6 +305,10 @@ public class RoundControlTest {
@Test
public void laidOutEnoughChangedTable() {
roundControl.startRound();
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
view.playerPanel.endTurnEvent.emit();
}
view.startTurnEvent.emit();
// Fake Turn to put stones on the table
Stone blueSeven = new Stone(7, BLUE);
@ -359,6 +379,10 @@ public class RoundControlTest {
@Test
public void laidOutJustChangedTable() {
roundControl.startRound();
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
view.playerPanel.endTurnEvent.emit();
}
view.startTurnEvent.emit();
// Fake Turn to put stones on the table
Stone blueFour = new Stone(4, BLUE);
@ -419,6 +443,10 @@ public class RoundControlTest {
@Test
public void laidOutValid() {
roundControl.startRound();
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
view.playerPanel.endTurnEvent.emit();
}
view.startTurnEvent.emit();
// Fake Turn to put stones on the table
Stone blueSeven = new Stone(7, BLUE);
@ -503,6 +531,10 @@ public class RoundControlTest {
public void testTableValidHandChanged() {
testRoundState.players.get(0).setLaidOut(true);
testRound.startRound();
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
view.playerPanel.endTurnEvent.emit();
}
MockTable oldTable = testRoundState.table;
testTable.valid = true;
oldTable.clonedTable = testTable;
@ -525,6 +557,10 @@ public class RoundControlTest {
@Test
public void testTableInvalidHandChanged() {
testRound.startRound();
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
view.playerPanel.endTurnEvent.emit();
}
MockTable oldTable = testRoundState.table;
testTable.valid = false;
oldTable.clonedTable = testTable;
@ -549,6 +585,10 @@ public class RoundControlTest {
public void testTableValidHandUnchanged() {
testRoundState.players.get(0).setLaidOut(true);
testRound.startRound();
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
view.playerPanel.endTurnEvent.emit();
}
MockTable oldTable = testRoundState.table;
testTable.valid = true;
oldTable.clonedTable = testTable;
@ -570,6 +610,10 @@ public class RoundControlTest {
public void testTableInvalidHandUnchanged() {
testRoundState.players.get(1).setLaidOut(true);
testRound.startRound();
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
view.playerPanel.endTurnEvent.emit();
}
MockTable oldTable = testRoundState.table;
testTable.valid = false;
oldTable.clonedTable = testTable;
@ -598,6 +642,10 @@ public class RoundControlTest {
});
testRound.startRound();
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
view.playerPanel.endTurnEvent.emit();
}
MockTable oldTable = testRoundState.table;
testTable.valid = true;
oldTable.clonedTable = testTable;
@ -731,6 +779,10 @@ public class RoundControlTest {
roundState.getGameHeap().drawStones(106 - 14 * 4 - 1);
roundControl.startRound();
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
view.playerPanel.endTurnEvent.emit();
}
IPlayer player1 = roundState.getActivePlayer();
@ -839,4 +891,22 @@ public class RoundControlTest {
assertEquals(-3, (int) roundScore.getPoints().get(2));
assertEquals(-100, (int) roundScore.getPoints().get(3));
}
/** */
@Test
public void testInspectOnly() {
testRound.startRound();
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
assertTrue(view.tablePanel.clickEvent.listeners.isEmpty());
view.playerPanel.endTurnEvent.emit();
assertEquals(14, testRoundState.players.get(i).hand.getSize());
}
for (int i = 0; i < 4; i++) {
view.startTurnEvent.emit();
assertFalse(view.tablePanel.clickEvent.listeners.isEmpty());
view.playerPanel.endTurnEvent.emit();
assertFalse(14 == testRoundState.players.get(i).hand.getSize());
}
}
}