summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/control')
-rw-r--r--src/jrummikub/control/ApplicationControl.java9
-rw-r--r--src/jrummikub/control/GameControl.java18
-rw-r--r--src/jrummikub/control/SaveControl.java81
3 files changed, 100 insertions, 8 deletions
diff --git a/src/jrummikub/control/ApplicationControl.java b/src/jrummikub/control/ApplicationControl.java
index 0f73310..a0bdf35 100644
--- a/src/jrummikub/control/ApplicationControl.java
+++ b/src/jrummikub/control/ApplicationControl.java
@@ -11,6 +11,7 @@ import jrummikub.view.IView.BottomPanelType;
* game control
*/
public class ApplicationControl {
+ private SaveControl saveControl;
private IView view;
/**
@@ -21,6 +22,7 @@ public class ApplicationControl {
*/
public ApplicationControl(IView view) {
this.view = view;
+ saveControl = new SaveControl(view);
view.getMenuQuitEvent().add(new IListener() {
@Override
@@ -38,11 +40,13 @@ public class ApplicationControl {
view.setBottomPanel(BottomPanelType.START_GAME_PANEL);
SettingsControl settingsControl = new SettingsControl(view,
new GameSettings());
- settingsControl.getStartGameEvent().add(new IListener1<GameSettings>() {
+ settingsControl.getStartGameEvent().add(new IListener1<GameSettings>() {
@Override
public void handle(GameSettings settings) {
- GameControl gameControl = new GameControl(settings, view);
+ saveControl.setGameSettings(settings);
+
+ GameControl gameControl = new GameControl(settings, saveControl, view);
gameControl.getEndOfGameEvent().add(new IListener() {
@Override
public void handle() {
@@ -55,5 +59,4 @@ public class ApplicationControl {
});
settingsControl.startSettings();
}
-
}
diff --git a/src/jrummikub/control/GameControl.java b/src/jrummikub/control/GameControl.java
index 247e67a..fb3fcf8 100644
--- a/src/jrummikub/control/GameControl.java
+++ b/src/jrummikub/control/GameControl.java
@@ -20,6 +20,8 @@ import jrummikub.view.IView.BottomPanelType;
* Controls a Game, at some point including all Rounds, starts new Rounds
*/
public class GameControl {
+ private SaveControl saveControl;
+
private GameSettings gameSettings;
private IView view;
RoundControl roundControl;
@@ -31,15 +33,21 @@ public class GameControl {
* Constructor
*
* @param gameSettings
- * the game settings
+ * the game settings
+ * @param saveControl
+ * the save control
* @param view
- * the view
+ * the view
*/
- public GameControl(GameSettings gameSettings, IView view) {
+ public GameControl(GameSettings gameSettings, SaveControl saveControl,
+ IView view) {
this.gameSettings = gameSettings;
+ this.saveControl = saveControl;
this.view = view;
gameState = new GameState();
+ saveControl.setGameState(gameState);
+
gameState.setFirstRoundFirstPlayer((int) (Math.random() * gameSettings
.getPlayerList().size()));
@@ -96,6 +104,7 @@ public class GameControl {
view.showScorePanel(false);
IRoundState roundState = new RoundState(gameSettings);
+ saveControl.setRoundState(roundState);
roundState.setActivePlayerNumber(gameState.getFirstRoundFirstPlayer()
+ gameState.getScores().size());
@@ -133,8 +142,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/SaveControl.java b/src/jrummikub/control/SaveControl.java
new file mode 100644
index 0000000..458c0dc
--- /dev/null
+++ b/src/jrummikub/control/SaveControl.java
@@ -0,0 +1,81 @@
+package jrummikub.control;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.ObjectOutputStream;
+
+import jrummikub.model.GameSettings;
+import jrummikub.model.GameState;
+import jrummikub.model.IRoundState;
+import jrummikub.util.IListener1;
+import jrummikub.view.IView;
+
+/**
+ * The save control is responsible for loading and saving game and round states
+ */
+public class SaveControl {
+ private GameSettings gameSettings;
+ private GameState gameState;
+ private IRoundState roundState;
+
+ /**
+ * Creates a new SaveControl
+ *
+ * @param view
+ * the view to use
+ */
+ public SaveControl(IView view) {
+ view.getSaveEvent().add(new IListener1<File>() {
+ @Override
+ public void handle(File file) {
+ save(file);
+ }
+ });
+ }
+
+ /**
+ * Sets the current game settings
+ *
+ * @param gameSettings
+ * the game settings
+ */
+ public void setGameSettings(GameSettings gameSettings) {
+ this.gameSettings = gameSettings;
+ }
+
+ /**
+ * Sets the current game state
+ *
+ * @param gameState
+ * the game state
+ */
+ public void setGameState(GameState gameState) {
+ this.gameState = gameState;
+ }
+
+ /**
+ * Sets the current round state
+ *
+ * @param roundState
+ * the round state
+ */
+ public void setRoundState(IRoundState roundState) {
+ this.roundState = roundState;
+ }
+
+ private void save(File file) {
+ try {
+ ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(
+ file));
+
+ stream.writeObject(gameSettings);
+ stream.writeObject(gameState);
+ stream.writeObject(roundState);
+ stream.flush();
+
+ stream.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+}