Rauskommen fertig und getestet
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@256 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
cb3f9cc011
commit
7354002de5
13 changed files with 324 additions and 181 deletions
|
@ -6,6 +6,7 @@ import javax.swing.UIManager;
|
|||
|
||||
import jrummikub.control.GameControl;
|
||||
import jrummikub.model.GameSettings;
|
||||
import jrummikub.model.IGameSettings;
|
||||
import jrummikub.model.PlayerSettings;
|
||||
import jrummikub.view.impl.View;
|
||||
|
||||
|
@ -28,7 +29,7 @@ public class JRummikub {
|
|||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
GameSettings gameSettings = new GameSettings();
|
||||
IGameSettings gameSettings = new GameSettings();
|
||||
gameSettings.getPlayerList().add(new PlayerSettings("Ida", Color.RED));
|
||||
gameSettings.getPlayerList().add(
|
||||
new PlayerSettings("Matthias", Color.YELLOW));
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package jrummikub.control;
|
||||
|
||||
import jrummikub.model.GameSettings;
|
||||
import jrummikub.model.IGameSettings;
|
||||
import jrummikub.model.RoundState;
|
||||
import jrummikub.util.IListener;
|
||||
import jrummikub.view.IView;
|
||||
|
@ -9,7 +9,7 @@ import jrummikub.view.IView;
|
|||
* Controls a Game, at some point including all Rounds, starts new Rounds
|
||||
*/
|
||||
public class GameControl {
|
||||
private GameSettings gameSettings;
|
||||
private IGameSettings gameSettings;
|
||||
private IView view;
|
||||
private RoundControl roundControl;
|
||||
|
||||
|
@ -21,7 +21,7 @@ public class GameControl {
|
|||
* @param view
|
||||
* the view
|
||||
*/
|
||||
public GameControl(GameSettings gameSettings, IView view) {
|
||||
public GameControl(IGameSettings gameSettings, IView view) {
|
||||
this.gameSettings = gameSettings;
|
||||
this.view = view;
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ import jrummikub.view.IView;
|
|||
* Controller that manages a single round of rummikub
|
||||
*/
|
||||
public class RoundControl {
|
||||
private IRoundState gameState;
|
||||
private IRoundState roundState;
|
||||
private IView view;
|
||||
private ITable clonedTable;
|
||||
private Event endRoundEvent = new Event();
|
||||
|
@ -37,8 +37,8 @@ public class RoundControl {
|
|||
* @param view
|
||||
* view used for user interaction
|
||||
*/
|
||||
public RoundControl(IRoundState gameState, IView view) {
|
||||
this.gameState = gameState;
|
||||
public RoundControl(IRoundState roundState, IView view) {
|
||||
this.roundState = roundState;
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
|
@ -69,20 +69,21 @@ public class RoundControl {
|
|||
}
|
||||
|
||||
private void prepareTurn() {
|
||||
clonedTable = (ITable) gameState.getTable().clone();
|
||||
clonedTable = (ITable) roundState.getTable().clone();
|
||||
view.enableStartTurnPanel(true);
|
||||
view.getTablePanel().setStoneSets(clonedTable);
|
||||
view.setCurrentPlayerName(gameState.getActivePlayer().getPlayerSettings().getName());
|
||||
view.setCurrentPlayerName(roundState.getActivePlayer()
|
||||
.getPlayerSettings().getName());
|
||||
view.getTablePanel().setLeftPlayerName(
|
||||
gameState.getNthNextPlayer(1).getPlayerSettings().getName());
|
||||
roundState.getNthNextPlayer(1).getPlayerSettings().getName());
|
||||
view.getTablePanel().setTopPlayerName(
|
||||
gameState.getNthNextPlayer(2).getPlayerSettings().getName());
|
||||
roundState.getNthNextPlayer(2).getPlayerSettings().getName());
|
||||
view.getTablePanel().setRightPlayerName(
|
||||
gameState.getNthNextPlayer(3).getPlayerSettings().getName());
|
||||
roundState.getNthNextPlayer(3).getPlayerSettings().getName());
|
||||
}
|
||||
|
||||
private void startTurn() {
|
||||
TurnControl turnControl = new TurnControl(gameState.getActivePlayer()
|
||||
TurnControl turnControl = new TurnControl(roundState.getActivePlayer()
|
||||
.getHand(), clonedTable, view);
|
||||
connections.add(turnControl.getEndOfTurnEvent().add(new IListener() {
|
||||
|
||||
|
@ -96,42 +97,95 @@ public class RoundControl {
|
|||
}
|
||||
|
||||
void deal() {
|
||||
for (int i = 0; i < gameState.getPlayerCount(); i++) {
|
||||
IHand hand = gameState.getNthNextPlayer(i).getHand();
|
||||
for (int i = 0; i < roundState.getPlayerCount(); i++) {
|
||||
IHand hand = roundState.getNthNextPlayer(i).getHand();
|
||||
for (int j = 0; j < 7; j++) {
|
||||
hand.drop(gameState.getGameHeap().drawStone(), new Position(j,
|
||||
hand.drop(roundState.getGameHeap().drawStone(), new Position(j,
|
||||
0));
|
||||
hand.drop(gameState.getGameHeap().drawStone(), new Position(j,
|
||||
hand.drop(roundState.getGameHeap().drawStone(), new Position(j,
|
||||
1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* after a legal move
|
||||
*
|
||||
* @return win or no win
|
||||
*/
|
||||
private boolean postLegalMove() {
|
||||
roundState.setTable(clonedTable);
|
||||
|
||||
if (roundState.getActivePlayer().getHand().getSize() == 0) {
|
||||
win();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean notLaidOutYet(Set<Stone> tableDiff) {
|
||||
boolean win = false;
|
||||
if (tableSetDifference(clonedTable, roundState.getTable()).isEmpty()) {
|
||||
// laid sthg out and didn't change table
|
||||
List<StoneSet> newSets = tableSetDifference(roundState.getTable(),
|
||||
clonedTable);
|
||||
|
||||
int totalValue = 0;
|
||||
for (StoneSet set : newSets) {
|
||||
totalValue += set.classify().getSecond();
|
||||
}
|
||||
|
||||
if (totalValue >= roundState.getGameSettings()
|
||||
.getInitialMeldThreshold()) {
|
||||
|
||||
roundState.getActivePlayer().setLaidOut(true);
|
||||
win = postLegalMove();
|
||||
return win;
|
||||
} else {
|
||||
// deal penalty, reset
|
||||
roundState.getGameHeap().putBack(tableDiff);
|
||||
dealPenalty(tableDiff.size());
|
||||
return win;
|
||||
}
|
||||
} else {
|
||||
// deal penalty, reset
|
||||
roundState.getGameHeap().putBack(tableDiff);
|
||||
dealPenalty(tableDiff.size());
|
||||
return win;
|
||||
}
|
||||
}
|
||||
|
||||
private void endOfTurn() {
|
||||
Set<Stone> tableDiff = tableDifference(gameState.getTable(),
|
||||
Set<Stone> tableDiff = tableDifference(roundState.getTable(),
|
||||
clonedTable);
|
||||
|
||||
if (!tableDiff.isEmpty()) { // Player has made a move
|
||||
if (tableDiff.isEmpty()) {
|
||||
// Player hasn't made a move
|
||||
if (clonedTable.isValid()) {
|
||||
gameState.setTable(clonedTable);
|
||||
|
||||
if (gameState.getActivePlayer().getHand().getSize() == 0) {
|
||||
win();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
gameState.getGameHeap().putBack(tableDiff);
|
||||
dealPenalty(tableDiff.size());
|
||||
roundState.setTable(clonedTable);
|
||||
}
|
||||
} else { // Player hasn't made a move
|
||||
if (clonedTable.isValid()) {
|
||||
gameState.setTable(clonedTable);
|
||||
}
|
||||
|
||||
dealStone();
|
||||
} else {
|
||||
// Player has made a move
|
||||
if (!clonedTable.isValid()) {
|
||||
// deal penalty, reset
|
||||
roundState.getGameHeap().putBack(tableDiff);
|
||||
dealPenalty(tableDiff.size());
|
||||
} else {
|
||||
if (roundState.getActivePlayer().getLaidOut()) {
|
||||
// Player has laid out
|
||||
if (postLegalMove()) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// Player hasn't laid out
|
||||
if (notLaidOutYet(tableDiff)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gameState.nextPlayer();
|
||||
roundState.nextPlayer();
|
||||
prepareTurn();
|
||||
}
|
||||
|
||||
|
@ -164,7 +218,7 @@ public class RoundControl {
|
|||
}
|
||||
|
||||
void dealStones(int count) {
|
||||
IHand hand = gameState.getActivePlayer().getHand();
|
||||
IHand hand = roundState.getActivePlayer().getHand();
|
||||
int rowCount = hand.getRowCount();
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
|
@ -172,7 +226,7 @@ public class RoundControl {
|
|||
rowCount++;
|
||||
}
|
||||
|
||||
hand.drop(gameState.getGameHeap().drawStone(), new Position(
|
||||
hand.drop(roundState.getGameHeap().drawStone(), new Position(
|
||||
Hand.WIDTH - 1, rowCount - 1));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import java.util.List;
|
|||
/**
|
||||
* The overall game settings
|
||||
*/
|
||||
public class GameSettings {
|
||||
public class GameSettings implements IGameSettings {
|
||||
private List<PlayerSettings> players = new ArrayList<PlayerSettings>();
|
||||
|
||||
private int initialMeldThreshold;
|
||||
|
@ -18,30 +18,26 @@ public class GameSettings {
|
|||
initialMeldThreshold = 30;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list containing the settings of all players
|
||||
*
|
||||
* @return the player settings list
|
||||
/* (non-Javadoc)
|
||||
* @see jrummikub.model.IGameSettings#getPlayerList()
|
||||
*/
|
||||
@Override
|
||||
public List<PlayerSettings> getPlayerList() {
|
||||
return players;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the initial meld threshold
|
||||
*
|
||||
* @param value
|
||||
* the value to set
|
||||
/* (non-Javadoc)
|
||||
* @see jrummikub.model.IGameSettings#setInitialMeldThreshold(int)
|
||||
*/
|
||||
@Override
|
||||
public void setInitialMeldThreshold(int value) {
|
||||
initialMeldThreshold = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the initial meld threshold
|
||||
*
|
||||
* @return the threshold
|
||||
/* (non-Javadoc)
|
||||
* @see jrummikub.model.IGameSettings#getInitialMeldThreshold()
|
||||
*/
|
||||
@Override
|
||||
public int getInitialMeldThreshold() {
|
||||
return initialMeldThreshold;
|
||||
}
|
||||
|
|
|
@ -25,4 +25,10 @@ public interface IPlayer {
|
|||
* @return the player settings
|
||||
*/
|
||||
public PlayerSettings getPlayerSettings();
|
||||
|
||||
/**
|
||||
* Set if the player laid out
|
||||
*
|
||||
*/
|
||||
void setLaidOut(boolean laidOut);
|
||||
}
|
|
@ -10,7 +10,7 @@ public interface IRoundState {
|
|||
*
|
||||
* @return The game settings
|
||||
*/
|
||||
public GameSettings getGameSettings();
|
||||
public IGameSettings getGameSettings();
|
||||
|
||||
/**
|
||||
* Get the current {@link Table}
|
||||
|
|
|
@ -10,7 +10,7 @@ public class Player implements IPlayer {
|
|||
* Create a new player with a given name and color
|
||||
*
|
||||
* @param settings
|
||||
* the player settings
|
||||
* the player settings
|
||||
*/
|
||||
public Player(PlayerSettings settings) {
|
||||
this.settings = settings;
|
||||
|
@ -29,6 +29,11 @@ public class Player implements IPlayer {
|
|||
return laidOut;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLaidOut(boolean laidOut) {
|
||||
this.laidOut = laidOut;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerSettings getPlayerSettings() {
|
||||
return settings;
|
||||
|
|
|
@ -5,7 +5,7 @@ import java.util.List;
|
|||
|
||||
/** Class managing the overall and momentary RoundState */
|
||||
public class RoundState implements IRoundState {
|
||||
private GameSettings gameSettings;
|
||||
private IGameSettings gameSettings;
|
||||
|
||||
private ITable table;
|
||||
private List<Player> players;
|
||||
|
@ -18,7 +18,7 @@ public class RoundState implements IRoundState {
|
|||
* @param gameSettings
|
||||
* the game settings
|
||||
*/
|
||||
public RoundState(GameSettings gameSettings) {
|
||||
public RoundState(IGameSettings gameSettings) {
|
||||
this.gameSettings = gameSettings;
|
||||
|
||||
table = new Table();
|
||||
|
@ -68,7 +68,7 @@ public class RoundState implements IRoundState {
|
|||
}
|
||||
|
||||
@Override
|
||||
public GameSettings getGameSettings() {
|
||||
public IGameSettings getGameSettings() {
|
||||
return gameSettings;
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue