Don't duplicate round scores in network games

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@549 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-06-21 16:43:59 +02:00
parent 39ba50e1ac
commit 81de4ad99f

View file

@ -9,7 +9,6 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import jrummikub.control.turn.AIControl; import jrummikub.control.turn.AIControl;
import jrummikub.control.turn.HumanTurnControl;
import jrummikub.control.turn.ITurnControl; import jrummikub.control.turn.ITurnControl;
import jrummikub.control.turn.TurnControlFactory; import jrummikub.control.turn.TurnControlFactory;
import jrummikub.control.turn.TurnMode; import jrummikub.control.turn.TurnMode;
@ -45,20 +44,19 @@ public class RoundControl {
/** There are invalid set(s) on the table */ /** There are invalid set(s) on the table */
INVALID_SETS, INVALID_SETS,
/** /**
* The player tried to modify the table without providing the initial * The player tried to modify the table without providing the initial meld
* meld threshold first * threshold first
*/ */
INITIAL_MELD_ERROR, INITIAL_MELD_ERROR,
/** /**
* The player didn't provide enough points for the initial meld * The player didn't provide enough points for the initial meld threshold
* threshold
*/ */
NOT_ENOUGH_POINTS NOT_ENOUGH_POINTS
} }
/** /**
* Table, stone sets and type of an invalid turn to allow a user to track * Table, stone sets and type of an invalid turn to allow a user to track his
* his own errors * own errors
*/ */
public static class InvalidTurnInfo implements Serializable { public static class InvalidTurnInfo implements Serializable {
private static final long serialVersionUID = -3591000741414366776L; private static final long serialVersionUID = -3591000741414366776L;
@ -71,11 +69,11 @@ public class RoundControl {
* Creates new InvalidTurnInfo * Creates new InvalidTurnInfo
* *
* @param table * @param table
* the table after the turn * the table after the turn
* @param type * @param type
* the type of the invalid turn * the type of the invalid turn
* @param invalidSets * @param invalidSets
* the sets causing the turn to be invalid * the sets causing the turn to be invalid
*/ */
public InvalidTurnInfo(ITable table, InvalidTurnType type, public InvalidTurnInfo(ITable table, InvalidTurnType type,
Collection<StoneSet> invalidSets) { Collection<StoneSet> invalidSets) {
@ -119,7 +117,6 @@ public class RoundControl {
private Event1<IRoundState> roundStateUpdateEvent = new Event1<IRoundState>(); private Event1<IRoundState> roundStateUpdateEvent = new Event1<IRoundState>();
private Event1<Score> endOfRoundEvent = new Event1<Score>(); private Event1<Score> endOfRoundEvent = new Event1<Score>();
protected List<Connection> connections = new ArrayList<Connection>(); protected List<Connection> connections = new ArrayList<Connection>();
private boolean roundFinished;
private boolean mayPause; private boolean mayPause;
public RoundControl(IRoundState roundState, IView view) { public RoundControl(IRoundState roundState, IView view) {
@ -130,9 +127,9 @@ public class RoundControl {
* Create a new RoundControl using the given gameState and view * Create a new RoundControl using the given gameState and view
* *
* @param roundState * @param roundState
* initial round state * initial round state
* @param view * @param view
* view used for user interaction * view used for user interaction
*/ */
protected RoundControl(IRoundState roundState, IView view, boolean mayPause) { protected RoundControl(IRoundState roundState, IView view, boolean mayPause) {
this.roundState = roundState; this.roundState = roundState;
@ -208,7 +205,7 @@ public class RoundControl {
* Sets the current round state * Sets the current round state
* *
* @param state * @param state
* to be set * to be set
*/ */
protected void setRoundState(IRoundState state) { protected void setRoundState(IRoundState state) {
roundState = state; roundState = state;
@ -247,12 +244,11 @@ public class RoundControl {
} }
view.getTablePanel().setStoneSets(roundState.getTable().clone()); view.getTablePanel().setStoneSets(roundState.getTable().clone());
view.setCurrentPlayerName(roundState.getActivePlayer() view.setCurrentPlayerName(roundState.getActivePlayer().getPlayerSettings()
.getPlayerSettings().getName()); .getName());
view.setCurrentPlayerColor(roundState.getActivePlayer() view.setCurrentPlayerColor(roundState.getActivePlayer().getPlayerSettings()
.getPlayerSettings().getColor()); .getColor());
view.setCurrentPlayerHasLaidOut(roundState.getActivePlayer() view.setCurrentPlayerHasLaidOut(roundState.getActivePlayer().getLaidOut());
.getLaidOut());
turnControl = createTurnControl(roundState.getActivePlayer() turnControl = createTurnControl(roundState.getActivePlayer()
.getPlayerSettings().getType()); .getPlayerSettings().getType());
@ -331,7 +327,7 @@ public class RoundControl {
* Override this * Override this
* *
* @param turnControl * @param turnControl
* current turn control * current turn control
*/ */
protected void addTurnControlListeners(ITurnControl turnControl) { protected void addTurnControlListeners(ITurnControl turnControl) {
} }
@ -340,7 +336,7 @@ public class RoundControl {
* Creates new turn control of the specified type * Creates new turn control of the specified type
* *
* @param type * @param type
* of the new turn control * of the new turn control
* @return the new turn control * @return the new turn control
*/ */
protected ITurnControl createTurnControl(Type type) { protected ITurnControl createTurnControl(Type type) {
@ -354,10 +350,8 @@ public class RoundControl {
protected void deal() { protected void deal() {
for (int i = 0; i < roundState.getPlayerCount(); i++) { for (int i = 0; i < roundState.getPlayerCount(); i++) {
IHand hand = roundState.getNthNextPlayer(i).getHand(); IHand hand = roundState.getNthNextPlayer(i).getHand();
for (int j = 0; j < roundState.getGameSettings() for (int j = 0; j < roundState.getGameSettings().getNumberOfStonesDealt(); j++) {
.getNumberOfStonesDealt(); j++) { hand.drop(roundState.getGameHeap().drawStone(), new Position(0, 0));
hand.drop(roundState.getGameHeap().drawStone(), new Position(0,
0));
} }
} }
@ -368,54 +362,47 @@ public class RoundControl {
* End the players turn * End the players turn
* *
* @param invalidTurnInfo * @param invalidTurnInfo
* info about the player's last turn * info about the player's last turn
*/ */
protected void endOfTurn(InvalidTurnInfo invalidTurnInfo) { protected void endOfTurn(InvalidTurnInfo invalidTurnInfo) {
boolean wasAI = turnControl instanceof AIControl; boolean isHuman = roundState.getActivePlayer().getPlayerSettings()
boolean wasHuman = turnControl instanceof HumanTurnControl; .getType() == Type.HUMAN;
turnControl = null; turnControl = null;
view.getTablePanel().setStoneSets(invalidTurnInfo.getTable()); view.getTablePanel().setStoneSets(invalidTurnInfo.getTable());
if (invalidTurnInfo.getType() != null) { if (invalidTurnInfo.getType() != null) {
if (wasHuman) { if (isHuman) {
view.setBottomPanel(BottomPanelType.INVALID_TURN_PANEL); view.setBottomPanel(BottomPanelType.INVALID_TURN_PANEL);
} }
view.setInvalidStoneSets(invalidTurnInfo.getInvalidSets()); view.setInvalidStoneSets(invalidTurnInfo.getInvalidSets());
switch (invalidTurnInfo.getType()) { switch (invalidTurnInfo.getType()) {
case INITIAL_MELD_ERROR: case INITIAL_MELD_ERROR:
view.setInitialMeldFirstError(); view.setInitialMeldFirstError();
break; break;
case INVALID_SETS: case INVALID_SETS:
view.setStoneCollectionHidden(true); view.setStoneCollectionHidden(true);
break; break;
case NOT_ENOUGH_POINTS: case NOT_ENOUGH_POINTS:
view.setInitialMeldError(roundState.getGameSettings() view.setInitialMeldError(roundState.getGameSettings()
.getInitialMeldThreshold()); .getInitialMeldThreshold());
break; break;
} }
if (wasAI) { if (!isHuman) {
nextPlayer(); nextPlayer();
} }
return; return;
} }
if (roundState.getActivePlayer().getHand().getSize() == 0) {
endOfRound();
return;
}
view.setBottomPanel(BottomPanelType.NONHUMAN_HAND_PANEL); view.setBottomPanel(BottomPanelType.NONHUMAN_HAND_PANEL);
view.getPlayerPanel().setTime(roundState.getGameSettings().getTotalTime(), view.getPlayerPanel().setTime(roundState.getGameSettings().getTotalTime(),
roundState.getGameSettings().getTotalTime()); roundState.getGameSettings().getTotalTime());
if (wasHuman || wasAI) { nextPlayer();
nextPlayer();
}
} }
/** /**
@ -425,6 +412,12 @@ public class RoundControl {
view.setSelectedStones(Collections.<Stone> emptyList()); view.setSelectedStones(Collections.<Stone> emptyList());
view.setInvalidStoneSets(Collections.<StoneSet> emptyList()); view.setInvalidStoneSets(Collections.<StoneSet> emptyList());
view.setStoneCollectionHidden(false); view.setStoneCollectionHidden(false);
if (roundState.getActivePlayer().getHand().getSize() == 0) {
endOfRound();
return;
}
if (roundState.getLastPlayer() == null) { if (roundState.getLastPlayer() == null) {
if (roundState.getGameHeap().getSize() == 0) { if (roundState.getGameHeap().getSize() == 0) {
roundState.setLastPlayer(roundState.getActivePlayer()); roundState.setLastPlayer(roundState.getActivePlayer());
@ -437,14 +430,14 @@ public class RoundControl {
} else { } else {
if (roundState.getActivePlayer() == roundState.getLastPlayer()) { if (roundState.getActivePlayer() == roundState.getLastPlayer()) {
endOfRound(); endOfRound();
return;
} else { } else {
roundState.nextPlayer(); roundState.nextPlayer();
roundState.nextTurn(); roundState.nextTurn();
} }
} }
if (!roundFinished) {
prepareTurn(); prepareTurn();
}
} }
/** /**
@ -454,7 +447,6 @@ public class RoundControl {
removeListeners(); removeListeners();
Score roundScore = score(); Score roundScore = score();
endOfRoundEvent.emit(roundScore); endOfRoundEvent.emit(roundScore);
roundFinished = true;
} }
/** /**
@ -495,12 +487,10 @@ public class RoundControl {
stonePoints = playerHand.isInitialMeldPossible(roundState stonePoints = playerHand.isInitialMeldPossible(roundState
.getGameSettings()) ? 200 : 100; .getGameSettings()) ? 200 : 100;
} else { } else {
stonePoints = playerHand.getStonePoints(roundState stonePoints = playerHand.getStonePoints(roundState.getGameSettings());
.getGameSettings());
} }
bestScore = updateBestScore(bestScore, -stonePoints, bestScore = updateBestScore(bestScore, -stonePoints, playerHand.getSize());
playerHand.getSize());
points.add(-stonePoints); points.add(-stonePoints);
pointSum += stonePoints; pointSum += stonePoints;
@ -524,11 +514,11 @@ public class RoundControl {
* (everybody still has stones on hand) * (everybody still has stones on hand)
* *
* @param bestScore * @param bestScore
* of previous rounds * of previous rounds
* @param stonePoints * @param stonePoints
* sum of points still left on hands * sum of points still left on hands
* @param size * @param size
* number of players in game (= size of score list in columns) * number of players in game (= size of score list in columns)
* @return Pair of maximum points and hand size * @return Pair of maximum points and hand size
*/ */
private static Pair<Integer, Integer> updateBestScore( private static Pair<Integer, Integer> updateBestScore(
@ -552,8 +542,8 @@ public class RoundControl {
} }
/** /**
* Redeal stones and restart round if a player was allowed to redeal and * Redeal stones and restart round if a player was allowed to redeal and chose
* chose to do so * to do so
*/ */
private void redeal() { private void redeal() {
turnControl = null; turnControl = null;