diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-06-19 19:46:06 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-06-19 19:46:06 +0200 |
commit | 45656861ab2e618938764f0c46f830184099a71d (patch) | |
tree | 8e5cd9ab28d47f242408d6dcfc29317902048605 /src/jrummikub/control/RoundControl.java | |
parent | 9b7aac51f58eb628978ed0e4d1a922a959917a3b (diff) | |
download | JRummikub-45656861ab2e618938764f0c46f830184099a71d.tar JRummikub-45656861ab2e618938764f0c46f830184099a71d.zip |
Added NetworkRoundControlTest and started NetworkRoundControl and NetworkTurnControl implementation
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@491 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/control/RoundControl.java')
-rw-r--r-- | src/jrummikub/control/RoundControl.java | 120 |
1 files changed, 65 insertions, 55 deletions
diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java index d8435af..a19a2ab 100644 --- a/src/jrummikub/control/RoundControl.java +++ b/src/jrummikub/control/RoundControl.java @@ -8,6 +8,8 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import jrummikub.control.turn.AIControl; +import jrummikub.control.turn.HumanTurnControl; import jrummikub.control.turn.ITurnControl; import jrummikub.control.turn.TurnControlFactory; import jrummikub.control.turn.TurnMode; @@ -16,6 +18,7 @@ import jrummikub.model.IHand; import jrummikub.model.IPlayer; import jrummikub.model.IRoundState; import jrummikub.model.ITable; +import jrummikub.model.PlayerSettings; import jrummikub.model.Position; import jrummikub.model.Score; import jrummikub.model.Stone; @@ -35,15 +38,15 @@ import jrummikub.view.IView.BottomPanelType; * Controller that manages a single round of rummikub */ public class RoundControl { - IRoundState roundState; + private ITurnControl turnControl; + protected IRoundState roundState; private IView view; private ITable clonedTable; IHand clonedHand; private Event restartRoundEvent = new Event(); + private Event1<IRoundState> roundStateUpdateEvent = new Event1<IRoundState>(); private Event1<Score> endOfRoundEvent = new Event1<Score>(); - private Event1<ITable> tableUpdateEvent = new Event1<ITable>(); - private List<Connection> connections = new ArrayList<Connection>(); - private ITurnControl turnControl; + protected List<Connection> connections = new ArrayList<Connection>(); private boolean roundFinished; private boolean lastTurnNotEnoughPoints; private boolean lastTurnMeldError; @@ -61,6 +64,10 @@ public class RoundControl { this.view = view; } + public IEvent1<IRoundState> getRoundStateUpdateEvent() { + return roundStateUpdateEvent; + } + /** * End the round * @@ -70,10 +77,6 @@ public class RoundControl { return endOfRoundEvent; } - public IEvent1<ITable> getTableUpdateEvent() { - return tableUpdateEvent; - } - /** * Begin the round */ @@ -122,16 +125,11 @@ public class RoundControl { clonedTable = (ITable) roundState.getTable().clone(); clonedHand = (IHand) roundState.getActivePlayer().getHand().clone(); - if (!oneHuman) { - view.setBottomPanel(isHuman ? BottomPanelType.START_TURN_PANEL - : BottomPanelType.COMPUTER_HAND_PANEL); + if (isHuman) { + view.setBottomPanel(oneHuman ? BottomPanelType.HUMAN_HAND_PANEL + : BottomPanelType.START_TURN_PANEL); } else { - if (!isHuman) { - view.setBottomPanel(BottomPanelType.COMPUTER_HAND_PANEL); - } else { - view.setBottomPanel(BottomPanelType.HUMAN_HAND_PANEL); - startTurn(); - } + view.setBottomPanel(BottomPanelType.NONHUMAN_HAND_PANEL); } view.getTablePanel().setStoneSets(clonedTable.clone()); @@ -141,13 +139,17 @@ public class RoundControl { .getColor()); view.setCurrentPlayerHasLaidOut(roundState.getActivePlayer().getLaidOut()); - if (!isHuman) + turnControl = createTurnControl(roundState.getActivePlayer() + .getPlayerSettings()); + + boolean isAI = (turnControl instanceof AIControl); + + if (isAI || (isHuman && oneHuman)) { startTurn(); + } } - private void startTurn() { - if (turnControl != null) - return; + protected void startTurn() { boolean isHuman = roundState.getActivePlayer().getPlayerSettings() .getType() == HUMAN; @@ -164,34 +166,35 @@ public class RoundControl { if (isHuman) { view.getPlayerPanel().setEndTurnMode(turnMode); } - turnControl = TurnControlFactory.getFactory( - roundState.getActivePlayer().getPlayerSettings().getType()).create(); + turnControl.setup(new ITurnControl.TurnInfo(clonedTable, clonedHand, roundState.getActivePlayer().getLaidOut(), turnMode), roundState .getGameSettings(), view); - turnControl.getEndOfTurnEvent().add(new IListener() { + turnControl.getEndOfTurnEvent().add(new IListener1<ITable>() { @Override - public void handle() { - endOfTurn(); + public void handle(ITable newTable) { + endOfTurn(newTable); } }); - - turnControl.getTableUpdateEvent().add(new IListener1<ITable>() { - @Override - public void handle(ITable table) { - tableUpdateEvent.emit(table); - } - }); - turnControl.getRedealEvent().add(new IListener() { @Override public void handle() { redeal(); } }); + addTurnControlListeners(turnControl); + turnControl.startTurn(); } + /** Override this */ + protected void addTurnControlListeners(ITurnControl turnControl) { + } + + protected ITurnControl createTurnControl(PlayerSettings playerSettings) { + return TurnControlFactory.getFactory(playerSettings.getType()).create(); + } + void deal() { for (int i = 0; i < roundState.getPlayerCount(); i++) { IHand hand = roundState.getNthNextPlayer(i).getHand(); @@ -214,37 +217,44 @@ public class RoundControl { || totalValue >= roundState.getGameSettings().getInitialMeldThreshold(); } - private void endOfTurn() { + private void endOfTurn(ITable newTable) { + boolean wasHuman = (turnControl instanceof HumanTurnControl); + boolean wasAI = (turnControl instanceof AIControl); + turnControl = null; + roundState.getActivePlayer().setHand(clonedHand); boolean goToNextPlayer = true; lastTurnNotEnoughPoints = false; lastTurnMeldError = false; if (roundState.getTurnNumber() >= 1) { - goToNextPlayer = checkTurn(); + goToNextPlayer = checkTurn(newTable); } - if (goToNextPlayer) { + if (goToNextPlayer || wasAI) { nextPlayer(); } else { - view.setBottomPanel(BottomPanelType.INVALID_TURN_PANEL); + if (wasHuman) { + view.setBottomPanel(BottomPanelType.INVALID_TURN_PANEL); + } + if (lastTurnNotEnoughPoints) { view.setInitialMeldError(roundState.getGameSettings() .getInitialMeldThreshold()); view.setInvalidStoneSets(tableSetDifference(roundState.getTable(), - clonedTable)); + newTable)); } else if (lastTurnMeldError) { view.setInitialMeldFirstError(); - view.setInvalidStoneSets(touchedStoneSets()); + view.setInvalidStoneSets(touchedStoneSets(newTable)); } else { view.setStoneCollectionHidden(true); - view.setInvalidStoneSets(invalidStoneSets()); + view.setInvalidStoneSets(invalidStoneSets(newTable)); } } } - private List<StoneSet> invalidStoneSets() { + private List<StoneSet> invalidStoneSets(ITable newTable) { List<StoneSet> invalidSets = new ArrayList<StoneSet>(); - for (Pair<StoneSet, Position> set : clonedTable) { + for (Pair<StoneSet, Position> set : newTable) { if (set.getFirst().isValid(roundState.getGameSettings())) { continue; } @@ -253,9 +263,9 @@ public class RoundControl { return invalidSets; } - private List<StoneSet> touchedStoneSets() { + private List<StoneSet> touchedStoneSets(ITable newTable) { List<StoneSet> touchedSets = new ArrayList<StoneSet>(); - for (StoneSet set : tableSetDifference(roundState.getTable(), clonedTable)) { + for (StoneSet set : tableSetDifference(roundState.getTable(), newTable)) { for (Stone stone : set) { if (!roundState.getActivePlayer().getHand().contains(stone)) { touchedSets.add(set); @@ -293,27 +303,27 @@ public class RoundControl { } } - private boolean checkTurn() { - if (!clonedTable.isValid()) { - rejectMove(); + private boolean checkTurn(ITable newTable) { + if (!newTable.isValid()) { + rejectMove(newTable); return false; } if (!roundState.getActivePlayer().getLaidOut()) { // Player touched forbidden stones - if (!tableSetDifference(clonedTable, roundState.getTable()).isEmpty()) { - rejectMove(); + if (!tableSetDifference(newTable, roundState.getTable()).isEmpty()) { + rejectMove(newTable); lastTurnMeldError = true; return false; } if (!laidOutValidPoints()) { - rejectMove(); + rejectMove(newTable); lastTurnNotEnoughPoints = true; return false; } } - Set<Stone> tableDiff = tableDifference(roundState.getTable(), clonedTable); + Set<Stone> tableDiff = tableDifference(roundState.getTable(), newTable); - roundState.setTable(clonedTable); + roundState.setTable(newTable); if (tableDiff.isEmpty()) { // Player hasn't made a move @@ -327,8 +337,8 @@ public class RoundControl { return true; } - private void rejectMove() { - Set<Stone> tableDiff = tableDifference(roundState.getTable(), clonedTable); + private void rejectMove(ITable newTable) { + Set<Stone> tableDiff = tableDifference(roundState.getTable(), newTable); // deal penalty, reset roundState.getGameHeap().putBack(tableDiff); dealPenalty(tableDiff.size()); |