summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/jrummikub/control/GameControl.java15
-rw-r--r--src/jrummikub/control/RoundControl.java22
-rw-r--r--src/jrummikub/control/SettingsControl.java16
-rw-r--r--src/jrummikub/control/TurnControl.java27
-rw-r--r--src/jrummikub/view/impl/SettingsPanel.java16
-rw-r--r--test/jrummikub/control/GameControlTest.java100
6 files changed, 169 insertions, 27 deletions
diff --git a/src/jrummikub/control/GameControl.java b/src/jrummikub/control/GameControl.java
index 781af31..0ae05a6 100644
--- a/src/jrummikub/control/GameControl.java
+++ b/src/jrummikub/control/GameControl.java
@@ -19,7 +19,7 @@ import jrummikub.view.IView;
public class GameControl {
private GameSettings gameSettings;
private IView view;
- private RoundControl roundControl;
+ RoundControl roundControl;
private GameState gameState;
private List<Connection> connections = new ArrayList<Connection>();
@@ -77,10 +77,23 @@ public class GameControl {
endOfRound();
}
});
+
+ roundControl.getRestartRoundEvent().add(new IListener() {
+
+ @Override
+ public void handle() {
+ restartRound();
+ }
+ });
roundControl.startRound();
}
+ private void restartRound() {
+ roundControl = null;
+ startRound();
+ }
+
private void endOfRound() {
roundControl = null;
view.enableWinPanel(true);
diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java
index e9e9453..8aed1ce 100644
--- a/src/jrummikub/control/RoundControl.java
+++ b/src/jrummikub/control/RoundControl.java
@@ -27,7 +27,7 @@ import jrummikub.view.IView;
* Controller that manages a single round of rummikub
*/
public class RoundControl {
- private IRoundState roundState;
+ IRoundState roundState;
private IView view;
private ITable clonedTable;
private Event restartRoundEvent = new Event();
@@ -70,12 +70,7 @@ public class RoundControl {
}
}));
- connections.add(view.getPlayerPanel().getRedealEvent().add(new IListener() {
- @Override
- public void handle() {
- redeal();
- }
- }));
+
prepareTurn();
}
@@ -91,13 +86,20 @@ public class RoundControl {
private void startTurn() {
TurnControl turnControl = new TurnControl(roundState.getActivePlayer()
.getHand(), clonedTable, view, roundState.getTurnNumber() < 1);
- connections.add(turnControl.getEndOfTurnEvent().add(new IListener() {
+ turnControl.getEndOfTurnEvent().add(new IListener() {
@Override
public void handle() {
endOfTurn();
}
- }));
+ });
+
+ turnControl.getRedealEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ redeal();
+ }
+ });
turnControl.startTurn();
}
@@ -322,7 +324,7 @@ public class RoundControl {
}
private void redeal() {
- for (Connection c : connections) {
+ for (Connection c : new ArrayList<Connection>(connections)) {
c.remove();
}
restartRoundEvent.emit();
diff --git a/src/jrummikub/control/SettingsControl.java b/src/jrummikub/control/SettingsControl.java
index 7b96e77..adcc369 100644
--- a/src/jrummikub/control/SettingsControl.java
+++ b/src/jrummikub/control/SettingsControl.java
@@ -1,8 +1,12 @@
package jrummikub.control;
+import java.awt.Color;
+
import jrummikub.model.GameSettings;
+import jrummikub.model.PlayerSettings;
import jrummikub.util.Event1;
import jrummikub.util.IEvent1;
+import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.view.IView;
@@ -31,7 +35,17 @@ public class SettingsControl {
* @Override public void handle(GameSettings settings) {
* startGame(settings); } });
*/
-
+ // TODO vvv this is just a temp. fix
+ view.getSettingsPanel().getStartGameEvent().add(new IListener() {
+
+ @Override
+ public void handle() {
+ GameSettings defaultSettings = new GameSettings();
+ defaultSettings.getPlayerList().add(new PlayerSettings("Foo", new Color(1.0f, 0, 0)));
+ defaultSettings.getPlayerList().add(new PlayerSettings("Bar", new Color(0, 1.0f, 0)));
+ startGame(defaultSettings);
+ }
+ });
view.showSettingsPanel(true);
}
diff --git a/src/jrummikub/control/TurnControl.java b/src/jrummikub/control/TurnControl.java
index 9ce08b0..1488c75 100644
--- a/src/jrummikub/control/TurnControl.java
+++ b/src/jrummikub/control/TurnControl.java
@@ -34,6 +34,7 @@ public class TurnControl {
private List<Stone> selectedStones = new ArrayList<Stone>();
private Event endOfTurnEvent = new Event();
+ private Event redealEvent = new Event();
private List<Connection> connections = new ArrayList<Connection>();
private boolean inspectOnly;
@@ -77,12 +78,19 @@ public class TurnControl {
@Override
public void handle() {
- endOfTurn();
+ endOfTurn(false);
}
};
connections.add(timer.getTimeRunOutEvent().add(endOfTurnListener));
connections.add(view.getPlayerPanel().getEndTurnEvent()
.add(endOfTurnListener));
+
+ connections.add(view.getPlayerPanel().getRedealEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ endOfTurn(true);
+ }
+ }));
addHandPanelHandlers();
addStoneCollectionHandlers();
@@ -441,9 +449,13 @@ public class TurnControl {
view.setSelectedStones(selectedStones);
}
- private void endOfTurn() {
+ private void endOfTurn(boolean redeal) {
timer.stopTimer();
- endOfTurnEvent.emit();
+ if (redeal) {
+ redealEvent.emit();
+ } else {
+ endOfTurnEvent.emit();
+ }
for (Connection c : connections) {
c.remove();
}
@@ -549,5 +561,14 @@ public class TurnControl {
}
}
}
+
+ /**
+ * Emitted when the round is aborted and needs to be restarted
+ *
+ * @return the event
+ */
+ public Event getRedealEvent() {
+ return redealEvent;
+ }
}
diff --git a/src/jrummikub/view/impl/SettingsPanel.java b/src/jrummikub/view/impl/SettingsPanel.java
index 03cfd4c..7200a16 100644
--- a/src/jrummikub/view/impl/SettingsPanel.java
+++ b/src/jrummikub/view/impl/SettingsPanel.java
@@ -31,6 +31,7 @@ import javax.swing.event.DocumentListener;
import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
+import jrummikub.util.Event;
import jrummikub.util.Event1;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
@@ -66,15 +67,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
private LinkedList<PlayerSettingsPanel> playerSettingsPanels = new LinkedList<PlayerSettingsPanel>();
private Event1<GameSettings> settingsChangeEvent = new Event1<GameSettings>();
- private Event1<GameSettings> startGameEvent = new Event1<GameSettings>();
-
- /*
- * @Override public IEvent1<GameSettings> getSettingsChangeEvent() { return
- * settingsChangeEvent; }
- *
- * @Override public IEvent1<GameSettings> getStartGameEvent() { return
- * startGameEvent; }
- */
+ private Event startGameEvent = new Event();
private void addPlayerSettings() {
// Find unused player name
@@ -177,7 +170,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
- startGameEvent.emit(gameSettings);
+ startGameEvent.emit();
}
});
add(startButton, c);
@@ -392,8 +385,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
@Override
public IEvent getStartGameEvent() {
- // TODO Auto-generated method stub
- return null;
+ return startGameEvent;
}
@Override
diff --git a/test/jrummikub/control/GameControlTest.java b/test/jrummikub/control/GameControlTest.java
new file mode 100644
index 0000000..706928c
--- /dev/null
+++ b/test/jrummikub/control/GameControlTest.java
@@ -0,0 +1,100 @@
+package jrummikub.control;
+
+import static jrummikub.model.StoneColor.RED;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.awt.Color;
+
+import jrummikub.model.GameSettings;
+import jrummikub.model.IHand;
+import jrummikub.model.PlayerSettings;
+import jrummikub.model.Position;
+import jrummikub.model.Stone;
+import jrummikub.util.Pair;
+import jrummikub.view.MockView;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests for the game control
+ */
+public class GameControlTest {
+ private GameControl gameControl;
+ private GameSettings gameSettings;
+ private MockView view;
+
+ /** */
+ @Before
+ public void setUp() {
+ view = new MockView();
+ gameSettings = new GameSettings();
+ gameSettings.getPlayerList().add(
+ new PlayerSettings("Foo", new Color(1.0f, 0, 0)));
+ gameSettings.getPlayerList().add(
+ new PlayerSettings("Bar", new Color(0, 1.0f, 0)));
+ gameControl = new GameControl(gameSettings, view);
+ }
+
+ /** */
+ @Test
+ public void testStartFirstRound() {
+ gameControl.startGame();
+ assertTrue(view.displayStartTurnPanel);
+ }
+
+ /** */
+ @Test
+ public void testEndOfRound() {
+ gameControl.startGame();
+ // Manipulate first players hand, to allow player1 to win
+ IHand playerHand = gameControl.roundControl.roundState
+ .getActivePlayer().getHand();
+ for (Pair<Stone, Position> entry : playerHand.clone()) {
+ playerHand.pickUp(entry.getFirst());
+ }
+ Stone stone1 = new Stone(9, RED);
+ Stone stone2 = new Stone(10, RED);
+ Stone stone3 = new Stone(11, RED);
+ playerHand.drop(stone1, new Position(0, 0));
+ playerHand.drop(stone2, new Position(0, 0));
+ playerHand.drop(stone3, new Position(0, 0));
+ // Done setting up first players hand
+
+ view.startTurnEvent.emit();
+ view.playerPanel.endTurnEvent.emit();
+ view.startTurnEvent.emit();
+ view.playerPanel.endTurnEvent.emit();
+ view.startTurnEvent.emit();
+
+ view.handPanel.stoneClickEvent.emit(stone1, false);
+ view.handPanel.stoneClickEvent.emit(stone2, true);
+ view.handPanel.stoneClickEvent.emit(stone3, true);
+
+ view.tablePanel.clickEvent.emit(new Position(0, 0));
+
+ view.playerPanel.endTurnEvent.emit();
+ assertTrue(view.displayWinPanel);
+
+ view.newRoundEvent.emit();
+ assertTrue(view.displayStartTurnPanel);
+ }
+
+ /** */
+ @Test
+ public void testRedealing() {
+ gameControl.startGame();
+ view.startTurnEvent.emit();
+ PlayerSettings firstPlayer = gameControl.roundControl.roundState
+ .getActivePlayer().getPlayerSettings();
+ view.playerPanel.endTurnEvent.emit();
+ view.startTurnEvent.emit();
+ view.playerPanel.redealEvent.emit();
+ assertTrue(view.displayStartTurnPanel);
+ view.startTurnEvent.emit();
+ assertSame(firstPlayer, gameControl.roundControl.roundState
+ .getActivePlayer().getPlayerSettings());
+
+ }
+}