Correctly show invalid sets

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@471 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-06-19 00:14:27 +02:00
parent e8549b95df
commit 479384d6bf
13 changed files with 183 additions and 100 deletions

View file

@ -50,9 +50,9 @@ public class RoundControl {
* Create a new RoundControl using the given gameState and view
*
* @param roundState
* initial round state
* initial round state
* @param view
* view used for user interaction
* view used for user interaction
*/
public RoundControl(IRoundState roundState, IView view) {
this.roundState = roundState;
@ -129,12 +129,11 @@ public class RoundControl {
}
view.getTablePanel().setStoneSets(clonedTable.clone());
view.setCurrentPlayerName(roundState.getActivePlayer()
.getPlayerSettings().getName());
view.setCurrentPlayerColor(roundState.getActivePlayer()
.getPlayerSettings().getColor());
view.setCurrentPlayerHasLaidOut(roundState.getActivePlayer()
.getLaidOut());
view.setCurrentPlayerName(roundState.getActivePlayer().getPlayerSettings()
.getName());
view.setCurrentPlayerColor(roundState.getActivePlayer().getPlayerSettings()
.getColor());
view.setCurrentPlayerHasLaidOut(roundState.getActivePlayer().getLaidOut());
if (!isHuman)
startTurn();
@ -160,11 +159,10 @@ public class RoundControl {
view.getPlayerPanel().setEndTurnMode(turnMode);
}
turnControl = TurnControlFactory.getFactory(
roundState.getActivePlayer().getPlayerSettings().getType())
.create();
roundState.getActivePlayer().getPlayerSettings().getType()).create();
turnControl.setup(new ITurnControl.TurnInfo(clonedTable, clonedHand,
roundState.getActivePlayer().getLaidOut(), turnMode),
roundState.getGameSettings(), view);
roundState.getActivePlayer().getLaidOut(), turnMode), roundState
.getGameSettings(), view);
turnControl.getEndOfTurnEvent().add(new IListener() {
@Override
public void handle() {
@ -184,10 +182,8 @@ public class RoundControl {
void deal() {
for (int i = 0; i < roundState.getPlayerCount(); i++) {
IHand hand = roundState.getNthNextPlayer(i).getHand();
for (int j = 0; j < roundState.getGameSettings()
.getNumberOfStonesDealt(); j++) {
hand.drop(roundState.getGameHeap().drawStone(), new Position(0,
0));
for (int j = 0; j < roundState.getGameSettings().getNumberOfStonesDealt(); j++) {
hand.drop(roundState.getGameHeap().drawStone(), new Position(0, 0));
}
}
}
@ -198,13 +194,11 @@ public class RoundControl {
int totalValue = 0;
for (StoneSet set : newSets) {
totalValue += set.classify(roundState.getGameSettings())
.getSecond();
totalValue += set.classify(roundState.getGameSettings()).getSecond();
}
return totalValue == 0
|| totalValue >= roundState.getGameSettings()
.getInitialMeldThreshold();
|| totalValue >= roundState.getGameSettings().getInitialMeldThreshold();
}
private void endOfTurn() {
@ -223,26 +217,46 @@ public class RoundControl {
if (lastTurnNotEnoughPoints) {
view.setInitialMeldError(roundState.getGameSettings()
.getInitialMeldThreshold());
view.setInvalidStoneSets(tableSetDifference(roundState.getTable(),
clonedTable));
} else if (lastTurnMeldError) {
view.setInitialMeldFirstError();
view.setInvalidStoneSets(touchedStoneSets());
} else {
List<Stone> markedStones = new ArrayList<Stone>();
for (Pair<StoneSet, Position> set : clonedTable) {
if (set.getFirst().isValid(roundState.getGameSettings())) {
continue;
}
for (Stone stone : set.getFirst()) {
markedStones.add(stone);
}
}
view.setStoneCollectionHidden(true);
view.setSelectedStones(markedStones);
view.setInvalidStoneSets(invalidStoneSets());
}
}
}
private List<StoneSet> invalidStoneSets() {
List<StoneSet> invalidSets = new ArrayList<StoneSet>();
for (Pair<StoneSet, Position> set : clonedTable) {
if (set.getFirst().isValid(roundState.getGameSettings())) {
continue;
}
invalidSets.add(set.getFirst());
}
return invalidSets;
}
private List<StoneSet> touchedStoneSets() {
List<StoneSet> touchedSets = new ArrayList<StoneSet>();
for (StoneSet set : tableSetDifference(roundState.getTable(), clonedTable)) {
for (Stone stone : set) {
if (!roundState.getActivePlayer().getHand().contains(stone)) {
touchedSets.add(set);
break;
}
}
}
return touchedSets;
}
private void nextPlayer() {
view.setSelectedStones(Collections.<Stone> emptyList());
view.setInvalidStoneSets(Collections.<StoneSet> emptyList());
view.setStoneCollectionHidden(false);
if (roundState.getLastPlayer() == null) {
if (roundState.getGameHeap().getSize() == 0) {
@ -273,8 +287,7 @@ public class RoundControl {
}
if (!roundState.getActivePlayer().getLaidOut()) {
// Player touched forbidden stones
if (!tableSetDifference(clonedTable, roundState.getTable())
.isEmpty()) {
if (!tableSetDifference(clonedTable, roundState.getTable()).isEmpty()) {
rejectMove();
lastTurnMeldError = true;
return false;
@ -285,8 +298,7 @@ public class RoundControl {
return false;
}
}
Set<Stone> tableDiff = tableDifference(roundState.getTable(),
clonedTable);
Set<Stone> tableDiff = tableDifference(roundState.getTable(), clonedTable);
roundState.setTable(clonedTable);
@ -303,8 +315,7 @@ public class RoundControl {
}
private void rejectMove() {
Set<Stone> tableDiff = tableDifference(roundState.getTable(),
clonedTable);
Set<Stone> tableDiff = tableDifference(roundState.getTable(), clonedTable);
// deal penalty, reset
roundState.getGameHeap().putBack(tableDiff);
dealPenalty(tableDiff.size());
@ -398,12 +409,10 @@ public class RoundControl {
stonePoints = playerHand.isInitialMeldPossible(roundState
.getGameSettings()) ? 200 : 100;
} else {
stonePoints = playerHand.getStonePoints(roundState
.getGameSettings());
stonePoints = playerHand.getStonePoints(roundState.getGameSettings());
}
bestScore = updateBestScore(bestScore, -stonePoints, playerHand
.getSize());
bestScore = updateBestScore(bestScore, -stonePoints, playerHand.getSize());
points.add(-stonePoints);
pointSum += stonePoints;
@ -425,8 +434,8 @@ public class RoundControl {
private static Pair<Integer, Integer> updateBestScore(
Pair<Integer, Integer> bestScore, int stonePoints, int size) {
if (bestScore.getFirst() == stonePoints) {
return new Pair<Integer, Integer>(stonePoints, Math.min(bestScore
.getSecond(), size));
return new Pair<Integer, Integer>(stonePoints, Math.min(
bestScore.getSecond(), size));
} else if (bestScore.getFirst() < stonePoints) {
return new Pair<Integer, Integer>(stonePoints, size);
}

View file

@ -441,7 +441,7 @@ public class HumanTurnControl extends AbstractTurnControl {
private void endOfTurn(boolean redeal) {
cleanUp();
view.setSelectedStones(new ArrayList<Stone>());
view.setSelectedStones(Collections.<Stone> emptyList());
if (redeal) {
redealEvent.emit();
} else {