From 8b763a817b21aff45d704940ad2e17799dc7bb43 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 7 Jun 2011 21:51:20 +0200 Subject: Saving is working git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@382 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/control/ApplicationControl.java | 9 ++- src/jrummikub/control/GameControl.java | 18 ++++-- src/jrummikub/control/SaveControl.java | 81 +++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 src/jrummikub/control/SaveControl.java (limited to 'src/jrummikub/control') 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() { + settingsControl.getStartGameEvent().add(new IListener1() { @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() { + @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(); + } + } +} -- cgit v1.2.3