aboutsummaryrefslogtreecommitdiff
path: root/test/ch
diff options
context:
space:
mode:
authorTimothée Floure2016-05-09 15:15:10 +0200
committerTimothée Floure2016-05-09 15:15:10 +0200
commitfc7783bebc429a2ba368f96be8ced16c9d7509d4 (patch)
tree15c04810ffb674f308019d32b6a06c52bafc0da1 /test/ch
parent3ba97db409c1a0713b514a394407da325db0feb8 (diff)
downloadxblast-fc7783bebc429a2ba368f96be8ced16c9d7509d4.tar.gz
Basic Graphical Simulation
Diffstat (limited to 'test/ch')
-rw-r--r--test/ch/epfl/xblast/simulation/GraphicalSimulation.java57
1 files changed, 57 insertions, 0 deletions
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 @@
1package ch.epfl.xblast.simulation;
2
3import ch.epfl.xblast.PlayerID;
4import ch.epfl.xblast.client.*;
5import ch.epfl.xblast.server.*;
6import ch.epfl.xblast.server.GameState;
7import ch.epfl.xblast.server.debug.RandomEventGenerator;
8import ch.epfl.xblast.server.painter.BoardPainter;
9
10import javax.swing.*;
11import java.util.List;
12
13/**
14 * @author Timothée FLOURE (257420)
15 */
16public class GraphicalSimulation {
17 private static final GameState INITIAL_GAMESTATE = Level.DEFAULT_LEVEL.initialState();
18 private static final BoardPainter BOARD_PAINTER = Level.DEFAULT_LEVEL.painter();
19
20 private static final int SEED = 2016;
21 private static final int SPEED_CHANGE_PROB = 30;
22 private static final int BOMB_PROB = 100;
23 private static final RandomEventGenerator RANDOM_EVENT_GENERATOR = new RandomEventGenerator(SEED, SPEED_CHANGE_PROB, BOMB_PROB);
24
25 private static ch.epfl.xblast.client.GameState getClientData(GameState gs) {
26 List<Byte> serializedGameState = GameStateSerializer.serialize(BOARD_PAINTER, gs);
27 return GameStateDeserializer.deserialize(serializedGameState);
28 }
29
30 private static boolean isSimulationOver(GameState gs) {
31 return gs == null || gs.isGameOver();
32 }
33
34 private static GameState nextGameState(GameState gs) {
35 return gs.next(RANDOM_EVENT_GENERATOR.randomSpeedChangeEvents(), RANDOM_EVENT_GENERATOR.randomBombDropEvents());
36 }
37
38 private static void displayGameState(ch.epfl.xblast.client.GameState gs, XBlastComponent gui) {
39 gui.setGameState(gs, PlayerID.PLAYER_1);
40 }
41
42 private static JFrame buildFrame() {
43 JFrame frame = new JFrame();
44 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
45 frame.setVisible(true);
46 return frame;
47 }
48
49 public static void main(String[] args) {
50 JFrame frame = buildFrame();
51 XBlastComponent xblast = new XBlastComponent();
52 frame.setContentPane(xblast);
53
54 for (GameState gs = INITIAL_GAMESTATE; !isSimulationOver(gs); gs = nextGameState(gs))
55 displayGameState(getClientData(gs), xblast);
56 }
57}