2011-05-04 15:20:52 +02:00
|
|
|
package jrummikub.control;
|
|
|
|
|
2011-05-10 03:13:11 +02:00
|
|
|
import java.util.ArrayList;
|
2011-05-05 16:48:17 +02:00
|
|
|
import java.util.HashSet;
|
2011-05-10 03:13:11 +02:00
|
|
|
import java.util.List;
|
2011-05-05 15:57:13 +02:00
|
|
|
import java.util.Set;
|
|
|
|
|
2011-05-29 19:46:50 +02:00
|
|
|
import jrummikub.control.turn.ITurnControl;
|
2011-05-29 21:49:03 +02:00
|
|
|
import jrummikub.control.turn.TurnControlFactory;
|
2011-05-10 05:53:30 +02:00
|
|
|
import jrummikub.model.Hand;
|
2011-05-04 19:24:51 +02:00
|
|
|
import jrummikub.model.IHand;
|
2011-05-25 15:51:34 +02:00
|
|
|
import jrummikub.model.IPlayer;
|
|
|
|
import jrummikub.model.IRoundState;
|
2011-05-05 15:57:13 +02:00
|
|
|
import jrummikub.model.ITable;
|
2011-05-04 19:24:51 +02:00
|
|
|
import jrummikub.model.Position;
|
2011-05-25 15:51:34 +02:00
|
|
|
import jrummikub.model.Score;
|
2011-05-05 15:57:13 +02:00
|
|
|
import jrummikub.model.Stone;
|
2011-05-05 16:48:17 +02:00
|
|
|
import jrummikub.model.StoneSet;
|
2011-05-10 03:13:11 +02:00
|
|
|
import jrummikub.util.Connection;
|
2011-05-27 17:54:42 +02:00
|
|
|
import jrummikub.util.Event;
|
2011-05-25 15:51:34 +02:00
|
|
|
import jrummikub.util.Event1;
|
2011-05-27 17:54:42 +02:00
|
|
|
import jrummikub.util.IEvent;
|
2011-05-25 15:51:34 +02:00
|
|
|
import jrummikub.util.IEvent1;
|
2011-05-05 01:33:58 +02:00
|
|
|
import jrummikub.util.IListener;
|
2011-05-05 16:48:17 +02:00
|
|
|
import jrummikub.util.Pair;
|
2011-05-04 15:20:52 +02:00
|
|
|
import jrummikub.view.IView;
|
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/**
|
|
|
|
* Controller that manages a single round of rummikub
|
|
|
|
*/
|
2011-05-04 19:09:39 +02:00
|
|
|
public class RoundControl {
|
2011-05-27 17:54:46 +02:00
|
|
|
IRoundState roundState;
|
2011-05-04 15:20:52 +02:00
|
|
|
private IView view;
|
2011-05-05 15:57:13 +02:00
|
|
|
private ITable clonedTable;
|
2011-05-27 17:54:42 +02:00
|
|
|
private Event restartRoundEvent = new Event();
|
2011-05-25 15:51:34 +02:00
|
|
|
private Event1<Score> endOfRoundEvent = new Event1<Score>();
|
2011-05-10 03:13:11 +02:00
|
|
|
private List<Connection> connections = new ArrayList<Connection>();
|
2011-05-24 01:51:53 +02:00
|
|
|
private boolean roundFinished;
|
2011-05-04 15:20:52 +02:00
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/**
|
|
|
|
* Create a new RoundControl using the given gameState and view
|
|
|
|
*
|
2011-05-24 21:57:18 +02:00
|
|
|
* @param roundState
|
2011-05-27 17:54:42 +02:00
|
|
|
* initial round state
|
2011-05-10 03:54:48 +02:00
|
|
|
* @param view
|
2011-05-27 17:54:42 +02:00
|
|
|
* view used for user interaction
|
2011-05-10 03:54:48 +02:00
|
|
|
*/
|
2011-05-21 15:51:36 +02:00
|
|
|
public RoundControl(IRoundState roundState, IView view) {
|
|
|
|
this.roundState = roundState;
|
2011-05-04 15:20:52 +02:00
|
|
|
this.view = view;
|
2011-05-10 03:13:11 +02:00
|
|
|
}
|
|
|
|
|
2011-05-10 04:07:49 +02:00
|
|
|
/**
|
|
|
|
* End the round
|
|
|
|
*
|
2011-05-24 22:16:16 +02:00
|
|
|
* @return endOfRoundEvent
|
2011-05-10 04:07:49 +02:00
|
|
|
*/
|
2011-05-25 15:51:34 +02:00
|
|
|
public IEvent1<Score> getEndOfRoundEvent() {
|
2011-05-24 22:16:16 +02:00
|
|
|
return endOfRoundEvent;
|
2011-05-04 15:20:52 +02:00
|
|
|
}
|
|
|
|
|
2011-05-10 03:54:48 +02:00
|
|
|
/**
|
|
|
|
* Begin the round
|
|
|
|
*/
|
2011-05-04 19:09:39 +02:00
|
|
|
public void startRound() {
|
2011-05-05 01:33:58 +02:00
|
|
|
deal();
|
2011-05-04 19:09:39 +02:00
|
|
|
|
2011-05-10 03:13:11 +02:00
|
|
|
connections.add(view.getStartTurnEvent().add(new IListener() {
|
2011-05-05 01:33:58 +02:00
|
|
|
@Override
|
|
|
|
public void handle() {
|
|
|
|
startTurn();
|
|
|
|
}
|
2011-05-10 03:13:11 +02:00
|
|
|
}));
|
2011-05-05 17:14:34 +02:00
|
|
|
|
|
|
|
prepareTurn();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void prepareTurn() {
|
2011-05-21 15:51:36 +02:00
|
|
|
clonedTable = (ITable) roundState.getTable().clone();
|
2011-05-05 17:14:34 +02:00
|
|
|
view.enableStartTurnPanel(true);
|
2011-05-05 15:57:13 +02:00
|
|
|
view.getTablePanel().setStoneSets(clonedTable);
|
2011-05-27 17:54:42 +02:00
|
|
|
view.setCurrentPlayerName(roundState.getActivePlayer()
|
|
|
|
.getPlayerSettings().getName());
|
2011-05-29 18:42:36 +02:00
|
|
|
view.setCurrentPlayerColor(roundState.getActivePlayer()
|
|
|
|
.getPlayerSettings().getColor());
|
|
|
|
view.setHasLaidOut(roundState.getActivePlayer().getLaidOut());
|
2011-05-27 18:55:19 +02:00
|
|
|
|
2011-05-05 01:33:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void startTurn() {
|
2011-05-27 18:55:19 +02:00
|
|
|
boolean inspectOnly = roundState.getTurnNumber() < 1;
|
|
|
|
boolean mayRedeal = inspectOnly
|
|
|
|
&& roundState.getActivePlayer().getHand()
|
|
|
|
.getIdenticalStoneCount() >= 3;
|
|
|
|
|
|
|
|
view.getPlayerPanel().setEndTurnMode(inspectOnly, mayRedeal);
|
|
|
|
|
2011-05-29 21:49:03 +02:00
|
|
|
ITurnControl turnControl = TurnControlFactory.getFactory(roundState.getActivePlayer()
|
|
|
|
.getPlayerSettings().getTurnControlType()).create();
|
2011-05-30 01:31:32 +02:00
|
|
|
turnControl.setup(roundState.getActivePlayer(), clonedTable,
|
2011-05-29 20:22:47 +02:00
|
|
|
view, inspectOnly, mayRedeal);
|
2011-05-27 17:54:46 +02:00
|
|
|
turnControl.getEndOfTurnEvent().add(new IListener() {
|
2011-05-05 15:57:13 +02:00
|
|
|
@Override
|
|
|
|
public void handle() {
|
|
|
|
endOfTurn();
|
|
|
|
}
|
2011-05-27 17:54:46 +02:00
|
|
|
});
|
2011-05-27 18:55:19 +02:00
|
|
|
|
2011-05-27 17:54:46 +02:00
|
|
|
turnControl.getRedealEvent().add(new IListener() {
|
|
|
|
@Override
|
|
|
|
public void handle() {
|
|
|
|
redeal();
|
|
|
|
}
|
|
|
|
});
|
2011-05-05 19:49:58 +02:00
|
|
|
turnControl.startTurn();
|
2011-05-04 19:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void deal() {
|
2011-05-21 15:51:36 +02:00
|
|
|
for (int i = 0; i < roundState.getPlayerCount(); i++) {
|
|
|
|
IHand hand = roundState.getNthNextPlayer(i).getHand();
|
2011-05-04 19:24:51 +02:00
|
|
|
for (int j = 0; j < 7; j++) {
|
2011-05-27 17:54:42 +02:00
|
|
|
hand.drop(roundState.getGameHeap().drawStone(), new Position(j,
|
|
|
|
0));
|
|
|
|
hand.drop(roundState.getGameHeap().drawStone(), new Position(j,
|
|
|
|
1));
|
2011-05-04 19:24:51 +02:00
|
|
|
}
|
|
|
|
}
|
2011-05-04 19:09:39 +02:00
|
|
|
}
|
|
|
|
|
2011-05-24 22:33:08 +02:00
|
|
|
private boolean laidOutValidPoints() {
|
2011-05-24 01:51:47 +02:00
|
|
|
List<StoneSet> newSets = tableSetDifference(roundState.getTable(),
|
|
|
|
clonedTable);
|
2011-05-05 21:13:59 +02:00
|
|
|
|
2011-05-24 01:51:47 +02:00
|
|
|
int totalValue = 0;
|
|
|
|
for (StoneSet set : newSets) {
|
|
|
|
totalValue += set.classify().getSecond();
|
2011-05-21 15:51:36 +02:00
|
|
|
}
|
2011-05-05 21:13:59 +02:00
|
|
|
|
2011-05-24 22:33:08 +02:00
|
|
|
return totalValue == 0
|
2011-05-27 17:54:42 +02:00
|
|
|
|| totalValue >= roundState.getGameSettings()
|
|
|
|
.getInitialMeldThreshold();
|
2011-05-24 01:51:47 +02:00
|
|
|
}
|
2011-05-21 15:51:36 +02:00
|
|
|
|
2011-05-24 01:51:47 +02:00
|
|
|
private void endOfTurn() {
|
2011-05-25 17:10:43 +02:00
|
|
|
if (roundState.getTurnNumber() >= 1) {
|
|
|
|
checkTurn();
|
|
|
|
}
|
2011-05-24 22:33:08 +02:00
|
|
|
|
2011-05-24 01:51:53 +02:00
|
|
|
if (roundState.getLastPlayer() == null) {
|
|
|
|
if (roundState.getGameHeap().getSize() == 0) {
|
|
|
|
roundState.setLastPlayer(roundState.getNthNextPlayer(-1));
|
2011-05-25 17:10:43 +02:00
|
|
|
roundState.nextTurn();
|
2011-05-24 01:51:53 +02:00
|
|
|
} else {
|
|
|
|
roundState.nextPlayer();
|
2011-05-25 17:10:43 +02:00
|
|
|
roundState.nextTurn();
|
2011-05-24 01:51:53 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (roundState.getActivePlayer() == roundState.getLastPlayer()) {
|
2011-05-24 22:16:16 +02:00
|
|
|
endOfRound();
|
2011-05-24 01:51:53 +02:00
|
|
|
} else {
|
|
|
|
roundState.nextPlayer();
|
2011-05-25 17:10:43 +02:00
|
|
|
roundState.nextTurn();
|
2011-05-24 01:51:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!roundFinished) {
|
|
|
|
prepareTurn();
|
|
|
|
}
|
2011-05-24 01:51:47 +02:00
|
|
|
}
|
2011-05-24 22:33:08 +02:00
|
|
|
|
2011-05-24 01:51:47 +02:00
|
|
|
private void checkTurn() {
|
|
|
|
if (!clonedTable.isValid()) {
|
|
|
|
rejectMove();
|
|
|
|
return;
|
2011-05-24 22:33:08 +02:00
|
|
|
}
|
2011-05-24 01:51:47 +02:00
|
|
|
if (!roundState.getActivePlayer().getLaidOut()) {
|
|
|
|
// Player touched forbidden stones
|
2011-05-27 17:54:42 +02:00
|
|
|
if (!tableSetDifference(clonedTable, roundState.getTable())
|
|
|
|
.isEmpty()) {
|
2011-05-24 01:51:47 +02:00
|
|
|
rejectMove();
|
|
|
|
return;
|
2011-05-21 15:51:36 +02:00
|
|
|
}
|
2011-05-24 22:33:08 +02:00
|
|
|
if (!laidOutValidPoints()) {
|
2011-05-24 01:51:47 +02:00
|
|
|
rejectMove();
|
|
|
|
return;
|
2011-05-05 21:13:59 +02:00
|
|
|
}
|
2011-05-21 15:51:36 +02:00
|
|
|
}
|
2011-05-27 17:54:42 +02:00
|
|
|
Set<Stone> tableDiff = tableDifference(roundState.getTable(),
|
|
|
|
clonedTable);
|
2011-05-24 22:33:08 +02:00
|
|
|
|
2011-05-24 01:51:47 +02:00
|
|
|
roundState.setTable(clonedTable);
|
2011-05-21 15:51:36 +02:00
|
|
|
|
|
|
|
if (tableDiff.isEmpty()) {
|
|
|
|
// Player hasn't made a move
|
2011-05-05 21:13:59 +02:00
|
|
|
dealStone();
|
2011-05-21 15:51:36 +02:00
|
|
|
} else {
|
2011-05-24 01:51:47 +02:00
|
|
|
roundState.getActivePlayer().setLaidOut(true);
|
|
|
|
if (roundState.getActivePlayer().getHand().getSize() == 0) {
|
2011-05-24 22:16:16 +02:00
|
|
|
endOfRound();
|
2011-05-21 15:51:36 +02:00
|
|
|
}
|
2011-05-05 21:13:59 +02:00
|
|
|
}
|
2011-05-24 01:51:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void rejectMove() {
|
2011-05-27 17:54:42 +02:00
|
|
|
Set<Stone> tableDiff = tableDifference(roundState.getTable(),
|
|
|
|
clonedTable);
|
2011-05-24 01:51:47 +02:00
|
|
|
// deal penalty, reset
|
|
|
|
roundState.getGameHeap().putBack(tableDiff);
|
|
|
|
dealPenalty(tableDiff.size());
|
2011-05-24 22:33:08 +02:00
|
|
|
|
2011-05-04 15:20:52 +02:00
|
|
|
}
|
|
|
|
|
2011-05-05 15:57:13 +02:00
|
|
|
static Set<Stone> tableDifference(ITable oldTable, ITable newTable) {
|
2011-05-05 16:48:17 +02:00
|
|
|
Set<Stone> ret = new HashSet<Stone>();
|
|
|
|
|
|
|
|
for (Pair<StoneSet, Position> entry : newTable) {
|
|
|
|
for (Stone stone : entry.getFirst()) {
|
|
|
|
ret.add(stone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (Pair<StoneSet, Position> entry : oldTable) {
|
|
|
|
for (Stone stone : entry.getFirst()) {
|
|
|
|
ret.remove(stone);
|
|
|
|
}
|
|
|
|
}
|
2011-05-16 22:24:11 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2011-05-05 17:14:34 +02:00
|
|
|
|
2011-05-16 22:24:11 +02:00
|
|
|
static List<StoneSet> tableSetDifference(ITable oldTable, ITable newTable) {
|
|
|
|
List<StoneSet> ret = new ArrayList<StoneSet>();
|
|
|
|
|
|
|
|
for (Pair<StoneSet, Position> entry : newTable) {
|
|
|
|
ret.add(entry.getFirst());
|
|
|
|
}
|
|
|
|
for (Pair<StoneSet, Position> entry : oldTable) {
|
|
|
|
ret.remove(entry.getFirst());
|
|
|
|
}
|
2011-05-05 16:48:17 +02:00
|
|
|
return ret;
|
2011-05-04 15:20:52 +02:00
|
|
|
}
|
|
|
|
|
2011-05-16 22:01:02 +02:00
|
|
|
void dealStones(int count) {
|
2011-05-21 15:51:36 +02:00
|
|
|
IHand hand = roundState.getActivePlayer().getHand();
|
2011-05-16 22:01:02 +02:00
|
|
|
int rowCount = hand.getRowCount();
|
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
if (hand.getFreeRowSpace(rowCount - 1) == 0) {
|
|
|
|
rowCount++;
|
|
|
|
}
|
|
|
|
|
2011-05-21 15:51:36 +02:00
|
|
|
hand.drop(roundState.getGameHeap().drawStone(), new Position(
|
2011-05-16 22:01:02 +02:00
|
|
|
Hand.WIDTH - 1, rowCount - 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void dealStone() {
|
|
|
|
dealStones(1);
|
2011-05-05 21:13:59 +02:00
|
|
|
}
|
2011-05-04 15:20:52 +02:00
|
|
|
|
2011-05-05 21:13:59 +02:00
|
|
|
private void dealPenalty(int count) {
|
2011-05-16 22:01:02 +02:00
|
|
|
dealStones(count + 3);
|
2011-05-04 15:20:52 +02:00
|
|
|
}
|
2011-05-06 01:35:09 +02:00
|
|
|
|
2011-05-25 15:51:34 +02:00
|
|
|
void endOfRound() {
|
2011-05-10 03:13:11 +02:00
|
|
|
for (Connection c : connections) {
|
|
|
|
c.remove();
|
|
|
|
}
|
2011-05-25 15:51:34 +02:00
|
|
|
Score roundScore = score();
|
|
|
|
endOfRoundEvent.emit(roundScore);
|
2011-05-24 01:51:53 +02:00
|
|
|
roundFinished = true;
|
2011-05-06 01:35:09 +02:00
|
|
|
}
|
2011-05-25 15:51:34 +02:00
|
|
|
|
|
|
|
private Score score() {
|
|
|
|
List<Boolean> winners = new ArrayList<Boolean>();
|
|
|
|
List<Integer> points = new ArrayList<Integer>();
|
|
|
|
|
|
|
|
boolean foundRegularWinner = false;
|
|
|
|
int winnerPlayerNumber = 0;
|
2011-05-26 16:02:33 +02:00
|
|
|
Pair<Integer, Integer> bestScore = new Pair<Integer, Integer>(
|
|
|
|
Integer.MIN_VALUE, Integer.MAX_VALUE);
|
2011-05-25 15:51:34 +02:00
|
|
|
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();
|
|
|
|
}
|
2011-05-26 16:02:33 +02:00
|
|
|
|
2011-05-27 17:54:42 +02:00
|
|
|
bestScore = updateBestScore(bestScore, -stonePoints,
|
|
|
|
playerHand.getSize());
|
2011-05-26 16:02:33 +02:00
|
|
|
|
2011-05-25 15:51:34 +02:00
|
|
|
points.add(-stonePoints);
|
|
|
|
pointSum += stonePoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundRegularWinner) {
|
|
|
|
points.set(winnerPlayerNumber, pointSum);
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < roundState.getPlayerCount(); i++) {
|
2011-05-26 16:02:33 +02:00
|
|
|
if (bestScore.equals(new Pair<Integer, Integer>(points.get(i),
|
|
|
|
roundState.getNthPlayer(i).getHand().getSize()))) {
|
2011-05-25 15:51:34 +02:00
|
|
|
winners.set(i, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Score(winners, points);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Pair<Integer, Integer> updateBestScore(
|
|
|
|
Pair<Integer, Integer> bestScore, int stonePoints, int size) {
|
|
|
|
if (bestScore.getFirst() == stonePoints) {
|
2011-05-26 16:02:33 +02:00
|
|
|
return new Pair<Integer, Integer>(stonePoints, Math.min(
|
|
|
|
bestScore.getSecond(), size));
|
2011-05-25 15:51:34 +02:00
|
|
|
} else if (bestScore.getFirst() < stonePoints) {
|
|
|
|
return new Pair<Integer, Integer>(stonePoints, size);
|
|
|
|
}
|
|
|
|
return bestScore;
|
|
|
|
}
|
2011-05-27 17:54:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Emitted when the round is aborted and needs to be restarted
|
|
|
|
*
|
|
|
|
* @return the event
|
|
|
|
*/
|
|
|
|
public IEvent getRestartRoundEvent() {
|
|
|
|
return restartRoundEvent;
|
|
|
|
}
|
2011-05-27 17:54:44 +02:00
|
|
|
|
|
|
|
private void redeal() {
|
2011-05-27 17:54:46 +02:00
|
|
|
for (Connection c : new ArrayList<Connection>(connections)) {
|
2011-05-27 17:54:44 +02:00
|
|
|
c.remove();
|
|
|
|
}
|
|
|
|
restartRoundEvent.emit();
|
|
|
|
}
|
2011-05-04 15:20:52 +02:00
|
|
|
}
|