2011-05-04 19:09:39 +02:00
|
|
|
package jrummikub.control;
|
|
|
|
|
2011-05-05 15:57:13 +02:00
|
|
|
import static jrummikub.model.StoneColor.BLACK;
|
|
|
|
import static jrummikub.model.StoneColor.BLUE;
|
2011-05-16 22:24:11 +02:00
|
|
|
import static jrummikub.model.StoneColor.ORANGE;
|
2011-05-05 15:57:13 +02:00
|
|
|
import static jrummikub.model.StoneColor.RED;
|
2011-05-04 23:26:01 +02:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
2011-05-05 21:23:18 +02:00
|
|
|
import static org.junit.Assert.assertNull;
|
2011-05-04 23:26:01 +02:00
|
|
|
import static org.junit.Assert.assertSame;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
2011-05-05 00:43:11 +02:00
|
|
|
|
2011-05-05 15:57:13 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashSet;
|
2011-05-05 00:43:11 +02:00
|
|
|
import java.util.Iterator;
|
2011-05-16 22:24:11 +02:00
|
|
|
import java.util.List;
|
2011-05-05 15:57:13 +02:00
|
|
|
import java.util.Set;
|
2011-05-05 00:43:11 +02:00
|
|
|
|
2011-05-16 22:01:02 +02:00
|
|
|
import jrummikub.model.Hand;
|
|
|
|
import jrummikub.model.IHand;
|
2011-05-16 22:24:11 +02:00
|
|
|
import jrummikub.model.ITable;
|
2011-05-18 15:25:26 +02:00
|
|
|
import jrummikub.model.MockRoundState;
|
2011-05-04 23:26:01 +02:00
|
|
|
import jrummikub.model.MockTable;
|
|
|
|
import jrummikub.model.Position;
|
|
|
|
import jrummikub.model.Stone;
|
|
|
|
import jrummikub.model.StoneSet;
|
2011-05-16 22:24:11 +02:00
|
|
|
import jrummikub.model.Table;
|
2011-05-05 00:43:11 +02:00
|
|
|
import jrummikub.util.Pair;
|
2011-05-04 19:09:39 +02:00
|
|
|
import jrummikub.view.MockView;
|
|
|
|
|
2011-05-04 21:37:30 +02:00
|
|
|
import org.junit.Before;
|
2011-05-04 19:09:39 +02:00
|
|
|
import org.junit.Test;
|
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/**
|
|
|
|
* Tests for {@link RoundControl}
|
|
|
|
*/
|
2011-05-04 19:09:39 +02:00
|
|
|
public class RoundControlTest {
|
2011-05-04 21:37:30 +02:00
|
|
|
private MockView view;
|
2011-05-18 15:25:26 +02:00
|
|
|
private MockRoundState testGameState;
|
2011-05-04 21:37:30 +02:00
|
|
|
private RoundControl testRound;
|
2011-05-05 00:43:11 +02:00
|
|
|
private MockTable newTable;
|
2011-05-04 19:09:39 +02:00
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/**
|
|
|
|
* For each test create a round control initialized by a mock model and view
|
|
|
|
*/
|
2011-05-04 21:37:30 +02:00
|
|
|
@Before
|
|
|
|
public void setup() {
|
|
|
|
view = new MockView();
|
2011-05-18 15:25:26 +02:00
|
|
|
testGameState = new MockRoundState();
|
2011-05-04 21:37:30 +02:00
|
|
|
testRound = new RoundControl(testGameState, view);
|
2011-05-05 00:43:11 +02:00
|
|
|
Stone stone = testGameState.getGameHeap().drawStone();
|
|
|
|
testGameState.table.drop(new StoneSet(stone), new Position(5, 0));
|
|
|
|
newTable = new MockTable();
|
2011-05-05 01:33:58 +02:00
|
|
|
newTable.sets.add(testGameState.table.sets.get(0));
|
|
|
|
testGameState.table.clonedTable = newTable;
|
2011-05-04 21:37:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void checkCorrectlyDealed() {
|
2011-05-05 01:33:58 +02:00
|
|
|
assertEquals(106 - testGameState.getPlayerCount() * 14
|
2011-05-16 22:01:02 +02:00
|
|
|
- testGameState.table.getSize(), testGameState.getGameHeap().getSize());
|
2011-05-04 19:09:39 +02:00
|
|
|
for (int i = 0; i < testGameState.getPlayerCount(); i++) {
|
2011-05-16 22:01:02 +02:00
|
|
|
assertEquals(14, testGameState.getNthNextPlayer(i).getHand().getSize());
|
2011-05-04 19:09:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-04 23:26:01 +02:00
|
|
|
private void checkTurnStartSetUp() {
|
|
|
|
assertNotNull(view.currentPlayerName);
|
|
|
|
assertNotNull(view.getTablePanel().leftPlayerName);
|
|
|
|
assertNotNull(view.getTablePanel().topPlayerName);
|
|
|
|
assertNotNull(view.getTablePanel().rightPlayerName);
|
|
|
|
assertTrue(view.displayStartTurnPanel);
|
|
|
|
assertFalse(view.startTurnEvent.listeners.isEmpty());
|
2011-05-06 01:35:09 +02:00
|
|
|
assertFalse(view.displayWinPanel);
|
2011-05-05 00:43:11 +02:00
|
|
|
checkTableDisplay();
|
|
|
|
}
|
2011-05-05 01:33:58 +02:00
|
|
|
|
2011-05-05 00:43:11 +02:00
|
|
|
private void checkTableDisplay() {
|
2011-05-05 01:33:58 +02:00
|
|
|
Iterator<Pair<StoneSet, Position>> stoneSetsView = view.tablePanel.stoneSets
|
|
|
|
.iterator();
|
2011-05-09 20:54:17 +02:00
|
|
|
Iterator<Pair<StoneSet, Position>> stoneSetsModel = testGameState.table
|
|
|
|
.clone().sets.iterator();
|
2011-05-05 01:33:58 +02:00
|
|
|
|
|
|
|
while (stoneSetsView.hasNext()) {
|
2011-05-05 00:43:11 +02:00
|
|
|
assertTrue(stoneSetsModel.hasNext());
|
|
|
|
assertSame(stoneSetsView.next(), stoneSetsModel.next());
|
|
|
|
}
|
|
|
|
assertFalse(stoneSetsModel.hasNext());
|
2011-05-04 23:26:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void resetTurnStart() {
|
|
|
|
view.currentPlayerName = null;
|
|
|
|
view.getTablePanel().leftPlayerName = null;
|
|
|
|
view.getTablePanel().topPlayerName = null;
|
|
|
|
view.getTablePanel().rightPlayerName = null;
|
|
|
|
view.displayStartTurnPanel = false;
|
|
|
|
}
|
2011-05-16 22:01:02 +02:00
|
|
|
|
2011-05-16 20:54:37 +02:00
|
|
|
/** */
|
2011-05-16 22:01:02 +02:00
|
|
|
@Test
|
|
|
|
public void playerCameOutCorrectly() {
|
2011-05-16 20:54:37 +02:00
|
|
|
MockTable oldTable = new MockTable();
|
|
|
|
MockTable newTable = new MockTable();
|
|
|
|
Stone blueTen = new Stone(10, BLUE);
|
|
|
|
Stone redTen = new Stone(10, RED);
|
|
|
|
Stone blackTen = new Stone(10, BLACK);
|
2011-05-16 22:01:02 +02:00
|
|
|
|
2011-05-16 20:54:37 +02:00
|
|
|
}
|
2011-05-04 23:26:01 +02:00
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/** */
|
2011-05-09 20:54:17 +02:00
|
|
|
@Test
|
2011-05-16 22:01:02 +02:00
|
|
|
public void testDealStones() {
|
2011-05-09 20:54:17 +02:00
|
|
|
testRound.deal();
|
|
|
|
checkCorrectlyDealed();
|
2011-05-16 22:01:02 +02:00
|
|
|
for (int i = 0; i < 14; i++) {
|
|
|
|
testRound.dealStones(2);
|
2011-05-09 20:54:17 +02:00
|
|
|
}
|
2011-05-16 22:01:02 +02:00
|
|
|
assertEquals(2 * 14 + 14, testGameState.getActivePlayer().getHand()
|
|
|
|
.getSize());
|
2011-05-09 20:54:17 +02:00
|
|
|
}
|
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/** */
|
2011-05-04 21:37:30 +02:00
|
|
|
@Test
|
|
|
|
public void testDeal() {
|
|
|
|
testRound.deal();
|
|
|
|
checkCorrectlyDealed();
|
|
|
|
}
|
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/** */
|
2011-05-04 21:37:30 +02:00
|
|
|
@Test
|
|
|
|
public void testStartRound() {
|
|
|
|
testRound.startRound();
|
|
|
|
checkCorrectlyDealed();
|
|
|
|
|
2011-05-04 23:26:01 +02:00
|
|
|
checkTurnStartSetUp();
|
|
|
|
}
|
2011-05-05 01:33:58 +02:00
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/** */
|
2011-05-05 00:43:11 +02:00
|
|
|
@Test
|
2011-05-05 00:49:52 +02:00
|
|
|
public void testTableDisplay() {
|
2011-05-05 00:43:11 +02:00
|
|
|
testRound.startRound();
|
|
|
|
checkCorrectlyDealed();
|
2011-05-05 00:49:52 +02:00
|
|
|
view.startTurnEvent.emit();
|
2011-05-05 00:43:11 +02:00
|
|
|
checkTableDisplay();
|
2011-05-05 01:08:40 +02:00
|
|
|
view.getPlayerPanel().endTurnEvent.emit();
|
2011-05-05 00:43:11 +02:00
|
|
|
}
|
2011-05-04 23:26:01 +02:00
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/** */
|
2011-05-04 23:26:01 +02:00
|
|
|
@Test
|
|
|
|
public void testTableValidHandChanged() {
|
|
|
|
testRound.startRound();
|
|
|
|
MockTable oldTable = testGameState.table;
|
|
|
|
newTable.valid = true;
|
|
|
|
oldTable.clonedTable = newTable;
|
|
|
|
|
|
|
|
view.startTurnEvent.emit();
|
|
|
|
assertFalse(view.displayStartTurnPanel);
|
2011-05-16 22:01:02 +02:00
|
|
|
|
|
|
|
IHand hand = testGameState.players.get(0).hand;
|
|
|
|
hand.pickUp(hand.iterator().next().getFirst());
|
2011-05-04 23:26:01 +02:00
|
|
|
resetTurnStart();
|
|
|
|
view.getPlayerPanel().endTurnEvent.emit();
|
|
|
|
|
2011-05-05 21:23:18 +02:00
|
|
|
assertSame(newTable, testGameState.setTable);
|
2011-05-04 23:26:01 +02:00
|
|
|
assertEquals(1, testGameState.activePlayer);
|
|
|
|
|
|
|
|
checkTurnStartSetUp();
|
|
|
|
}
|
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/** */
|
2011-05-04 23:26:01 +02:00
|
|
|
@Test
|
|
|
|
public void testTableInvalidHandChanged() {
|
|
|
|
testRound.startRound();
|
|
|
|
MockTable oldTable = testGameState.table;
|
|
|
|
newTable.valid = false;
|
|
|
|
oldTable.clonedTable = newTable;
|
|
|
|
|
|
|
|
view.startTurnEvent.emit();
|
|
|
|
assertFalse(view.displayStartTurnPanel);
|
2011-05-16 22:01:02 +02:00
|
|
|
IHand hand = testGameState.players.get(0).hand;
|
|
|
|
Stone stone = hand.iterator().next().getFirst();
|
|
|
|
hand.pickUp(stone);
|
2011-05-04 23:26:01 +02:00
|
|
|
newTable.drop(new StoneSet(stone), new Position(0, 0));
|
|
|
|
resetTurnStart();
|
|
|
|
view.getPlayerPanel().endTurnEvent.emit();
|
|
|
|
|
2011-05-05 21:23:18 +02:00
|
|
|
assertNull(testGameState.setTable);
|
2011-05-04 23:26:01 +02:00
|
|
|
assertEquals(1, testGameState.activePlayer);
|
|
|
|
assertEquals(14 + 3, testGameState.players.get(0).hand.getSize());
|
|
|
|
checkTurnStartSetUp();
|
|
|
|
}
|
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/** */
|
2011-05-04 23:26:01 +02:00
|
|
|
@Test
|
|
|
|
public void testTableValidHandUnchanged() {
|
|
|
|
testRound.startRound();
|
|
|
|
MockTable oldTable = testGameState.table;
|
|
|
|
newTable.valid = true;
|
|
|
|
oldTable.clonedTable = newTable;
|
|
|
|
|
|
|
|
view.startTurnEvent.emit();
|
|
|
|
assertFalse(view.displayStartTurnPanel);
|
|
|
|
resetTurnStart();
|
|
|
|
view.getPlayerPanel().endTurnEvent.emit();
|
|
|
|
|
2011-05-05 21:23:18 +02:00
|
|
|
assertSame(newTable, testGameState.setTable);
|
2011-05-04 23:26:01 +02:00
|
|
|
assertEquals(14 + 1, testGameState.players.get(0).hand.getSize());
|
|
|
|
assertEquals(1, testGameState.activePlayer);
|
|
|
|
|
|
|
|
checkTurnStartSetUp();
|
|
|
|
}
|
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/** */
|
2011-05-04 23:26:01 +02:00
|
|
|
@Test
|
|
|
|
public void testTableInvalidHandUnchanged() {
|
|
|
|
testRound.startRound();
|
|
|
|
MockTable oldTable = testGameState.table;
|
|
|
|
newTable.valid = false;
|
|
|
|
oldTable.clonedTable = newTable;
|
|
|
|
|
|
|
|
view.startTurnEvent.emit();
|
|
|
|
assertFalse(view.displayStartTurnPanel);
|
|
|
|
resetTurnStart();
|
|
|
|
view.getPlayerPanel().endTurnEvent.emit();
|
|
|
|
|
2011-05-05 21:23:18 +02:00
|
|
|
assertNull(testGameState.setTable);
|
2011-05-04 23:26:01 +02:00
|
|
|
assertEquals(14 + 1, testGameState.players.get(0).hand.getSize());
|
|
|
|
assertEquals(1, testGameState.activePlayer);
|
|
|
|
|
|
|
|
checkTurnStartSetUp();
|
|
|
|
}
|
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/** */
|
2011-05-04 23:26:01 +02:00
|
|
|
@Test
|
|
|
|
public void testWinning() {
|
|
|
|
testRound.startRound();
|
|
|
|
MockTable oldTable = testGameState.table;
|
|
|
|
newTable.valid = true;
|
|
|
|
oldTable.clonedTable = newTable;
|
|
|
|
|
|
|
|
view.startTurnEvent.emit();
|
|
|
|
assertFalse(view.displayStartTurnPanel);
|
2011-05-16 22:01:02 +02:00
|
|
|
IHand hand = testGameState.players.get(0).hand;
|
|
|
|
Stone stone = hand.iterator().next().getFirst();
|
|
|
|
hand.pickUp(stone);
|
2011-05-06 01:35:09 +02:00
|
|
|
newTable.drop(new StoneSet(stone), new Position(0, 0));
|
2011-05-16 22:01:02 +02:00
|
|
|
testGameState.players.get(0).hand = new Hand();
|
2011-05-04 23:26:01 +02:00
|
|
|
resetTurnStart();
|
|
|
|
view.getPlayerPanel().endTurnEvent.emit();
|
|
|
|
|
|
|
|
assertTrue(view.displayWinPanel);
|
2011-05-04 21:37:30 +02:00
|
|
|
}
|
2011-05-05 15:57:13 +02:00
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/** */
|
2011-05-05 15:57:13 +02:00
|
|
|
@Test
|
|
|
|
public void testTableDifference() {
|
|
|
|
MockTable oldTable = new MockTable();
|
|
|
|
MockTable newTable = new MockTable();
|
|
|
|
Stone blueOne = new Stone(1, BLUE);
|
|
|
|
Stone redOne = new Stone(1, RED);
|
|
|
|
Stone blackOne = new Stone(1, BLACK);
|
|
|
|
Stone blueTwo = new Stone(2, BLUE);
|
|
|
|
Stone blueThree = new Stone(3, BLUE);
|
|
|
|
Stone blueFour = new Stone(4, BLUE);
|
2011-05-16 22:01:02 +02:00
|
|
|
StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne, blackOne));
|
2011-05-05 15:57:13 +02:00
|
|
|
StoneSet oldSet2 = new StoneSet(blueTwo);
|
|
|
|
oldTable.drop(oldSet1, new Position(0, 0));
|
|
|
|
oldTable.drop(oldSet2, new Position(0, 0));
|
2011-05-16 22:01:02 +02:00
|
|
|
StoneSet newSet1 = new StoneSet(Arrays.asList(blueOne, blueTwo, blueFour));
|
|
|
|
StoneSet newSet2 = new StoneSet(Arrays.asList(redOne, blackOne, blueThree));
|
2011-05-05 15:57:13 +02:00
|
|
|
newTable.drop(newSet1, new Position(0, 0));
|
|
|
|
newTable.drop(newSet2, new Position(0, 0));
|
2011-05-09 20:54:17 +02:00
|
|
|
|
2011-05-05 15:57:13 +02:00
|
|
|
Set<Stone> expectedStones = new HashSet<Stone>();
|
|
|
|
expectedStones.add(blueThree);
|
|
|
|
expectedStones.add(blueFour);
|
2011-05-09 20:54:17 +02:00
|
|
|
|
2011-05-05 15:57:13 +02:00
|
|
|
Set<Stone> stones = RoundControl.tableDifference(oldTable, newTable);
|
2011-05-09 20:54:17 +02:00
|
|
|
|
2011-05-05 15:57:13 +02:00
|
|
|
assertTrue(expectedStones.containsAll(stones));
|
|
|
|
assertTrue(stones.containsAll(expectedStones));
|
|
|
|
}
|
2011-05-16 22:24:11 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testTableSetDifference() {
|
|
|
|
ITable oldTable = new Table();
|
|
|
|
Stone blueOne = new Stone(1, BLUE);
|
|
|
|
Stone redOne = new Stone(1, RED);
|
|
|
|
Stone blackOne = new Stone(1, BLACK);
|
|
|
|
Stone orangeOne = new Stone(1, ORANGE);
|
|
|
|
Stone blueTwo = new Stone(2, BLUE);
|
|
|
|
Stone blueThree = new Stone(3, BLUE);
|
|
|
|
Stone blueFour = new Stone(4, BLUE);
|
|
|
|
StoneSet oldSet1 = new StoneSet(Arrays.asList(blueOne, redOne,
|
|
|
|
blackOne, orangeOne));
|
|
|
|
StoneSet oldSet2 = new StoneSet(Arrays.asList(blueTwo, blueThree,
|
|
|
|
blueFour));
|
|
|
|
oldTable.drop(oldSet1, new Position(0, 0));
|
|
|
|
oldTable.drop(oldSet2, new Position(0, 0));
|
|
|
|
ITable newTable = (Table) oldTable.clone();
|
|
|
|
List<StoneSet> newSets = RoundControl.tableSetDifference(oldTable,
|
|
|
|
newTable);
|
|
|
|
List<StoneSet> vanishedSets = RoundControl.tableSetDifference(newTable,
|
|
|
|
oldTable);
|
|
|
|
|
|
|
|
assertTrue(newSets.isEmpty());
|
|
|
|
assertTrue(vanishedSets.isEmpty());
|
|
|
|
|
|
|
|
newTable.pickUp(oldSet2);
|
|
|
|
newTable.drop(oldSet2.join(new StoneSet(new Stone(5, BLUE))),
|
|
|
|
new Position(0, 0));
|
|
|
|
newSets = RoundControl.tableSetDifference(oldTable, newTable);
|
|
|
|
vanishedSets = RoundControl.tableSetDifference(newTable, oldTable);
|
|
|
|
|
|
|
|
assertFalse(newSets.isEmpty());
|
|
|
|
assertFalse(vanishedSets.isEmpty());
|
|
|
|
assertEquals(1, newSets.size());
|
|
|
|
assertEquals(1, vanishedSets.size());
|
|
|
|
|
|
|
|
Stone redTwo = new Stone(2, RED);
|
|
|
|
Stone redThree = new Stone(3, RED);
|
|
|
|
Stone redFour = new Stone(4, RED);
|
|
|
|
StoneSet oldSet3 = new StoneSet(
|
|
|
|
Arrays.asList(redTwo, redThree, redFour));
|
|
|
|
ITable newTable2 = (Table) oldTable.clone();
|
|
|
|
newTable2.drop(oldSet3, new Position(0, 0));
|
|
|
|
newSets = RoundControl.tableSetDifference(oldTable, newTable2);
|
|
|
|
vanishedSets = RoundControl.tableSetDifference(newTable2, oldTable);
|
|
|
|
|
|
|
|
assertFalse(newSets.isEmpty());
|
|
|
|
assertTrue(vanishedSets.isEmpty());
|
|
|
|
assertEquals(1, newSets.size());
|
|
|
|
}
|
2011-05-04 19:09:39 +02:00
|
|
|
}
|