diff options
-rw-r--r-- | test/ch/epfl/xblast/simulation/ConsoleSimulation.java | 67 | ||||
-rw-r--r-- | test/ch/epfl/xblast/simulation/GraphicalSimulation.java | 78 | ||||
-rw-r--r-- | test/ch/epfl/xblast/simulation/Simulation.java | 54 |
3 files changed, 98 insertions, 101 deletions
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 @@ | |||
1 | package ch.epfl.xblast.simulation; | 1 | package ch.epfl.xblast.simulation; |
2 | 2 | ||
3 | import ch.epfl.xblast.Cell; | ||
4 | import ch.epfl.xblast.PlayerID; | ||
5 | import ch.epfl.xblast.server.Board; | ||
6 | import ch.epfl.xblast.server.BoardTest; | ||
7 | import ch.epfl.xblast.server.GameState; | 3 | import ch.epfl.xblast.server.GameState; |
8 | import ch.epfl.xblast.server.Player; | ||
9 | import ch.epfl.xblast.server.debug.GameStatePrinter; | 4 | import ch.epfl.xblast.server.debug.GameStatePrinter; |
10 | import ch.epfl.xblast.server.debug.RandomEventGenerator; | ||
11 | |||
12 | import java.util.Arrays; | ||
13 | import java.util.List; | ||
14 | 5 | ||
15 | /** | 6 | /** |
16 | * Random game simulation. | 7 | * Random game simulation printed in the console. |
17 | * | 8 | * |
18 | * @author Pacien TRAN-GIRARD (261948) | 9 | * @author Pacien TRAN-GIRARD (261948) |
19 | */ | 10 | */ |
20 | public class ConsoleSimulation { | 11 | public class ConsoleSimulation extends Simulation { |
21 | |||
22 | private static final long DISPLAY_DELAY = 50; // in milliseconds | ||
23 | |||
24 | private static final int PLAYER_LIVES = 4; | ||
25 | private static final int PLAYER_MAX_BOMBS = 4; | ||
26 | private static final int PLAYER_BOMB_RANGE = 4; | ||
27 | |||
28 | private static final int SEED = 2016; | ||
29 | private static final int SPEED_CHANGE_PROB = 30; | ||
30 | private static final int BOMB_PROB = 100; | ||
31 | private static final RandomEventGenerator RANDOM_EVENT_GENERATOR = new RandomEventGenerator(SEED, SPEED_CHANGE_PROB, BOMB_PROB); | ||
32 | |||
33 | private static Board buildTestBoard() { | ||
34 | return Board.ofQuadrantNWBlocksWalled(BoardTest.buildNWQuadrantMap()); | ||
35 | } | ||
36 | |||
37 | private static Player newPlayer(PlayerID id, Cell pos) { | ||
38 | return new Player(id, PLAYER_LIVES, pos, PLAYER_MAX_BOMBS, PLAYER_BOMB_RANGE); | ||
39 | } | ||
40 | |||
41 | private static List<Player> buildPlayersList() { | ||
42 | return Arrays.asList( | ||
43 | newPlayer(PlayerID.PLAYER_1, new Cell(1, 1)), | ||
44 | newPlayer(PlayerID.PLAYER_2, new Cell(13, 1)), | ||
45 | newPlayer(PlayerID.PLAYER_3, new Cell(13, 11)), | ||
46 | newPlayer(PlayerID.PLAYER_4, new Cell(1, 11))); | ||
47 | } | ||
48 | |||
49 | private static GameState buildInitialGameState() { | ||
50 | return new GameState(buildTestBoard(), buildPlayersList()); | ||
51 | } | ||
52 | |||
53 | private static GameState nextGameState(GameState gs) { | ||
54 | return gs.next(RANDOM_EVENT_GENERATOR.randomSpeedChangeEvents(), RANDOM_EVENT_GENERATOR.randomBombDropEvents()); | ||
55 | } | ||
56 | |||
57 | private static boolean isSimulationOver(GameState gs) { | ||
58 | return gs == null || gs.isGameOver(); | ||
59 | } | ||
60 | |||
61 | private static void delay() { | ||
62 | try { | ||
63 | Thread.sleep(DISPLAY_DELAY); | ||
64 | } catch (InterruptedException e) { | ||
65 | e.printStackTrace(); | ||
66 | System.exit(1); | ||
67 | } | ||
68 | } | ||
69 | 12 | ||
70 | private static void displayGameState(GameState gs) { | 13 | @Override |
14 | void displayGameState(GameState gs) { | ||
71 | GameStatePrinter.printGameState(gs); | 15 | GameStatePrinter.printGameState(gs); |
72 | delay(); | 16 | delay(); |
73 | } | 17 | } |
74 | 18 | ||
75 | public static void main(String[] args) { | 19 | public static void main(String[] args) { |
76 | for (GameState gs = buildInitialGameState(); !isSimulationOver(gs); gs = nextGameState(gs)) | 20 | (new ConsoleSimulation()).runSimulation(); |
77 | displayGameState(gs); | ||
78 | } | 21 | } |
79 | 22 | ||
80 | } | 23 | } |
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; | |||
2 | 2 | ||
3 | import ch.epfl.xblast.PlayerAction; | 3 | import ch.epfl.xblast.PlayerAction; |
4 | import ch.epfl.xblast.PlayerID; | 4 | import ch.epfl.xblast.PlayerID; |
5 | import ch.epfl.xblast.client.*; | 5 | import ch.epfl.xblast.client.GameStateDeserializer; |
6 | import ch.epfl.xblast.server.*; | 6 | import ch.epfl.xblast.client.KeyboardEventHandler; |
7 | import ch.epfl.xblast.client.XBlastComponent; | ||
7 | import ch.epfl.xblast.server.GameState; | 8 | import ch.epfl.xblast.server.GameState; |
8 | import ch.epfl.xblast.server.debug.RandomEventGenerator; | 9 | import ch.epfl.xblast.server.GameStateSerializer; |
10 | import ch.epfl.xblast.server.Level; | ||
9 | import ch.epfl.xblast.server.painter.BoardPainter; | 11 | import ch.epfl.xblast.server.painter.BoardPainter; |
10 | 12 | ||
11 | import javax.swing.*; | 13 | import javax.swing.*; |
12 | import java.awt.event.KeyEvent; | 14 | import java.awt.event.KeyEvent; |
15 | import java.util.Collections; | ||
13 | import java.util.HashMap; | 16 | import java.util.HashMap; |
14 | import java.util.List; | 17 | import java.util.List; |
15 | import java.util.Map; | 18 | import java.util.Map; |
16 | import java.util.function.Consumer; | 19 | import java.util.function.Consumer; |
17 | 20 | ||
18 | /** | 21 | /** |
22 | * Graphical game simulation. | ||
23 | * | ||
19 | * @author Timothée FLOURE (257420) | 24 | * @author Timothée FLOURE (257420) |
25 | * @author Pacien TRAN-GIRARD (261948) | ||
20 | */ | 26 | */ |
21 | public class GraphicalSimulation { | 27 | public class GraphicalSimulation extends Simulation { |
22 | private static final GameState INITIAL_GAMESTATE = Level.DEFAULT_LEVEL.initialState(); | ||
23 | private static final BoardPainter BOARD_PAINTER = Level.DEFAULT_LEVEL.painter(); | ||
24 | 28 | ||
25 | private static final int SEED = 2016; | 29 | private static final BoardPainter BOARD_PAINTER = Level.DEFAULT_LEVEL.painter(); |
26 | private static final int SPEED_CHANGE_PROB = 30; | 30 | private static final Consumer<PlayerAction> PLAYER_ACTION_CONSUMER = System.out::println; |
27 | private static final int BOMB_PROB = 100; | 31 | private static final PlayerID MAIN_PLAYER = PlayerID.PLAYER_1; |
28 | private static final RandomEventGenerator RANDOM_EVENT_GENERATOR = new RandomEventGenerator(SEED, SPEED_CHANGE_PROB, BOMB_PROB); | ||
29 | 32 | ||
30 | private static Map<Integer,PlayerAction> buildPlayerActionMap() { | 33 | private static Map<Integer, PlayerAction> buildPlayerActionMap() { |
31 | Map<Integer, PlayerAction> playerActionMap = new HashMap<>(); | 34 | Map<Integer, PlayerAction> playerActionMap = new HashMap<>(); |
35 | |||
32 | playerActionMap.put(KeyEvent.VK_UP, PlayerAction.MOVE_N); | 36 | playerActionMap.put(KeyEvent.VK_UP, PlayerAction.MOVE_N); |
33 | playerActionMap.put(KeyEvent.VK_RIGHT, PlayerAction.MOVE_E); | 37 | playerActionMap.put(KeyEvent.VK_RIGHT, PlayerAction.MOVE_E); |
34 | playerActionMap.put(KeyEvent.VK_DOWN, PlayerAction.MOVE_S); | 38 | playerActionMap.put(KeyEvent.VK_DOWN, PlayerAction.MOVE_S); |
@@ -36,49 +40,45 @@ public class GraphicalSimulation { | |||
36 | playerActionMap.put(KeyEvent.VK_SPACE, PlayerAction.DROP_BOMB); | 40 | playerActionMap.put(KeyEvent.VK_SPACE, PlayerAction.DROP_BOMB); |
37 | playerActionMap.put(KeyEvent.VK_SHIFT, PlayerAction.STOP); | 41 | playerActionMap.put(KeyEvent.VK_SHIFT, PlayerAction.STOP); |
38 | 42 | ||
39 | return playerActionMap; | 43 | return Collections.unmodifiableMap(playerActionMap); |
40 | } | 44 | } |
41 | 45 | ||
42 | private static ch.epfl.xblast.client.GameState getClientData(GameState gs) { | 46 | private static ch.epfl.xblast.client.GameState translateToClientData(GameState gs) { |
43 | List<Byte> serializedGameState = GameStateSerializer.serialize(BOARD_PAINTER, gs); | 47 | List<Byte> serializedGameState = GameStateSerializer.serialize(BOARD_PAINTER, gs); |
44 | return GameStateDeserializer.deserialize(serializedGameState); | 48 | return GameStateDeserializer.deserialize(serializedGameState); |
45 | } | 49 | } |
46 | 50 | ||
47 | private static boolean isSimulationOver(GameState gs) { | 51 | private static JFrame buildFrame(XBlastComponent gui) { |
48 | return gs == null || gs.isGameOver(); | ||
49 | } | ||
50 | |||
51 | private static GameState nextGameState(GameState gs) { | ||
52 | return gs.next(RANDOM_EVENT_GENERATOR.randomSpeedChangeEvents(), RANDOM_EVENT_GENERATOR.randomBombDropEvents()); | ||
53 | } | ||
54 | |||
55 | private static void displayGameState(ch.epfl.xblast.client.GameState gs, XBlastComponent gui) { | ||
56 | gui.setGameState(gs, PlayerID.PLAYER_1); | ||
57 | } | ||
58 | |||
59 | private static JFrame buildFrame() { | ||
60 | JFrame frame = new JFrame(); | 52 | JFrame frame = new JFrame(); |
61 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | 53 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
62 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | 54 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
63 | frame.setVisible(true); | 55 | frame.setVisible(true); |
56 | |||
57 | frame.setContentPane(gui); | ||
58 | frame.pack(); | ||
64 | return frame; | 59 | return frame; |
65 | } | 60 | } |
66 | 61 | ||
67 | public static void main(String[] args) { | 62 | private void attachKeyboardHandler(JFrame frame) { |
63 | frame.addKeyListener(new KeyboardEventHandler(buildPlayerActionMap(), PLAYER_ACTION_CONSUMER)); | ||
64 | frame.requestFocusInWindow(); | ||
65 | } | ||
68 | 66 | ||
69 | // Build the window | 67 | private final XBlastComponent gui; |
70 | JFrame frame = buildFrame(); | ||
71 | XBlastComponent xblast = new XBlastComponent(); | ||
72 | frame.setContentPane(xblast); | ||
73 | frame.pack(); | ||
74 | 68 | ||
75 | // Attach |