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-18 15:25:26 +02:00
|
|
|
import jrummikub.model.IRoundState;
|
2011-05-04 19:24:51 +02:00
|
|
|
import jrummikub.model.IHand;
|
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-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;
|
|
|
|
import jrummikub.util.Event;
|
|
|
|
import jrummikub.util.IEvent;
|
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-10 03:13:11 +02:00
|
|
|
private Event endRoundEvent = new Event();
|
|
|
|
private List<Connection> connections = new ArrayList<Connection>();
|
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
|
|
|
|
*
|
|
|
|
* @param gameState
|
2011-05-16 22:24:11 +02:00
|
|
|
* initial game 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
|
|
|
|
*
|
|
|
|
* @return endRoundEvent
|
|
|
|
*/
|
2011-05-10 03:13:11 +02:00
|
|
|
public IEvent getEndRoundEvent() {
|
|
|
|
return endRoundEvent;
|
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-05 15:57:13 +02:00
|
|
|
.getHand(), clonedTable, view);
|
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 01:51:47 +02:00
|
|
|
private boolean laidOutEnough() {
|
|
|
|
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 01:51:47 +02:00
|
|
|
return totalValue >= roundState.getGameSettings()
|
|
|
|
.getInitialMeldThreshold();
|
|
|
|
}
|
2011-05-21 15:51:36 +02:00
|
|
|
|
2011-05-24 01:51:47 +02:00
|
|
|
private void endOfTurn() {
|
|
|
|
checkTurn();
|
|
|
|
roundState.nextPlayer();
|
|
|
|
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;
|
2011-05-21 15:51:36 +02:00
|
|
|
}
|
2011-05-24 01:51:47 +02:00
|
|
|
if (!laidOutEnough()) {
|
|
|
|
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 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) {
|
|
|
|
win();
|
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-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
|
|
|
|
|
|
|
private void win() {
|
2011-05-10 03:13:11 +02:00
|
|
|
for (Connection c : connections) {
|
|
|
|
c.remove();
|
|
|
|
}
|
|
|
|
endRoundEvent.emit();
|
2011-05-06 01:35:09 +02:00
|
|
|
view.enableWinPanel(true);
|
|
|
|
}
|
2011-05-04 15:20:52 +02:00
|
|
|
}
|