From e50e68d0523ece7cdf772b12904a6c0dafa2ec4a Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Mon, 9 May 2016 13:46:36 +0200 Subject: PlayerAction Enum and rename RandomSimulation to ConsoleSimulation --- .../epfl/xblast/simulation/ConsoleSimulation.java | 80 ++++++++++++++++++++++ .../epfl/xblast/simulation/RandomSimulation.java | 80 ---------------------- 2 files changed, 80 insertions(+), 80 deletions(-) create mode 100644 test/ch/epfl/xblast/simulation/ConsoleSimulation.java delete mode 100644 test/ch/epfl/xblast/simulation/RandomSimulation.java (limited to 'test/ch') diff --git a/test/ch/epfl/xblast/simulation/ConsoleSimulation.java b/test/ch/epfl/xblast/simulation/ConsoleSimulation.java new file mode 100644 index 0000000..35c0eb8 --- /dev/null +++ b/test/ch/epfl/xblast/simulation/ConsoleSimulation.java @@ -0,0 +1,80 @@ +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. + * + * @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); + } + } + + private static 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); + } + +} diff --git a/test/ch/epfl/xblast/simulation/RandomSimulation.java b/test/ch/epfl/xblast/simulation/RandomSimulation.java deleted file mode 100644 index 3888650..0000000 --- a/test/ch/epfl/xblast/simulation/RandomSimulation.java +++ /dev/null @@ -1,80 +0,0 @@ -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. - * - * @author Pacien TRAN-GIRARD (261948) - */ -public class RandomSimulation { - - 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); - } - } - - private static 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); - } - -} -- cgit v1.2.3 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') 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 From c27e85228fe3c7de8465959940c1cde64bdb7887 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Mon, 9 May 2016 16:18:02 +0200 Subject: Fix the window size in GraphicalSimulation --- test/ch/epfl/xblast/simulation/GraphicalSimulation.java | 2 ++ 1 file changed, 2 insertions(+) (limited to 'test/ch') diff --git a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java index 1a0d88f..5706577 100644 --- a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java +++ b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java @@ -42,6 +42,7 @@ public class GraphicalSimulation { private static JFrame buildFrame() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); return frame; } @@ -50,6 +51,7 @@ public class GraphicalSimulation { JFrame frame = buildFrame(); XBlastComponent xblast = new XBlastComponent(); frame.setContentPane(xblast); + frame.pack(); for (GameState gs = INITIAL_GAMESTATE; !isSimulationOver(gs); gs = nextGameState(gs)) displayGameState(getClientData(gs), xblast); -- cgit v1.2.3 From 38711042dd2c39cf51cd2d063f2eb38d0bbb5a45 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Mon, 9 May 2016 16:37:36 +0200 Subject: Implement KeyboardEventHandler to the Graphical Simulation --- .../xblast/simulation/GraphicalSimulation.java | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'test/ch') diff --git a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java index 5706577..7e4454d 100644 --- a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java +++ b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java @@ -1,5 +1,6 @@ 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.*; @@ -8,7 +9,11 @@ import ch.epfl.xblast.server.debug.RandomEventGenerator; import ch.epfl.xblast.server.painter.BoardPainter; import javax.swing.*; +import java.awt.event.KeyEvent; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.function.Consumer; /** * @author Timothée FLOURE (257420) @@ -22,6 +27,18 @@ public class GraphicalSimulation { private static final int BOMB_PROB = 100; private static final RandomEventGenerator RANDOM_EVENT_GENERATOR = new RandomEventGenerator(SEED, SPEED_CHANGE_PROB, BOMB_PROB); + 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); + playerActionMap.put(KeyEvent.VK_LEFT, PlayerAction.MOVE_W); + playerActionMap.put(KeyEvent.VK_SPACE, PlayerAction.DROP_BOMB); + playerActionMap.put(KeyEvent.VK_SHIFT, PlayerAction.STOP); + + return playerActionMap; + } + private static ch.epfl.xblast.client.GameState getClientData(GameState gs) { List serializedGameState = GameStateSerializer.serialize(BOARD_PAINTER, gs); return GameStateDeserializer.deserialize(serializedGameState); @@ -48,11 +65,19 @@ public class GraphicalSimulation { } public static void main(String[] args) { + + // Build the window JFrame frame = buildFrame(); XBlastComponent xblast = new XBlastComponent(); frame.setContentPane(xblast); frame.pack(); + // Attach a KeyboardEventHandler + Consumer c = System.out::println; + frame.addKeyListener(new KeyboardEventHandler(buildPlayerActionMap(), c)); + frame.requestFocusInWindow(); + + // Run the Simulation for (GameState gs = INITIAL_GAMESTATE; !isSimulationOver(gs); gs = nextGameState(gs)) displayGameState(getClientData(gs), xblast); } -- cgit v1.2.3 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') 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 From e85032012f1d5ded1b6a49a88d345cc60a6ac88b Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 9 May 2016 22:52:56 +0200 Subject: Properly handle keyboard events --- test/ch/epfl/xblast/simulation/GraphicalSimulation.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'test/ch') diff --git a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java index 8009d9f..9329cc7 100644 --- a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java +++ b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java @@ -11,6 +11,7 @@ import ch.epfl.xblast.server.Level; import ch.epfl.xblast.server.painter.BoardPainter; import javax.swing.*; +import java.awt.*; import java.awt.event.KeyEvent; import java.util.Collections; import java.util.HashMap; @@ -48,27 +49,28 @@ public class GraphicalSimulation extends Simulation { return GameStateDeserializer.deserialize(serializedGameState); } - private static JFrame buildFrame(XBlastComponent gui) { + private static JFrame buildFrame(Container content) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); - frame.setContentPane(gui); + frame.setContentPane(content); frame.pack(); return frame; } - private void attachKeyboardHandler(JFrame frame) { - frame.addKeyListener(new KeyboardEventHandler(buildPlayerActionMap(), PLAYER_ACTION_CONSUMER)); - frame.requestFocusInWindow(); + private void attachKeyboardHandler(Component comp) { + comp.addKeyListener(new KeyboardEventHandler(buildPlayerActionMap(), PLAYER_ACTION_CONSUMER)); + comp.requestFocusInWindow(); } private final XBlastComponent gui; private GraphicalSimulation() { this.gui = new XBlastComponent(); - attachKeyboardHandler(buildFrame(this.gui)); + buildFrame(this.gui); + attachKeyboardHandler(this.gui); } @Override -- cgit v1.2.3 From 766114c05bcd0b0388988aaf606e046857e6946a Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 10 May 2016 17:10:31 +0200 Subject: Add lists and maps functions --- test/ch/epfl/xblast/ListsTest.java | 47 ++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) (limited to 'test/ch') diff --git a/test/ch/epfl/xblast/ListsTest.java b/test/ch/epfl/xblast/ListsTest.java index b18b05e..e62eede 100644 --- a/test/ch/epfl/xblast/ListsTest.java +++ b/test/ch/epfl/xblast/ListsTest.java @@ -2,10 +2,7 @@ package ch.epfl.xblast; import org.junit.Test; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -63,4 +60,46 @@ public class ListsTest { assertEquals(expected, Lists.permutations(sampleList)); } + @Test + public void areListsMapped() { + final List keyList = Arrays.asList('a', 'b'); + final List valueList = Arrays.asList(0, 1); + + final Map expectedMap = new HashMap<>(); + expectedMap.put('a', 0); + expectedMap.put('b', 1); + + assertEquals(expectedMap, Lists.linearMap(keyList, valueList)); + } + + @Test + public void areMapsTraversed() { + final Map m1 = new HashMap<>(); + m1.put('a', 0); + m1.put('b', 1); + + final Map m2 = new HashMap<>(); + m2.put(0, "Apple"); + m2.put(1, "Banana"); + + final Map expectedMap = new HashMap<>(); + expectedMap.put('a', "Apple"); + expectedMap.put('b', "Banana"); + + assertEquals(expectedMap, Lists.traverseMaps(m1, m2)); + } + + @Test + public void isMapInverted() { + final Map sampleMap = new HashMap<>(); + sampleMap.put('a', 0); + sampleMap.put('b', 1); + + final Map expectedMap = new HashMap<>(); + expectedMap.put(0, 'a'); + expectedMap.put(1, 'b'); + + assertEquals(expectedMap, Lists.invertMap(sampleMap)); + } + } -- cgit v1.2.3