summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/RoundControl.java
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2011-06-20 06:41:15 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2011-06-20 06:41:15 +0200
commit0c3eb9a28363ae697f984d9413eb187bfe2511a9 (patch)
treee72cc28d72bd27c0dad9372d3b51274af9ffdeed /src/jrummikub/control/RoundControl.java
parent74d8205f30732a8afd6aa45f859188b0a3d447e5 (diff)
downloadJRummikub-0c3eb9a28363ae697f984d9413eb187bfe2511a9.tar
JRummikub-0c3eb9a28363ae697f984d9413eb187bfe2511a9.zip
Fixed a bunch of network synchronization bugs
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@510 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/control/RoundControl.java')
-rw-r--r--src/jrummikub/control/RoundControl.java81
1 files changed, 46 insertions, 35 deletions
diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java
index ba0fa2a..63ec1a2 100644
--- a/src/jrummikub/control/RoundControl.java
+++ b/src/jrummikub/control/RoundControl.java
@@ -18,7 +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.PlayerSettings.Type;
import jrummikub.model.Position;
import jrummikub.model.Score;
import jrummikub.model.Stone;
@@ -29,7 +29,7 @@ import jrummikub.util.Event1;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.util.IListener;
-import jrummikub.util.IListener1;
+import jrummikub.util.IListener3;
import jrummikub.util.Pair;
import jrummikub.view.IView;
import jrummikub.view.IView.BottomPanelType;
@@ -153,14 +153,11 @@ public class RoundControl {
.getType() == HUMAN;
boolean oneHuman = roundState.getGameSettings().oneHuman();
- clonedTable = (ITable) roundState.getTable().clone();
- clonedHand = (IHand) roundState.getActivePlayer().getHand().clone();
-
if (isHuman && !oneHuman) {
view.setBottomPanel(BottomPanelType.START_TURN_PANEL);
}
- view.getTablePanel().setStoneSets(clonedTable.clone());
+ view.getTablePanel().setStoneSets(roundState.getTable().clone());
view.setCurrentPlayerName(roundState.getActivePlayer().getPlayerSettings()
.getName());
view.setCurrentPlayerColor(roundState.getActivePlayer().getPlayerSettings()
@@ -168,7 +165,7 @@ public class RoundControl {
view.setCurrentPlayerHasLaidOut(roundState.getActivePlayer().getLaidOut());
turnControl = createTurnControl(roundState.getActivePlayer()
- .getPlayerSettings());
+ .getPlayerSettings().getType());
}
protected void startTurn() {
@@ -184,6 +181,11 @@ public class RoundControl {
TurnMode turnMode = TurnMode.NORMAL_TURN;
+ view.getTablePanel().setStoneSets(roundState.getTable().clone());
+
+ clonedTable = (ITable) roundState.getTable().clone();
+ clonedHand = (IHand) roundState.getActivePlayer().getHand().clone();
+
if (roundState.getTurnNumber() < 1) {
view.setStoneCollectionHidden(true);
turnMode = TurnMode.INSPECT_ONLY;
@@ -199,12 +201,20 @@ public class RoundControl {
turnControl.setup(new ITurnControl.TurnInfo(clonedTable, clonedHand,
roundState.getActivePlayer().getLaidOut(), turnMode), roundState
.getGameSettings(), view);
- turnControl.getEndOfTurnEvent().add(new IListener1<ITable>() {
- @Override
- public void handle(ITable newTable) {
- endOfTurn(newTable);
- }
- });
+ turnControl.getEndOfTurnEvent().add(
+ new IListener3<IHand, ITable, ITable>() {
+ @Override
+ public void handle(IHand oldHand, ITable oldTable, ITable newTable) {
+ if (oldHand == null) {
+ oldHand = roundState.getActivePlayer().getHand();
+ }
+ if (oldTable == null) {
+ oldTable = roundState.getTable();
+ }
+
+ endOfTurn(oldHand, oldTable, newTable);
+ }
+ });
turnControl.getRedealEvent().add(new IListener() {
@Override
public void handle() {
@@ -233,8 +243,8 @@ public class RoundControl {
protected void addTurnControlListeners(ITurnControl turnControl) {
}
- protected ITurnControl createTurnControl(PlayerSettings playerSettings) {
- return TurnControlFactory.getFactory(playerSettings.getType()).create();
+ protected ITurnControl createTurnControl(Type type) {
+ return TurnControlFactory.getFactory(type).create();
}
void deal() {
@@ -248,9 +258,8 @@ public class RoundControl {
view.getSidePanel().setHeapSize(roundState.getGameHeap().getSize());
}
- private boolean laidOutValidPoints() {
- List<StoneSet> newSets = tableSetDifference(roundState.getTable(),
- clonedTable);
+ private boolean laidOutValidPoints(ITable oldTable, ITable newTable) {
+ List<StoneSet> newSets = tableSetDifference(oldTable, newTable);
int totalValue = 0;
for (StoneSet set : newSets) {
@@ -261,10 +270,12 @@ public class RoundControl {
|| totalValue >= roundState.getGameSettings().getInitialMeldThreshold();
}
- protected void endOfTurn(ITable newTable) {
+ protected void endOfTurn(IHand oldHand, ITable oldTable, ITable newTable) {
boolean wasHuman = (turnControl instanceof HumanTurnControl);
boolean wasAI = (turnControl instanceof AIControl);
+ view.setBottomPanel(BottomPanelType.NONHUMAN_HAND_PANEL);
+
turnControl = null;
roundState.getActivePlayer().setLastTurnInvalid(false);
@@ -278,7 +289,7 @@ public class RoundControl {
lastTurnNotEnoughPoints = false;
lastTurnMeldError = false;
if (roundState.getTurnNumber() >= 1) {
- goToNextPlayer = checkTurn(newTable);
+ goToNextPlayer = checkTurn(oldTable, newTable);
}
if (goToNextPlayer || wasAI) {
nextPlayer();
@@ -290,11 +301,10 @@ public class RoundControl {
if (lastTurnNotEnoughPoints) {
view.setInitialMeldError(roundState.getGameSettings()
.getInitialMeldThreshold());
- view.setInvalidStoneSets(tableSetDifference(roundState.getTable(),
- newTable));
+ view.setInvalidStoneSets(tableSetDifference(oldTable, newTable));
} else if (lastTurnMeldError) {
view.setInitialMeldFirstError();
- view.setInvalidStoneSets(touchedStoneSets(newTable));
+ view.setInvalidStoneSets(touchedStoneSets(oldHand, oldTable, newTable));
} else {
view.setStoneCollectionHidden(true);
view.setInvalidStoneSets(invalidStoneSets(newTable));
@@ -313,11 +323,12 @@ public class RoundControl {
return invalidSets;
}
- private List<StoneSet> touchedStoneSets(ITable newTable) {
+ private List<StoneSet> touchedStoneSets(IHand oldHand, ITable oldTable,
+ ITable newTable) {
List<StoneSet> touchedSets = new ArrayList<StoneSet>();
- for (StoneSet set : tableSetDifference(roundState.getTable(), newTable)) {
+ for (StoneSet set : tableSetDifference(oldTable, newTable)) {
for (Stone stone : set) {
- if (!roundState.getActivePlayer().getHand().contains(stone)) {
+ if (!oldHand.contains(stone)) {
touchedSets.add(set);
break;
}
@@ -353,25 +364,25 @@ public class RoundControl {
}
}
- private boolean checkTurn(ITable newTable) {
+ private boolean checkTurn(ITable oldTable, ITable newTable) {
if (!newTable.isValid()) {
- rejectMove(newTable);
+ rejectMove(oldTable, newTable);
return false;
}
if (!roundState.getActivePlayer().getLaidOut()) {
// Player touched forbidden stones
- if (!tableSetDifference(newTable, roundState.getTable()).isEmpty()) {
- rejectMove(newTable);
+ if (!tableSetDifference(newTable, oldTable).isEmpty()) {
+ rejectMove(oldTable, newTable);
lastTurnMeldError = true;
return false;
}
- if (!laidOutValidPoints()) {
- rejectMove(newTable);
+ if (!laidOutValidPoints(oldTable, newTable)) {
+ rejectMove(oldTable, newTable);
lastTurnNotEnoughPoints = true;
return false;
}
}
- Set<Stone> tableDiff = tableDifference(roundState.getTable(), newTable);
+ Set<Stone> tableDiff = tableDifference(oldTable, newTable);
roundState.setTable(newTable);
@@ -387,8 +398,8 @@ public class RoundControl {
return true;
}
- private void rejectMove(ITable newTable) {
- Set<Stone> tableDiff = tableDifference(roundState.getTable(), newTable);
+ private void rejectMove(ITable oldTable, ITable newTable) {
+ Set<Stone> tableDiff = tableDifference(oldTable, newTable);
// deal penalty, reset
roundState.getGameHeap().putBack(tableDiff);
roundState.getActivePlayer().setLastTurnInvalid(true);