GameControl tests, temporary handler in SettingsControl and small
fixes git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@288 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
63397e2f5f
commit
2d198820a9
6 changed files with 169 additions and 27 deletions
|
@ -19,7 +19,7 @@ import jrummikub.view.IView;
|
||||||
public class GameControl {
|
public class GameControl {
|
||||||
private GameSettings gameSettings;
|
private GameSettings gameSettings;
|
||||||
private IView view;
|
private IView view;
|
||||||
private RoundControl roundControl;
|
RoundControl roundControl;
|
||||||
private GameState gameState;
|
private GameState gameState;
|
||||||
private List<Connection> connections = new ArrayList<Connection>();
|
private List<Connection> connections = new ArrayList<Connection>();
|
||||||
|
|
||||||
|
@ -78,9 +78,22 @@ public class GameControl {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
roundControl.getRestartRoundEvent().add(new IListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle() {
|
||||||
|
restartRound();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
roundControl.startRound();
|
roundControl.startRound();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void restartRound() {
|
||||||
|
roundControl = null;
|
||||||
|
startRound();
|
||||||
|
}
|
||||||
|
|
||||||
private void endOfRound() {
|
private void endOfRound() {
|
||||||
roundControl = null;
|
roundControl = null;
|
||||||
view.enableWinPanel(true);
|
view.enableWinPanel(true);
|
||||||
|
|
|
@ -27,7 +27,7 @@ import jrummikub.view.IView;
|
||||||
* Controller that manages a single round of rummikub
|
* Controller that manages a single round of rummikub
|
||||||
*/
|
*/
|
||||||
public class RoundControl {
|
public class RoundControl {
|
||||||
private IRoundState roundState;
|
IRoundState roundState;
|
||||||
private IView view;
|
private IView view;
|
||||||
private ITable clonedTable;
|
private ITable clonedTable;
|
||||||
private Event restartRoundEvent = new Event();
|
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();
|
prepareTurn();
|
||||||
}
|
}
|
||||||
|
@ -91,13 +86,20 @@ public class RoundControl {
|
||||||
private void startTurn() {
|
private void startTurn() {
|
||||||
TurnControl turnControl = new TurnControl(roundState.getActivePlayer()
|
TurnControl turnControl = new TurnControl(roundState.getActivePlayer()
|
||||||
.getHand(), clonedTable, view, roundState.getTurnNumber() < 1);
|
.getHand(), clonedTable, view, roundState.getTurnNumber() < 1);
|
||||||
connections.add(turnControl.getEndOfTurnEvent().add(new IListener() {
|
turnControl.getEndOfTurnEvent().add(new IListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle() {
|
public void handle() {
|
||||||
endOfTurn();
|
endOfTurn();
|
||||||
}
|
}
|
||||||
}));
|
});
|
||||||
|
|
||||||
|
turnControl.getRedealEvent().add(new IListener() {
|
||||||
|
@Override
|
||||||
|
public void handle() {
|
||||||
|
redeal();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
turnControl.startTurn();
|
turnControl.startTurn();
|
||||||
}
|
}
|
||||||
|
@ -322,7 +324,7 @@ public class RoundControl {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void redeal() {
|
private void redeal() {
|
||||||
for (Connection c : connections) {
|
for (Connection c : new ArrayList<Connection>(connections)) {
|
||||||
c.remove();
|
c.remove();
|
||||||
}
|
}
|
||||||
restartRoundEvent.emit();
|
restartRoundEvent.emit();
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
package jrummikub.control;
|
package jrummikub.control;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
|
||||||
import jrummikub.model.GameSettings;
|
import jrummikub.model.GameSettings;
|
||||||
|
import jrummikub.model.PlayerSettings;
|
||||||
import jrummikub.util.Event1;
|
import jrummikub.util.Event1;
|
||||||
import jrummikub.util.IEvent1;
|
import jrummikub.util.IEvent1;
|
||||||
|
import jrummikub.util.IListener;
|
||||||
import jrummikub.util.IListener1;
|
import jrummikub.util.IListener1;
|
||||||
import jrummikub.view.IView;
|
import jrummikub.view.IView;
|
||||||
|
|
||||||
|
@ -31,7 +35,17 @@ public class SettingsControl {
|
||||||
* @Override public void handle(GameSettings settings) {
|
* @Override public void handle(GameSettings settings) {
|
||||||
* startGame(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);
|
view.showSettingsPanel(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ public class TurnControl {
|
||||||
private List<Stone> selectedStones = new ArrayList<Stone>();
|
private List<Stone> selectedStones = new ArrayList<Stone>();
|
||||||
|
|
||||||
private Event endOfTurnEvent = new Event();
|
private Event endOfTurnEvent = new Event();
|
||||||
|
private Event redealEvent = new Event();
|
||||||
private List<Connection> connections = new ArrayList<Connection>();
|
private List<Connection> connections = new ArrayList<Connection>();
|
||||||
|
|
||||||
private boolean inspectOnly;
|
private boolean inspectOnly;
|
||||||
|
@ -77,13 +78,20 @@ public class TurnControl {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle() {
|
public void handle() {
|
||||||
endOfTurn();
|
endOfTurn(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
connections.add(timer.getTimeRunOutEvent().add(endOfTurnListener));
|
connections.add(timer.getTimeRunOutEvent().add(endOfTurnListener));
|
||||||
connections.add(view.getPlayerPanel().getEndTurnEvent()
|
connections.add(view.getPlayerPanel().getEndTurnEvent()
|
||||||
.add(endOfTurnListener));
|
.add(endOfTurnListener));
|
||||||
|
|
||||||
|
connections.add(view.getPlayerPanel().getRedealEvent().add(new IListener() {
|
||||||
|
@Override
|
||||||
|
public void handle() {
|
||||||
|
endOfTurn(true);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
addHandPanelHandlers();
|
addHandPanelHandlers();
|
||||||
addStoneCollectionHandlers();
|
addStoneCollectionHandlers();
|
||||||
if (!inspectOnly)
|
if (!inspectOnly)
|
||||||
|
@ -441,9 +449,13 @@ public class TurnControl {
|
||||||
view.setSelectedStones(selectedStones);
|
view.setSelectedStones(selectedStones);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void endOfTurn() {
|
private void endOfTurn(boolean redeal) {
|
||||||
timer.stopTimer();
|
timer.stopTimer();
|
||||||
|
if (redeal) {
|
||||||
|
redealEvent.emit();
|
||||||
|
} else {
|
||||||
endOfTurnEvent.emit();
|
endOfTurnEvent.emit();
|
||||||
|
}
|
||||||
for (Connection c : connections) {
|
for (Connection c : connections) {
|
||||||
c.remove();
|
c.remove();
|
||||||
}
|
}
|
||||||
|
@ -550,4 +562,13 @@ public class TurnControl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emitted when the round is aborted and needs to be restarted
|
||||||
|
*
|
||||||
|
* @return the event
|
||||||
|
*/
|
||||||
|
public Event getRedealEvent() {
|
||||||
|
return redealEvent;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@ import javax.swing.event.DocumentListener;
|
||||||
|
|
||||||
import jrummikub.model.GameSettings;
|
import jrummikub.model.GameSettings;
|
||||||
import jrummikub.model.PlayerSettings;
|
import jrummikub.model.PlayerSettings;
|
||||||
|
import jrummikub.util.Event;
|
||||||
import jrummikub.util.Event1;
|
import jrummikub.util.Event1;
|
||||||
import jrummikub.util.IEvent;
|
import jrummikub.util.IEvent;
|
||||||
import jrummikub.util.IEvent1;
|
import jrummikub.util.IEvent1;
|
||||||
|
@ -66,15 +67,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
|
||||||
private LinkedList<PlayerSettingsPanel> playerSettingsPanels = new LinkedList<PlayerSettingsPanel>();
|
private LinkedList<PlayerSettingsPanel> playerSettingsPanels = new LinkedList<PlayerSettingsPanel>();
|
||||||
|
|
||||||
private Event1<GameSettings> settingsChangeEvent = new Event1<GameSettings>();
|
private Event1<GameSettings> settingsChangeEvent = new Event1<GameSettings>();
|
||||||
private Event1<GameSettings> startGameEvent = new Event1<GameSettings>();
|
private Event startGameEvent = new Event();
|
||||||
|
|
||||||
/*
|
|
||||||
* @Override public IEvent1<GameSettings> getSettingsChangeEvent() { return
|
|
||||||
* settingsChangeEvent; }
|
|
||||||
*
|
|
||||||
* @Override public IEvent1<GameSettings> getStartGameEvent() { return
|
|
||||||
* startGameEvent; }
|
|
||||||
*/
|
|
||||||
|
|
||||||
private void addPlayerSettings() {
|
private void addPlayerSettings() {
|
||||||
// Find unused player name
|
// Find unused player name
|
||||||
|
@ -177,7 +170,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
|
||||||
startButton.addActionListener(new ActionListener() {
|
startButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
startGameEvent.emit(gameSettings);
|
startGameEvent.emit();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
add(startButton, c);
|
add(startButton, c);
|
||||||
|
@ -392,8 +385,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IEvent getStartGameEvent() {
|
public IEvent getStartGameEvent() {
|
||||||
// TODO Auto-generated method stub
|
return startGameEvent;
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
100
test/jrummikub/control/GameControlTest.java
Normal file
100
test/jrummikub/control/GameControlTest.java
Normal file
|
@ -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());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue