aboutsummaryrefslogtreecommitdiff
path: root/test/ch/epfl/xblast/simulation/GraphicalSimulation.java
blob: 1a0d88f1cc086024ba40818013c7cf1f66478e64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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<Byte> 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);
    }
}