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-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-25 15:51:34 +02:00
|
|
|
import jrummikub.util.Event1;
|
|
|
|
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-21 15:51:36 +02:00
|
|
|
private 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-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
|
|
|
|
* initial round state
|
2011-05-10 03:54:48 +02:00
|
|
|
* @param view
|
2011-05-16 22:24:11 +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-21 15:51:36 +02:00
|
|
|
view.setCurrentPlayerName(roundState.getActivePlayer()
|
|
|
|
.getPlayerSettings().getName());
|
2011-05-10 03:37:34 +02:00
|
|
|
view.getTablePanel().setLeftPlayerName(
|
2011-05-21 15:51:36 +02:00
|
|
|
roundState.getNthNextPlayer(1).getPlayerSettings().getName());
|
2011-05-05 17:14:34 +02:00
|
|
|
view.getTablePanel().setTopPlayerName(
|
2011-05-21 15:51:36 +02:00
|
|
|
roundState.getNthNextPlayer(2).getPlayerSettings().getName());
|
2011-05-10 03:37:34 +02:00
|
|
|
view.getTablePanel().setRightPlayerName(
|
2011-05-21 15:51:36 +02:00
|
|
|
roundState.getNthNextPlayer(3).getPlayerSettings().getName());
|
2011-05-05 01:33:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void startTurn() {
|
2011-05-21 15:51:36 +02:00
|
|
|
TurnControl turnControl = new TurnControl(roundState.getActivePlayer()
|
2011-05-25 17:10:43 +02:00
|
|
|
.getHand(), clonedTable, view, roundState.getTurnNumber() < 1);
|
2011-05-10 03:13:11 +02:00
|
|
|
connections.add(turnControl.getEndOfTurnEvent().add(new IListener() {
|
2011-05-05 15:57:13 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handle() {
|
|
|
|
endOfTurn();
|
|
|
|
}
|
2011-05-10 03:13:11 +02:00
|
|
|
}));
|
2011-05-05 21:13:59 +02:00
|
|
|
|
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-21 15:51:36 +02:00
|
|
|
hand.drop(roundState.getGameHeap().drawStone(), new Position(j,
|
2011-05-16 22:24:11 +02:00
|
|
|
0));
|
2011-05-21 15:51:36 +02:00
|
|
|
hand.drop(roundState.getGameHeap().drawStone(), new Position(j,
|
2011-05-16 22:24:11 +02:00
|
|
|
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
|
|
|
|
|| 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-24 22:33:08 +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
|
|
|
}
|
|
|
|
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() {
|
|
|
|
Set<Stone> tableDiff = tableDifference(roundState.getTable(),
|
|
|
|
clonedTable);
|
|
|
|
// 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;
|
|
|
|
Pair<Integer, Integer> bestScore = new Pair<Integer, Integer>(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<Integer, Integer>(points.get(i), roundState.getNthPlayer(i).getHand().getSize()))) {
|
|
|
|
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) {
|
|
|
|
return new Pair<Integer, Integer>(stonePoints, Math.min(bestScore.getSecond(), size));
|
|
|
|
} else if (bestScore.getFirst() < stonePoints) {
|
|
|
|
return new Pair<Integer, Integer>(stonePoints, size);
|
|
|
|
}
|
|
|
|
return bestScore;
|
|
|
|
}
|
2011-05-04 15:20:52 +02:00
|
|
|
}
|