From e9b85677fd581791bb1b5dffbe7261a876879084 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 9 May 2016 19:43:30 +0200 Subject: Refactor simulations --- .../epfl/xblast/simulation/ConsoleSimulation.java | 67 ++----------------- .../xblast/simulation/GraphicalSimulation.java | 78 +++++++++++----------- test/ch/epfl/xblast/simulation/Simulation.java | 54 +++++++++++++++ 3 files changed, 98 insertions(+), 101 deletions(-) create mode 100644 test/ch/epfl/xblast/simulation/Simulation.java (limited to 'test/ch/epfl') diff --git a/test/ch/epfl/xblast/simulation/ConsoleSimulation.java b/test/ch/epfl/xblast/simulation/ConsoleSimulation.java index 35c0eb8..b029afb 100644 --- a/test/ch/epfl/xblast/simulation/ConsoleSimulation.java +++ b/test/ch/epfl/xblast/simulation/ConsoleSimulation.java @@ -1,80 +1,23 @@ package ch.epfl.xblast.simulation; -import ch.epfl.xblast.Cell; -import ch.epfl.xblast.PlayerID; -import ch.epfl.xblast.server.Board; -import ch.epfl.xblast.server.BoardTest; import ch.epfl.xblast.server.GameState; -import ch.epfl.xblast.server.Player; import ch.epfl.xblast.server.debug.GameStatePrinter; -import ch.epfl.xblast.server.debug.RandomEventGenerator; - -import java.util.Arrays; -import java.util.List; /** - * Random game simulation. + * Random game simulation printed in the console. * * @author Pacien TRAN-GIRARD (261948) */ -public class ConsoleSimulation { - - private static final long DISPLAY_DELAY = 50; // in milliseconds - - private static final int PLAYER_LIVES = 4; - private static final int PLAYER_MAX_BOMBS = 4; - private static final int PLAYER_BOMB_RANGE = 4; - - 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 Board buildTestBoard() { - return Board.ofQuadrantNWBlocksWalled(BoardTest.buildNWQuadrantMap()); - } - - private static Player newPlayer(PlayerID id, Cell pos) { - return new Player(id, PLAYER_LIVES, pos, PLAYER_MAX_BOMBS, PLAYER_BOMB_RANGE); - } - - private static List buildPlayersList() { - return Arrays.asList( - newPlayer(PlayerID.PLAYER_1, new Cell(1, 1)), - newPlayer(PlayerID.PLAYER_2, new Cell(13, 1)), - newPlayer(PlayerID.PLAYER_3, new Cell(13, 11)), - newPlayer(PlayerID.PLAYER_4, new Cell(1, 11))); - } - - private static GameState buildInitialGameState() { - return new GameState(buildTestBoard(), buildPlayersList()); - } - - private static GameState nextGameState(GameState gs) { - return gs.next(RANDOM_EVENT_GENERATOR.randomSpeedChangeEvents(), RANDOM_EVENT_GENERATOR.randomBombDropEvents()); - } - - private static boolean isSimulationOver(GameState gs) { - return gs == null || gs.isGameOver(); - } - - private static void delay() { - try { - Thread.sleep(DISPLAY_DELAY); - } catch (InterruptedException e) { - e.printStackTrace(); - System.exit(1); - } - } +public class ConsoleSimulation extends Simulation { - private static void displayGameState(GameState gs) { + @Override + void displayGameState(GameState gs) { GameStatePrinter.printGameState(gs); delay(); } public static void main(String[] args) { - for (GameState gs = buildInitialGameState(); !isSimulationOver(gs); gs = nextGameState(gs)) - displayGameState(gs); + (new ConsoleSimulation()).runSimulation(); } } diff --git a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java index 7e4454d..8009d9f 100644 --- a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java +++ b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java @@ -2,33 +2,37 @@ package ch.epfl.xblast.simulation; import ch.epfl.xblast.PlayerAction; import ch.epfl.xblast.PlayerID; -import ch.epfl.xblast.client.*; -import ch.epfl.xblast.server.*; +import ch.epfl.xblast.client.GameStateDeserializer; +import ch.epfl.xblast.client.KeyboardEventHandler; +import ch.epfl.xblast.client.XBlastComponent; import ch.epfl.xblast.server.GameState; -import ch.epfl.xblast.server.debug.RandomEventGenerator; +import ch.epfl.xblast.server.GameStateSerializer; +import ch.epfl.xblast.server.Level; import ch.epfl.xblast.server.painter.BoardPainter; import javax.swing.*; import java.awt.event.KeyEvent; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; /** + * Graphical game simulation. + * * @author Timothée FLOURE (257420) + * @author Pacien TRAN-GIRARD (261948) */ -public class GraphicalSimulation { - private static final GameState INITIAL_GAMESTATE = Level.DEFAULT_LEVEL.initialState(); - private static final BoardPainter BOARD_PAINTER = Level.DEFAULT_LEVEL.painter(); +public class GraphicalSimulation extends Simulation { - 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 final BoardPainter BOARD_PAINTER = Level.DEFAULT_LEVEL.painter(); + private static final Consumer PLAYER_ACTION_CONSUMER = System.out::println; + private static final PlayerID MAIN_PLAYER = PlayerID.PLAYER_1; - private static Map buildPlayerActionMap() { + private static Map buildPlayerActionMap() { Map playerActionMap = new HashMap<>(); + playerActionMap.put(KeyEvent.VK_UP, PlayerAction.MOVE_N); playerActionMap.put(KeyEvent.VK_RIGHT, PlayerAction.MOVE_E); playerActionMap.put(KeyEvent.VK_DOWN, PlayerAction.MOVE_S); @@ -36,49 +40,45 @@ public class GraphicalSimulation { playerActionMap.put(KeyEvent.VK_SPACE, PlayerAction.DROP_BOMB); playerActionMap.put(KeyEvent.VK_SHIFT, PlayerAction.STOP); - return playerActionMap; + return Collections.unmodifiableMap(playerActionMap); } - private static ch.epfl.xblast.client.GameState getClientData(GameState gs) { + private static ch.epfl.xblast.client.GameState translateToClientData(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() { + private static JFrame buildFrame(XBlastComponent gui) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); + + frame.setContentPane(gui); + frame.pack(); return frame; } - public static void main(String[] args) { + private void attachKeyboardHandler(JFrame frame) { + frame.addKeyListener(new KeyboardEventHandler(buildPlayerActionMap(), PLAYER_ACTION_CONSUMER)); + frame.requestFocusInWindow(); + } - // Build the window - JFrame frame = buildFrame(); - XBlastComponent xblast = new XBlastComponent(); - frame.setContentPane(xblast); - frame.pack(); + private final XBlastComponent gui; - // Attach a KeyboardEventHandler - Consumer c = System.out::println; - frame.addKeyListener(new KeyboardEventHandler(buildPlayerActionMap(), c)); - frame.requestFocusInWindow(); + private GraphicalSimulation() { + this.gui = new XBlastComponent(); + attachKeyboardHandler(buildFrame(this.gui)); + } - // Run the Simulation - for (GameState gs = INITIAL_GAMESTATE; !isSimulationOver(gs); gs = nextGameState(gs)) - displayGameState(getClientData(gs), xblast); + @Override + void displayGameState(GameState gs) { + this.gui.setGameState(translateToClientData(gs), MAIN_PLAYER); + delay(); } + + public static void main(String[] args) { + (new GraphicalSimulation()).runSimulation(); + } + } diff --git a/test/ch/epfl/xblast/simulation/Simulation.java b/test/ch/epfl/xblast/simulation/Simulation.java new file mode 100644 index 0000000..e70c688 --- /dev/null +++ b/test/ch/epfl/xblast/simulation/Simulation.java @@ -0,0 +1,54 @@ +package ch.epfl.xblast.simulation; + +import ch.epfl.xblast.server.GameState; +import ch.epfl.xblast.server.Level; +import ch.epfl.xblast.server.debug.RandomEventGenerator; + +/** + * An abstract game simulation. + * + * @author Pacien TRAN-GIRARD (261948) + */ +abstract class Simulation { + + private static final GameState INITIAL_GAME_STATE = Level.DEFAULT_LEVEL.initialState(); + + private static final int RNG_SEED = 2016; + private static final int SPEED_CHANGE_PROB = 30; + private static final int BOMB_PROB = 100; + + private static final long DISPLAY_DELAY = 50; // in milliseconds + + static void delay() { + try { + Thread.sleep(DISPLAY_DELAY); + } catch (InterruptedException e) { + e.printStackTrace(); + System.exit(1); + } + } + + private final RandomEventGenerator randomEventGenerator; + + Simulation() { + this.randomEventGenerator = new RandomEventGenerator(RNG_SEED, SPEED_CHANGE_PROB, BOMB_PROB); + } + + private boolean isSimulationOver(GameState gs) { + return gs == null || gs.isGameOver(); + } + + private GameState nextGameState(GameState gs) { + return gs.next( + this.randomEventGenerator.randomSpeedChangeEvents(), + this.randomEventGenerator.randomBombDropEvents()); + } + + void runSimulation() { + for (GameState gs = INITIAL_GAME_STATE; !isSimulationOver(gs); gs = nextGameState(gs)) + displayGameState(gs); + } + + abstract void displayGameState(GameState gs); + +} -- cgit v1.2.3