summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2011-06-08 21:58:16 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2011-06-08 21:58:16 +0200
commit45d5b3ae10ed8cfbecb5489636093c6fb0576970 (patch)
tree1aa1c012f46fe204d997e8c6896940800f5461bb /src/jrummikub/control
parent2e376414b941da3c6fa3c20ddad085c695175542 (diff)
downloadJRummikub-45d5b3ae10ed8cfbecb5489636093c6fb0576970.tar
JRummikub-45d5b3ae10ed8cfbecb5489636093c6fb0576970.zip
Implement pause function
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@390 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/control')
-rw-r--r--src/jrummikub/control/ApplicationControl.java14
-rw-r--r--src/jrummikub/control/GameControl.java9
-rw-r--r--src/jrummikub/control/RoundControl.java50
-rw-r--r--src/jrummikub/control/SaveControl.java12
-rw-r--r--src/jrummikub/control/TurnTimer.java2
-rw-r--r--src/jrummikub/control/turn/AbstractTurnControl.java32
-rw-r--r--src/jrummikub/control/turn/BaseAIControl.java26
-rw-r--r--src/jrummikub/control/turn/HumanTurnControl.java33
-rw-r--r--src/jrummikub/control/turn/ITurnControl.java10
-rw-r--r--src/jrummikub/control/turn/TurnControlFactory.java8
10 files changed, 102 insertions, 94 deletions
diff --git a/src/jrummikub/control/ApplicationControl.java b/src/jrummikub/control/ApplicationControl.java
index e4ddb55..e00952d 100644
--- a/src/jrummikub/control/ApplicationControl.java
+++ b/src/jrummikub/control/ApplicationControl.java
@@ -22,7 +22,7 @@ public class ApplicationControl {
* Creates a new application control
*
* @param view
- * the view to use
+ * the view to use
*/
public ApplicationControl(IView view) {
this.view = view;
@@ -51,14 +51,13 @@ public class ApplicationControl {
new IListener3<GameSettings, GameState, IRoundState>() {
@Override
- public void handle(GameSettings settings,
- GameState gameState, IRoundState roundState) {
+ public void handle(GameSettings settings, GameState gameState,
+ IRoundState roundState) {
settingsControl.abort();
- if (gameControl != null){
+ if (gameControl != null) {
gameControl.abortGame();
}
- gameControl = new GameControl(settings,
- saveControl, view);
+ gameControl = new GameControl(settings, saveControl, view);
addGameControlListeners(gameControl);
gameControl.continueGame(gameState, roundState);
@@ -70,8 +69,7 @@ public class ApplicationControl {
public void handle(GameSettings settings) {
saveControl.setGameSettings(settings);
- gameControl = new GameControl(settings,
- saveControl, view);
+ gameControl = new GameControl(settings, saveControl, view);
addGameControlListeners(gameControl);
gameControl.startGame();
diff --git a/src/jrummikub/control/GameControl.java b/src/jrummikub/control/GameControl.java
index 194d0de..e00f136 100644
--- a/src/jrummikub/control/GameControl.java
+++ b/src/jrummikub/control/GameControl.java
@@ -33,11 +33,11 @@ public class GameControl {
* Constructor
*
* @param gameSettings
- * the game settings
+ * the game settings
* @param saveControl
- * the save control
+ * the save control
* @param view
- * the view
+ * the view
*/
public GameControl(GameSettings gameSettings, SaveControl saveControl,
IView view) {
@@ -174,8 +174,7 @@ 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);
}
diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java
index e1009af..5371b0c 100644
--- a/src/jrummikub/control/RoundControl.java
+++ b/src/jrummikub/control/RoundControl.java
@@ -47,9 +47,9 @@ public class RoundControl {
* Create a new RoundControl using the given gameState and view
*
* @param roundState
- * initial round state
+ * initial round state
* @param view
- * view used for user interaction
+ * view used for user interaction
*/
public RoundControl(IRoundState roundState, IView view) {
this.roundState = roundState;
@@ -102,12 +102,11 @@ 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();
@@ -132,11 +131,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() {
@@ -156,10 +155,8 @@ 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));
}
}
}
@@ -170,13 +167,11 @@ 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() {
@@ -216,8 +211,7 @@ 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;
}
@@ -226,8 +220,7 @@ public class RoundControl {
return;
}
}
- Set<Stone> tableDiff = tableDifference(roundState.getTable(),
- clonedTable);
+ Set<Stone> tableDiff = tableDifference(roundState.getTable(), clonedTable);
roundState.setTable(clonedTable);
@@ -243,8 +236,7 @@ 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());
@@ -338,12 +330,10 @@ 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;
diff --git a/src/jrummikub/control/SaveControl.java b/src/jrummikub/control/SaveControl.java
index e0589e6..1412199 100644
--- a/src/jrummikub/control/SaveControl.java
+++ b/src/jrummikub/control/SaveControl.java
@@ -27,7 +27,7 @@ public class SaveControl {
* Creates a new SaveControl
*
* @param view
- * the view to use
+ * the view to use
*/
public SaveControl(IView view) {
view.getSaveEvent().add(new IListener1<File>() {
@@ -53,7 +53,7 @@ public class SaveControl {
* Sets the current game settings
*
* @param gameSettings
- * the game settings
+ * the game settings
*/
public void setGameSettings(GameSettings gameSettings) {
this.gameSettings = gameSettings;
@@ -63,7 +63,7 @@ public class SaveControl {
* Sets the current game state
*
* @param gameState
- * the game state
+ * the game state
*/
public void setGameState(GameState gameState) {
this.gameState = gameState;
@@ -73,7 +73,7 @@ public class SaveControl {
* Sets the current round state
*
* @param roundState
- * the round state
+ * the round state
*/
public void setRoundState(IRoundState roundState) {
this.roundState = roundState;
@@ -110,8 +110,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);
diff --git a/src/jrummikub/control/TurnTimer.java b/src/jrummikub/control/TurnTimer.java
index 17bdf0f..77c5b55 100644
--- a/src/jrummikub/control/TurnTimer.java
+++ b/src/jrummikub/control/TurnTimer.java
@@ -22,7 +22,7 @@ public class TurnTimer implements ActionListener, ITurnTimer {
* Create a new timer using a given view to display the current time left
*
* @param view
- * view to display
+ * view to display
*/
public TurnTimer(IView view) {
this.view = view;
diff --git a/src/jrummikub/control/turn/AbstractTurnControl.java b/src/jrummikub/control/turn/AbstractTurnControl.java
index ae1e155..84c478a 100644
--- a/src/jrummikub/control/turn/AbstractTurnControl.java
+++ b/src/jrummikub/control/turn/AbstractTurnControl.java
@@ -16,7 +16,6 @@ import jrummikub.view.IView;
* Abstract base class for TurnControls
*/
public abstract class AbstractTurnControl implements ITurnControl {
-
protected Event endOfTurnEvent = new Event();
protected Event redealEvent = new Event();
protected TurnInfo turnInfo;
@@ -24,8 +23,7 @@ public abstract class AbstractTurnControl implements ITurnControl {
protected IView view;
protected ITurnTimer timer;
protected List<Connection> connections = new ArrayList<Connection>();
-
-
+
@Override
public IEvent getEndOfTurnEvent() {
return endOfTurnEvent;
@@ -35,7 +33,17 @@ public abstract class AbstractTurnControl implements ITurnControl {
public IEvent getRedealEvent() {
return redealEvent;
}
-
+
+ private void pauseTurn() {
+ timer.stopTimer();
+ view.enablePauseMode(true);
+ }
+
+ private void resumeTurn() {
+ timer.startTimer();
+ view.enablePauseMode(false);
+ }
+
protected abstract void timeOut();
@Override
@@ -52,15 +60,27 @@ public abstract class AbstractTurnControl implements ITurnControl {
timeOut();
}
}));
+ connections.add(view.getPauseEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ pauseTurn();
+ }
+ }));
+ connections.add(view.getEndPauseEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ resumeTurn();
+ }
+ }));
}
-
+
protected void cleanUp() {
timer.stopTimer();
for (Connection c : connections) {
c.remove();
}
}
-
+
public void abortTurn() {
cleanUp();
}
diff --git a/src/jrummikub/control/turn/BaseAIControl.java b/src/jrummikub/control/turn/BaseAIControl.java
index 43ade18..11519c3 100644
--- a/src/jrummikub/control/turn/BaseAIControl.java
+++ b/src/jrummikub/control/turn/BaseAIControl.java
@@ -36,7 +36,7 @@ public class BaseAIControl extends AbstractTurnControl {
timer.startTimer();
computeThread.start();
}
-
+
protected void timeOut() {
cleanUp();
endOfTurnEvent.emit();
@@ -49,15 +49,15 @@ public class BaseAIControl extends AbstractTurnControl {
private void compute() {
switch (turnInfo.getTurnMode()) {
- case MAY_REDEAL:
- emitRedeal();
- break;
- case INSPECT_ONLY:
- emitEndOfTurn();
- break;
- case NORMAL_TURN:
- turn();
- break;
+ case MAY_REDEAL:
+ emitRedeal();
+ break;
+ case INSPECT_ONLY:
+ emitEndOfTurn();
+ break;
+ case NORMAL_TURN:
+ turn();
+ break;
}
}
@@ -111,8 +111,10 @@ public class BaseAIControl extends AbstractTurnControl {
for (Stone stone : set) {
handStones.add(pickUpMatchingStone(stone));
}
- turnInfo.getTable().drop(new StoneSet(handStones), new Position(
- (float) Math.random() * 30 - 15, (float) Math.random() * 6 - 3));
+ turnInfo.getTable().drop(
+ new StoneSet(handStones),
+ new Position((float) Math.random() * 30 - 15,
+ (float) Math.random() * 6 - 3));
}
emitEndOfTurn();
diff --git a/src/jrummikub/control/turn/HumanTurnControl.java b/src/jrummikub/control/turn/HumanTurnControl.java
index 02541d2..2ab7b21 100644
--- a/src/jrummikub/control/turn/HumanTurnControl.java
+++ b/src/jrummikub/control/turn/HumanTurnControl.java
@@ -48,7 +48,7 @@ public class HumanTurnControl extends AbstractTurnControl {
HumanTurnControl(ITurnTimer testTimer) {
this.timer = testTimer;
}
-
+
protected void timeOut() {
endOfTurn(false);
}
@@ -97,13 +97,12 @@ public class HumanTurnControl extends AbstractTurnControl {
}
}));
- connections.add(view.getPlayerPanel().getRedealEvent()
- .add(new IListener() {
- @Override
- public void handle() {
- endOfTurn(true);
- }
- }));
+ connections.add(view.getPlayerPanel().getRedealEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ endOfTurn(true);
+ }
+ }));
}
private void addHandPanelHandlers() {
@@ -304,8 +303,10 @@ public class HumanTurnControl extends AbstractTurnControl {
}
pickUpSelectedStones();
- turnInfo.getTable().drop(new StoneSet(selectedStones), new Position(position.getX()
- - selectedStones.size() * 0.5f, position.getY() - 0.5f));
+ turnInfo.getTable().drop(
+ new StoneSet(selectedStones),
+ new Position(position.getX() - selectedStones.size() * 0.5f, position
+ .getY() - 0.5f));
selectedStones.clear();
view.getTablePanel().setStoneSets(turnInfo.getTable());
@@ -421,15 +422,14 @@ public class HumanTurnControl extends AbstractTurnControl {
turnInfo.getTable().drop(joinedSet, newPos);
} else {
StoneSet joinedSet = new StoneSet(selectedStones).join(newSet);
- turnInfo.getTable().drop(joinedSet, new Position(newPos.getX()
- - selectedStones.size(), newPos.getY()));
+ turnInfo.getTable().drop(joinedSet,
+ new Position(newPos.getX() - selectedStones.size(), newPos.getY()));
}
} else {
turnInfo.getTable().drop(
new StoneSet(selectedStones),
- new Position(pos.getX()
- + (set.getSize() - selectedStones.size()) * 0.5f,
- pos.getY()));
+ new Position(pos.getX() + (set.getSize() - selectedStones.size())
+ * 0.5f, pos.getY()));
}
selectedStones.clear();
@@ -521,8 +521,7 @@ public class HumanTurnControl extends AbstractTurnControl {
static class HandStonePositionComparator implements
Comparator<Pair<Stone, Position>> {
@Override
- public int compare(Pair<Stone, Position> pair1,
- Pair<Stone, Position> pair2) {
+ public int compare(Pair<Stone, Position> pair1, Pair<Stone, Position> pair2) {
Position pos1 = pair1.getSecond(), pos2 = pair2.getSecond();
if (pos1.getY() < pos2.getY()) {
return -1;
diff --git a/src/jrummikub/control/turn/ITurnControl.java b/src/jrummikub/control/turn/ITurnControl.java
index 5a3d550..63290d5 100644
--- a/src/jrummikub/control/turn/ITurnControl.java
+++ b/src/jrummikub/control/turn/ITurnControl.java
@@ -44,6 +44,11 @@ public interface ITurnControl {
public void startTurn();
/**
+ * Abort the turn
+ */
+ public void abortTurn();
+
+ /**
* The TurnInfo class encapsulates all information concerning the current turn
*/
public class TurnInfo {
@@ -108,9 +113,4 @@ public interface ITurnControl {
return turnMode;
}
}
-
- /**
- * Abort the turn
- */
- public void abortTurn();
} \ No newline at end of file
diff --git a/src/jrummikub/control/turn/TurnControlFactory.java b/src/jrummikub/control/turn/TurnControlFactory.java
index 95396aa..b42f7ff 100644
--- a/src/jrummikub/control/turn/TurnControlFactory.java
+++ b/src/jrummikub/control/turn/TurnControlFactory.java
@@ -32,10 +32,10 @@ public abstract class TurnControlFactory {
*/
static public TurnControlFactory getFactory(Type type) {
switch (type) {
- case HUMAN:
- return HumanTurnControl.getFactory();
- case COMPUTER:
- return BaseAIControl.getFactory();
+ case HUMAN:
+ return HumanTurnControl.getFactory();
+ case COMPUTER:
+ return BaseAIControl.getFactory();
}
return null;
}