From fc7783bebc429a2ba368f96be8ced16c9d7509d4 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Mon, 9 May 2016 15:15:10 +0200 Subject: Basic Graphical Simulation --- .../xblast/simulation/GraphicalSimulation.java | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 test/ch/epfl/xblast/simulation/GraphicalSimulation.java (limited to 'test/ch/epfl') diff --git a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java new file mode 100644 index 0000000..1a0d88f --- /dev/null +++ b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java @@ -0,0 +1,57 @@ +package ch.epfl.xblast.simulation; + +import ch.epfl.xblast.PlayerID; +import ch.epfl.xblast.client.*; +import ch.epfl.xblast.server.*; +import ch.epfl.xblast.server.GameState; +import ch.epfl.xblast.server.debug.RandomEventGenerator; +import ch.epfl.xblast.server.painter.BoardPainter; + +import javax.swing.*; +import java.util.List; + +/** + * @author Timothée FLOURE (257420) + */ +public class GraphicalSimulation { + private static final GameState INITIAL_GAMESTATE = Level.DEFAULT_LEVEL.initialState(); + private static final BoardPainter BOARD_PAINTER = Level.DEFAULT_LEVEL.painter(); + + private static final int SEED = 2016; + private static final int SPEED_CHANGE_PROB = 30; + private static final int BOMB_PROB = 100; + private static final RandomEventGenerator RANDOM_EVENT_GENERATOR = new RandomEventGenerator(SEED, SPEED_CHANGE_PROB, BOMB_PROB); + + private static ch.epfl.xblast.client.GameState getClientData(GameState gs) { + List serializedGameState = GameStateSerializer.serialize(BOARD_PAINTER, gs); + return GameStateDeserializer.deserialize(serializedGameState); + } + + private static boolean isSimulationOver(GameState gs) { + return gs == null || gs.isGameOver(); + } + + private static GameState nextGameState(GameState gs) { + return gs.next(RANDOM_EVENT_GENERATOR.randomSpeedChangeEvents(), RANDOM_EVENT_GENERATOR.randomBombDropEvents()); + } + + private static void displayGameState(ch.epfl.xblast.client.GameState gs, XBlastComponent gui) { + gui.setGameState(gs, PlayerID.PLAYER_1); + } + + private static JFrame buildFrame() { + JFrame frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setVisible(true); + return frame; + } + + public static void main(String[] args) { + JFrame frame = buildFrame(); + XBlastComponent xblast = new XBlastComponent(); + frame.setContentPane(xblast); + + for (GameState gs = INITIAL_GAMESTATE; !isSimulationOver(gs); gs = nextGameState(gs)) + displayGameState(getClientData(gs), xblast); + } +} -- cgit v1.2.3