package jrummikub.control; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import jrummikub.control.turn.HumanTurnControl; import jrummikub.control.turn.ITurnControl; import jrummikub.model.Hand; import jrummikub.model.IHand; import jrummikub.model.IPlayer; import jrummikub.model.IRoundState; import jrummikub.model.ITable; import jrummikub.model.Position; import jrummikub.model.Score; import jrummikub.model.Stone; import jrummikub.model.StoneSet; import jrummikub.util.Connection; import jrummikub.util.Event; import jrummikub.util.Event1; import jrummikub.util.IEvent; import jrummikub.util.IEvent1; import jrummikub.util.IListener; import jrummikub.util.Pair; import jrummikub.view.IView; /** * Controller that manages a single round of rummikub */ public class RoundControl { IRoundState roundState; private IView view; private ITable clonedTable; private Event restartRoundEvent = new Event(); private Event1 endOfRoundEvent = new Event1(); private List connections = new ArrayList(); private boolean roundFinished; /** * Create a new RoundControl using the given gameState and view * * @param roundState * initial round state * @param view * view used for user interaction */ public RoundControl(IRoundState roundState, IView view) { this.roundState = roundState; this.view = view; } /** * End the round * * @return endOfRoundEvent */ public IEvent1 getEndOfRoundEvent() { return endOfRoundEvent; } /** * Begin the round */ public void startRound() { deal(); connections.add(view.getStartTurnEvent().add(new IListener() { @Override public void handle() { startTurn(); } })); prepareTurn(); } private void prepareTurn() { clonedTable = (ITable) roundState.getTable().clone(); view.enableStartTurnPanel(true); view.getTablePanel().setStoneSets(clonedTable); view.setCurrentPlayerName(roundState.getActivePlayer() .getPlayerSettings().getName()); view.setCurrentPlayerColor(roundState.getActivePlayer() .getPlayerSettings().getColor()); view.setHasLaidOut(roundState.getActivePlayer().getLaidOut()); } private void startTurn() { boolean inspectOnly = roundState.getTurnNumber() < 1; boolean mayRedeal = inspectOnly && roundState.getActivePlayer().getHand() .getIdenticalStoneCount() >= 3; view.getPlayerPanel().setEndTurnMode(inspectOnly, mayRedeal); ITurnControl turnControl = roundState.getActivePlayer() .getPlayerSettings().getTurnControlFactory().create(); turnControl.setup(roundState.getActivePlayer().getHand(), clonedTable, view, inspectOnly, mayRedeal); turnControl.getEndOfTurnEvent().add(new IListener() { @Override public void handle() { endOfTurn(); } }); turnControl.getRedealEvent().add(new IListener() { @Override public void handle() { redeal(); } }); turnControl.startTurn(); } void deal() { for (int i = 0; i < roundState.getPlayerCount(); i++) { IHand hand = roundState.getNthNextPlayer(i).getHand(); for (int j = 0; j < 7; j++) { hand.drop(roundState.getGameHeap().drawStone(), new Position(j, 0)); hand.drop(roundState.getGameHeap().drawStone(), new Position(j, 1)); } } } private boolean laidOutValidPoints() { List newSets = tableSetDifference(roundState.getTable(), clonedTable); int totalValue = 0; for (StoneSet set : newSets) { totalValue += set.classify().getSecond(); } return totalValue == 0 || totalValue >= roundState.getGameSettings() .getInitialMeldThreshold(); } private void endOfTurn() { if (roundState.getTurnNumber() >= 1) { checkTurn(); } if (roundState.getLastPlayer() == null) { if (roundState.getGameHeap().getSize() == 0) { roundState.setLastPlayer(roundState.getNthNextPlayer(-1)); roundState.nextTurn(); } else { roundState.nextPlayer(); roundState.nextTurn(); } } else { if (roundState.getActivePlayer() == roundState.getLastPlayer()) { endOfRound(); } else { roundState.nextPlayer(); roundState.nextTurn(); } } if (!roundFinished) { prepareTurn(); } } private void checkTurn() { if (!clonedTable.isValid()) { rejectMove(); return; } if (!roundState.getActivePlayer().getLaidOut()) { // Player touched forbidden stones if (!tableSetDifference(clonedTable, roundState.getTable()) .isEmpty()) { rejectMove(); return; } if (!laidOutValidPoints()) { rejectMove(); return; } } Set tableDiff = tableDifference(roundState.getTable(), clonedTable); roundState.setTable(clonedTable); if (tableDiff.isEmpty()) { // Player hasn't made a move dealStone(); } else { roundState.getActivePlayer().setLaidOut(true); if (roundState.getActivePlayer().getHand().getSize() == 0) { endOfRound(); } } } private void rejectMove() { Set tableDiff = tableDifference(roundState.getTable(), clonedTable); // deal penalty, reset roundState.getGameHeap().putBack(tableDiff); dealPenalty(tableDiff.size()); } static Set tableDifference(ITable oldTable, ITable newTable) { Set ret = new HashSet(); for (Pair entry : newTable) { for (Stone stone : entry.getFirst()) { ret.add(stone); } } for (Pair entry : oldTable) { for (Stone stone : entry.getFirst()) { ret.remove(stone); } } return ret; } static List tableSetDifference(ITable oldTable, ITable newTable) { List ret = new ArrayList(); for (Pair entry : newTable) { ret.add(entry.getFirst()); } for (Pair entry : oldTable) { ret.remove(entry.getFirst()); } return ret; } void dealStones(int count) { IHand hand = roundState.getActivePlayer().getHand(); int rowCount = hand.getRowCount(); for (int i = 0; i < count; ++i) { if (hand.getFreeRowSpace(rowCount - 1) == 0) { rowCount++; } hand.drop(roundState.getGameHeap().drawStone(), new Position( Hand.WIDTH - 1, rowCount - 1)); } } private void dealStone() { dealStones(1); } private void dealPenalty(int count) { dealStones(count + 3); } void endOfRound() { for (Connection c : connections) { c.remove(); } Score roundScore = score(); endOfRoundEvent.emit(roundScore); roundFinished = true; } private Score score() { List winners = new ArrayList(); List points = new ArrayList(); boolean foundRegularWinner = false; int winnerPlayerNumber = 0; Pair bestScore = new Pair( Integer.MIN_VALUE, Integer.MAX_VALUE); int pointSum = 0; for (int i = 0; i < roundState.getPlayerCount(); i++) { IPlayer player = roundState.getNthPlayer(i); IHand playerHand = player.getHand(); boolean isWinner = playerHand.getSize() == 0; winners.add(isWinner); if (isWinner) { foundRegularWinner = true; winnerPlayerNumber = i; } int stonePoints = 0; if (!player.getLaidOut()) { stonePoints = playerHand.isInitialMeldPossible() ? 200 : 100; } else { stonePoints = playerHand.getStonePoints(); } bestScore = updateBestScore(bestScore, -stonePoints, playerHand.getSize()); points.add(-stonePoints); pointSum += stonePoints; } if (foundRegularWinner) { points.set(winnerPlayerNumber, pointSum); } else { for (int i = 0; i < roundState.getPlayerCount(); i++) { if (bestScore.equals(new Pair(points.get(i), roundState.getNthPlayer(i).getHand().getSize()))) { winners.set(i, true); } } } return new Score(winners, points); } private static Pair updateBestScore( Pair bestScore, int stonePoints, int size) { if (bestScore.getFirst() == stonePoints) { return new Pair(stonePoints, Math.min( bestScore.getSecond(), size)); } else if (bestScore.getFirst() < stonePoints) { return new Pair(stonePoints, size); } return bestScore; } /** * Emitted when the round is aborted and needs to be restarted * * @return the event */ public IEvent getRestartRoundEvent() { return restartRoundEvent; } private void redeal() { for (Connection c : new ArrayList(connections)) { c.remove(); } restartRoundEvent.emit(); } }