Viele viele Kommentare, einige zu lange oder zu komplexe Methoden gefixt

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@417 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Ida Massow 2011-06-13 16:29:09 +02:00
parent 6ac71b62e3
commit ce7d43e365
15 changed files with 298 additions and 135 deletions

View file

@ -100,6 +100,14 @@ public class GameControl {
startRound();
}
/**
* Continues game after loading
*
* @param gameState
* the saved GameState (Players, startplayer, points)
* @param roundState
* the saved RoundState (activePlayer, Table, heap etc)
*/
public void continueGame(GameState gameState, IRoundState roundState) {
this.gameState = gameState;
if (roundState == null) {
@ -110,6 +118,11 @@ public class GameControl {
}
}
/**
* Clean abortion of a running game when another one is loaded or when a new
* one is started
*
*/
public void abortGame() {
removeListeners();
if (roundControl != null) {
@ -174,7 +187,8 @@ public class GameControl {
view.getScorePanel().setPlayers(gameSettings.getPlayerList());
view.getScorePanel().setScores(gameState.getScores());
view.getScorePanel().setAccumulatedScore(gameState.getAccumulatedScore());
view.getScorePanel().setAccumulatedScore(
gameState.getAccumulatedScore());
view.getScorePanel().update();
view.showScorePanel(true);
}

View file

@ -13,12 +13,22 @@ import jrummikub.util.IListener1;
import jrummikub.util.LoginData;
import jrummikub.view.IView;
/**
* Control checking and processing login data for network game
*
*/
public class LoginControl {
private IView view;
private Event1<LoginData> loginEvent = new Event1<LoginData>();
private Event cancelEvent = new Event();
private List<Connection> connections = new ArrayList<Connection>();
/**
* Constructor for login Control
*
* @param view
* for events which need handling
*/
public LoginControl(final IView view) {
this.view = view;
connections.add(view.getLoginPanel().getLoginEvent()
@ -30,7 +40,8 @@ public class LoginControl {
}
}));
connections.add(view.getLoginPanel().getCancelEvent().add(new IListener() {
connections.add(view.getLoginPanel().getCancelEvent()
.add(new IListener() {
@Override
public void handle() {
abort();
@ -39,18 +50,37 @@ public class LoginControl {
}));
}
/**
* Open Login panel
*
*/
public void startLogin() {
view.showLoginPanel(true);
}
/**
* Getter for loginEvent
*
* @return loginEvent
*/
public IEvent1<LoginData> getLoginEvent() {
return loginEvent;
}
/**
* Getter for cancelEvent
*
* @return cancelEvent
*/
public IEvent getCancelEvent() {
return cancelEvent;
}
/**
* Control abortion once all login data have been processed or login was
* canceled
*
*/
public void abort() {
view.showLoginPanel(false);
for (Connection c : connections) {

View file

@ -73,6 +73,9 @@ public class RoundControl {
continueRound();
}
/**
* Continue a saved round after loading
*/
public void continueRound() {
connections.add(view.getStartTurnEvent().add(new IListener() {
@Override
@ -84,6 +87,9 @@ public class RoundControl {
prepareTurn();
}
/**
* Abort round if a new one is started or a saved one is loaded
*/
public void abortRound() {
removeListeners();
if (turnControl != null) {
@ -102,11 +108,12 @@ public class RoundControl {
: BottomPanelType.COMPUTER_HAND_PANEL);
view.getTablePanel().setStoneSets(clonedTable.clone());
view.setCurrentPlayerName(roundState.getActivePlayer().getPlayerSettings()
.getName());
view.setCurrentPlayerColor(roundState.getActivePlayer().getPlayerSettings()
.getColor());
view.setCurrentPlayerHasLaidOut(roundState.getActivePlayer().getLaidOut());
view.setCurrentPlayerName(roundState.getActivePlayer()
.getPlayerSettings().getName());
view.setCurrentPlayerColor(roundState.getActivePlayer()
.getPlayerSettings().getColor());
view.setCurrentPlayerHasLaidOut(roundState.getActivePlayer()
.getLaidOut());
if (!isHuman)
startTurn();
@ -131,11 +138,11 @@ public class RoundControl {
view.getPlayerPanel().setEndTurnMode(turnMode);
}
turnControl = TurnControlFactory.getFactory(
roundState.getActivePlayer().getPlayerSettings().getTurnControlType())
.create();
roundState.getActivePlayer().getPlayerSettings()
.getTurnControlType()).create();
turnControl.setup(new ITurnControl.TurnInfo(clonedTable, clonedHand,
roundState.getActivePlayer().getLaidOut(), turnMode), roundState
.getGameSettings(), view);
roundState.getActivePlayer().getLaidOut(), turnMode),
roundState.getGameSettings(), view);
turnControl.getEndOfTurnEvent().add(new IListener() {
@Override
public void handle() {
@ -155,8 +162,10 @@ public class RoundControl {
void deal() {
for (int i = 0; i < roundState.getPlayerCount(); i++) {
IHand hand = roundState.getNthNextPlayer(i).getHand();
for (int j = 0; j < roundState.getGameSettings().getNumberOfStonesDealt(); j++) {
hand.drop(roundState.getGameHeap().drawStone(), new Position(0, 0));
for (int j = 0; j < roundState.getGameSettings()
.getNumberOfStonesDealt(); j++) {
hand.drop(roundState.getGameHeap().drawStone(), new Position(0,
0));
}
}
}
@ -167,11 +176,13 @@ public class RoundControl {
int totalValue = 0;
for (StoneSet set : newSets) {
totalValue += set.classify(roundState.getGameSettings()).getSecond();
totalValue += set.classify(roundState.getGameSettings())
.getSecond();
}
return totalValue == 0
|| totalValue >= roundState.getGameSettings().getInitialMeldThreshold();
|| totalValue >= roundState.getGameSettings()
.getInitialMeldThreshold();
}
private void endOfTurn() {
@ -211,7 +222,8 @@ public class RoundControl {
}
if (!roundState.getActivePlayer().getLaidOut()) {
// Player touched forbidden stones
if (!tableSetDifference(clonedTable, roundState.getTable()).isEmpty()) {
if (!tableSetDifference(clonedTable, roundState.getTable())
.isEmpty()) {
rejectMove();
return;
}
@ -220,7 +232,8 @@ public class RoundControl {
return;
}
}
Set<Stone> tableDiff = tableDifference(roundState.getTable(), clonedTable);
Set<Stone> tableDiff = tableDifference(roundState.getTable(),
clonedTable);
roundState.setTable(clonedTable);
@ -236,7 +249,8 @@ public class RoundControl {
}
private void rejectMove() {
Set<Stone> tableDiff = tableDifference(roundState.getTable(), clonedTable);
Set<Stone> tableDiff = tableDifference(roundState.getTable(),
clonedTable);
// deal penalty, reset
roundState.getGameHeap().putBack(tableDiff);
dealPenalty(tableDiff.size());
@ -330,10 +344,12 @@ public class RoundControl {
stonePoints = playerHand.isInitialMeldPossible(roundState
.getGameSettings()) ? 200 : 100;
} else {
stonePoints = playerHand.getStonePoints(roundState.getGameSettings());
stonePoints = playerHand.getStonePoints(roundState
.getGameSettings());
}
bestScore = updateBestScore(bestScore, -stonePoints, playerHand.getSize());
bestScore = updateBestScore(bestScore, -stonePoints,
playerHand.getSize());
points.add(-stonePoints);
pointSum += stonePoints;

View file

@ -45,6 +45,11 @@ public class SaveControl {
});
}
/**
* Getter for loadEvent
*
* @return loadEvent
*/
public IEvent3<GameSettings, GameState, IRoundState> getLoadEvent() {
return loadEvent;
}
@ -110,8 +115,8 @@ public class SaveControl {
return;
}
try {
ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(
file));
ObjectOutputStream stream = new ObjectOutputStream(
new FileOutputStream(file));
stream.writeObject(gameSettings);
stream.writeObject(gameState);

View file

@ -44,8 +44,8 @@ public class SettingsControl {
}
/**
* the start game event is emitted when the user wants to start a game and the
* settings made are valid
* the start game event is emitted when the user wants to start a game and
* the settings made are valid
*
* @return the event
*/
@ -78,7 +78,8 @@ public class SettingsControl {
private void addOptionListeners1() {
connections.add(view.getSettingsPanel()
.getChangeInitialMeldThresholdEvent().add(new IListener1<Integer>() {
.getChangeInitialMeldThresholdEvent()
.add(new IListener1<Integer>() {
@Override
public void handle(Integer value) {
settings.setInitialMeldThreshold(value);
@ -103,7 +104,8 @@ public class SettingsControl {
update();
}
}));
connections.add(view.getSettingsPanel().getChangeNumberOfStonesDealtEvent()
connections.add(view.getSettingsPanel()
.getChangeNumberOfStonesDealtEvent()
.add(new IListener1<Integer>() {
@Override
public void handle(Integer value) {
@ -126,7 +128,7 @@ public class SettingsControl {
.add(new IListener1<Integer>() {
@Override
public void handle(Integer value) {
settings.setTime(value);
settings.setTotalTime(value);
update();
}
}));
@ -147,6 +149,13 @@ public class SettingsControl {
update();
}
}));
addVariantListeners();
}
/**
* Add event listeners for variants, e.g. standard or children
*/
public void addVariantListeners() {
connections.add(view.getSettingsPanel().getSetVariantDefaultEvent()
.add(new IListener() {
@Override
@ -162,7 +171,7 @@ public class SettingsControl {
settings.reset();
settings.setHighestValue(10);
settings.setJokerNumber(6);
settings.setTime(120);
settings.setTotalTime(120);
settings.setInitialMeldThreshold(20);
update();
}
@ -239,7 +248,8 @@ public class SettingsControl {
break;
}
settings.getPlayerList().add(new PlayerSettings("Spieler " + num, color));
settings.getPlayerList().add(
new PlayerSettings("Spieler " + num, color));
update();
}
@ -280,7 +290,8 @@ public class SettingsControl {
protected void update() {
view.getSettingsPanel().enableRemovePlayerButtons(
settings.getPlayerList().size() > 2);
view.getSettingsPanel().enableAddPlayerButton(
view.getSettingsPanel()
.enableAddPlayerButton(
settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length);
checkSettings();
@ -302,7 +313,8 @@ public class SettingsControl {
for (int j = i + 1; j < settings.getPlayerList().size(); ++j) {
if (settings.getPlayerList().get(j).getName().equals(name)) {
view.getSettingsPanel().setError(
view.getSettingsPanel()
.setError(
ISettingsPanel.SettingsError.DUPLICATE_PLAYER_NAME_ERROR);
view.getSettingsPanel().enableStartGameButton(false);
return false;
@ -312,7 +324,8 @@ public class SettingsControl {
int totalStonesDealt = settings.getNumberOfStonesDealt()
* settings.getPlayerList().size();
int totalStones = settings.getHighestValue() * settings.getStoneSetNumber()
int totalStones = settings.getHighestValue()
* settings.getStoneSetNumber()
* settings.getStoneColors().size() + settings.getJokerNumber();
if (totalStones <= totalStonesDealt) {
@ -368,6 +381,9 @@ public class SettingsControl {
startGameEvent.emit(settings);
}
/**
* Abort settings control once settings are set
*/
public void abort() {
view.showSettingsPanel(false);
for (Connection c : connections) {

View file

@ -24,6 +24,8 @@ public class TurnTimer implements ActionListener, ITurnTimer {
*
* @param view
* view to display
* @param totalTime
* total time for turn
*/
public TurnTimer(IView view, int totalTime) {
this.view = view;

View file

@ -17,6 +17,9 @@ import jrummikub.view.IGameListPanel;
import jrummikub.view.IGameListPanel.GameData;
import jrummikub.view.IView;
/**
* Class dealing with network connection, offering and choice of network games
*/
public class NetworkControl {
private ConnectionControl connectionControl;
private IView view;
@ -27,14 +30,69 @@ public class NetworkControl {
private Map<UUID, GameData> gameMap = new HashMap<UUID, GameData>();
/**
* Creates a new network control
*
* @param loginData
* user's login data
* @param view
* for events and handlers
*/
public NetworkControl(final LoginData loginData, final IView view) {
this.view = view;
connectionControl = new ConnectionControl(loginData);
connections.add(connectionControl.getConnectedEvent().add(new IListener() {
addConnectionControlListeners(loginData, view);
connections.add(view.getGameListPanel().getJoinEvent()
.add(new IListener1<IGameListPanel.GameData>() {
@Override
public void handle(GameData value) {
// TODO Auto-generated method stub
}
}));
connections.add(view.getGameListPanel().getOpenNewGameEvent()
.add(new IListener() {
@Override
public void handle() {
view.getGameListPanel().setChannelName(loginData.getChannelName());
if (settingsControl == null) {
view.showGameListPanel(false);
settingsControl = new NetworkSettingsControl(
connectionControl, view, new GameSettings());
settingsControl.startSettings();
}
}
}));
connections.add(view.getGameListPanel().getCancelEvent()
.add(new IListener() {
@Override
public void handle() {
abort();
stopNetworkEvent.emit();
}
}));
}
/**
* Adds the listeners for connection control events
*
* @param loginData
* player's login data
* @param view
* view for events
*/
public void addConnectionControlListeners(final LoginData loginData,
final IView view) {
connections.add(connectionControl.getConnectedEvent().add(
new IListener() {
@Override
public void handle() {
view.getGameListPanel().setChannelName(
loginData.getChannelName());
view.showGameListPanel(true);
}
}));
@ -76,44 +134,18 @@ public class NetworkControl {
}
}
}));
connections.add(view.getGameListPanel().getJoinEvent()
.add(new IListener1<IGameListPanel.GameData>() {
@Override
public void handle(GameData value) {
// TODO Auto-generated method stub
}
}));
connections.add(view.getGameListPanel().getOpenNewGameEvent()
.add(new IListener() {
@Override
public void handle() {
if (settingsControl == null) {
view.showGameListPanel(false);
settingsControl = new NetworkSettingsControl(connectionControl,
view, new GameSettings());
settingsControl.startSettings();
}
}
}));
connections.add(view.getGameListPanel().getCancelEvent()
.add(new IListener() {
@Override
public void handle() {
abort();
stopNetworkEvent.emit();
}
}));
}
/**
* Starts a new network connection with the sepcified data
*/
public void startNetwork() {
connectionControl.connect();
}
/**
* Ends the network connection if canceled
*/
public void abort() {
for (Connection c : connections) {
c.remove();
@ -122,6 +154,11 @@ public class NetworkControl {
view.showGameListPanel(false);
}
/**
* Getter for stopNetworkEvent
*
* @return stopNetworkEvent
*/
public IEvent getStopNetworkEvent() {
return stopNetworkEvent;
}

View file

@ -7,11 +7,25 @@ import jrummikub.model.GameSettings;
import jrummikub.view.IGameListPanel.GameData;
import jrummikub.view.IView;
/**
* Settings for network games, entered by host
*/
public class NetworkSettingsControl extends SettingsControl {
private GameData gameData = new GameData(UUID.randomUUID(), settings);
private ConnectionControl connectionControl;
public NetworkSettingsControl(ConnectionControl connectionControl, IView view, GameSettings settings) {
/**
* Creates a new network settings control
*
* @param connectionControl
* for gameOffers
* @param view
* for events and handlers
* @param settings
* for playerNumber, color...
*/
public NetworkSettingsControl(ConnectionControl connectionControl,
IView view, GameSettings settings) {
super(view, settings);
this.connectionControl = connectionControl;
}

View file

@ -52,7 +52,7 @@ public abstract class AbstractTurnControl implements ITurnControl {
this.settings = settings;
this.view = view;
if (timer == null) {
timer = new TurnTimer(view, settings.getTime());
timer = new TurnTimer(view, settings.getTotalTime());
}
connections.add(timer.getTimeRunOutEvent().add(new IListener() {
@Override

View file

@ -23,7 +23,7 @@ public class GameSettings implements Serializable {
private int highestValue;
private int stoneSetNumber;
private int numberOfStonesDealt;
private int time;
private int totalTime;
private boolean noLimits;
private HashSet<StoneColor> stoneColors;
@ -34,6 +34,9 @@ public class GameSettings implements Serializable {
reset();
}
/**
* Reset the game settings to the default values
*/
public void reset() {
initialMeldThreshold = 30;
jokerPoints = 50;
@ -41,7 +44,7 @@ public class GameSettings implements Serializable {
highestValue = 13;
stoneSetNumber = 2;
numberOfStonesDealt = 14;
time = 60;
totalTime = 60;
noLimits = false;
stoneColors = new HashSet<StoneColor>(Arrays.asList(BLACK, BLUE,
ORANGE, RED));
@ -151,12 +154,23 @@ public class GameSettings implements Serializable {
this.stoneSetNumber = stoneSets;
}
public int getTime() {
return time;
/**
* Getter for the time for a turn
*
* @return time for a turn
*/
public int getTotalTime() {
return totalTime;
}
public void setTime(int time) {
this.time = time;
/**
* Setter for the time for a turn
*
* @param totalTime
* for a turn
*/
public void setTotalTime(int totalTime) {
this.totalTime = totalTime;
}
/**

View file

@ -84,6 +84,23 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
}
if (nonJoker == -1) {
return classifyJokersOnly(settings, nonJoker);
}
int runScore = isValidRun(nonJoker, settings);
int groupScore = isValidGroup(stones.get(nonJoker).getValue(), settings);
if (runScore > groupScore) {
return new Pair<Type, Integer>(RUN, runScore);
} else if (groupScore == 0) {
return new Pair<Type, Integer>(INVALID, 0);
} else {
return new Pair<Type, Integer>(GROUP, groupScore);
}
}
private Pair<Type, Integer> classifyJokersOnly(GameSettings settings,
int nonJoker) {
if (stones.size() > settings.getHighestValue()
&& stones.size() > settings.getStoneColors().size()
&& !settings.isNoLimits()) {
@ -105,18 +122,6 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
}
}
int runScore = isValidRun(nonJoker, settings);
int groupScore = isValidGroup(stones.get(nonJoker).getValue(), settings);
if (runScore > groupScore) {
return new Pair<Type, Integer>(RUN, runScore);
} else if (groupScore == 0) {
return new Pair<Type, Integer>(INVALID, 0);
} else {
return new Pair<Type, Integer>(GROUP, groupScore);
}
}
/**
* Test for rule conflict within the StoneSet, assuming we have a run
*

View file

@ -9,6 +9,8 @@ import java.util.HashSet;
* type of the first event parameter
* @param <T2>
* type of the second event parameter
* @param <T3>
* type of third event parameter
*/
public class Event3<T1, T2, T3> implements IEvent3<T1, T2, T3> {
private HashSet<IListener3<T1, T2, T3>> listeners = new HashSet<IListener3<T1, T2, T3>>();
@ -37,6 +39,8 @@ public class Event3<T1, T2, T3> implements IEvent3<T1, T2, T3> {
* the first event parameter
* @param value2
* the second event parameter
* @param value3
* the third event parameter
*/
public void emit(T1 value1, T2 value2, T3 value3) {
for (IListener3<T1, T2, T3> listener : listeners) {

View file

@ -7,6 +7,8 @@ package jrummikub.util;
* type of the first event parameter
* @param <T2>
* type of the second event parameter
* @param <T3>
* type of third event parameter
*/
public interface IEvent3<T1, T2, T3> {
/**

View file

@ -7,7 +7,9 @@ package jrummikub.util;
* @param <T1>
* type of the first event parameter
* @param <T2>
* type of the first event parameter
* type of the second event parameter
* @param <T3>
* type of third event parameter
*/
public interface IListener3<T1, T2, T3> {
/**
@ -17,6 +19,8 @@ public interface IListener3<T1, T2, T3> {
* the first event parameter
* @param value2
* the second event parameter
* @param value3
* the third event parameter
*/
public void handle(T1 value1, T2 value2, T3 value3);
}

View file

@ -265,7 +265,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
highestValueSpinner.setValue(gameSettings.getHighestValue());
numberOfStonesDealtSpinner.setValue(gameSettings.getNumberOfStonesDealt());
jokerNumberSpinner.setValue(gameSettings.getJokerNumber());
timeSpinner.setValue(gameSettings.getTime());
timeSpinner.setValue(gameSettings.getTotalTime());
noLimitsBox.setSelected(gameSettings.isNoLimits());
for (StoneColor color : StoneColor.values()) {