diff options
Diffstat (limited to 'test/ch/epfl')
-rw-r--r-- | test/ch/epfl/xblast/ListsTest.java | 47 | ||||
-rw-r--r-- | test/ch/epfl/xblast/simulation/ConsoleSimulation.java | 23 | ||||
-rw-r--r-- | test/ch/epfl/xblast/simulation/GraphicalSimulation.java | 86 | ||||
-rw-r--r-- | test/ch/epfl/xblast/simulation/RandomSimulation.java | 80 | ||||
-rw-r--r-- | test/ch/epfl/xblast/simulation/Simulation.java | 54 |
5 files changed, 206 insertions, 84 deletions
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; | |||
2 | 2 | ||
3 | import org.junit.Test; | 3 | import org.junit.Test; |
4 | 4 | ||
5 | import java.util.ArrayList; | 5 | import java.util.*; |
6 | import java.util.Arrays; | ||
7 | import java.util.LinkedList; | ||
8 | import java.util.List; | ||
9 | import java.util.stream.Collectors; | 6 | import java.util.stream.Collectors; |
10 | import java.util.stream.IntStream; | 7 | import java.util.stream.IntStream; |
11 | 8 | ||
@@ -63,4 +60,46 @@ public class ListsTest { | |||
63 | assertEquals(expected, Lists.permutations(sampleList)); | 60 | assertEquals(expected, Lists.permutations(sampleList)); |
64 | } | 61 | } |
65 | 62 | ||
63 | @Test | ||
64 | public void areListsMapped() { | ||
65 | final List<Character> keyList = Arrays.asList('a', 'b'); | ||
66 | final List<Integer> valueList = Arrays.asList(0, 1); | ||
67 | |||
68 | final Map<Character, Integer> expectedMap = new HashMap<>(); | ||
69 | expectedMap.put('a', 0); | ||
70 | expectedMap.put('b', 1); | ||
71 | |||
72 | assertEquals(expectedMap, Lists.linearMap(keyList, valueList)); | ||
73 | } | ||
74 | |||
75 | @Test | ||
76 | public void areMapsTraversed() { | ||
77 | final Map<Character, Integer> m1 = new HashMap<>(); | ||
78 | m1.put('a', 0); | ||
79 | m1.put('b', 1); | ||
80 | |||
81 | final Map<Integer, String> m2 = new HashMap<>(); | ||
82 | m2.put(0, "Apple"); | ||
83 | m2.put(1, "Banana"); | ||
84 | |||
85 | final Map<Character, String> expectedMap = new HashMap<>(); | ||
86 | expectedMap.put('a', "Apple"); | ||
87 | expectedMap.put('b', "Banana"); | ||
88 | |||
89 | assertEquals(expectedMap, Lists.traverseMaps(m1, m2)); | ||
90 | } | ||
91 | |||
92 | @Test | ||
93 | public void isMapInverted() { | ||
94 | final Map<Character, Integer> sampleMap = new HashMap<>(); | ||
95 | sampleMap.put('a', 0); | ||
96 | sampleMap.put('b', 1); | ||
97 | |||
98 | final Map<Integer, Character> expectedMap = new HashMap<>(); | ||
99 | expectedMap.put(0, 'a'); | ||
100 | expectedMap.put(1, 'b'); | ||
101 | |||
102 | assertEquals(expectedMap, Lists.invertMap(sampleMap)); | ||
103 | } | ||
104 | |||
66 | } | 105 | } |
diff --git a/test/ch/epfl/xblast/simulation/ConsoleSimulation.java b/test/ch/epfl/xblast/simulation/ConsoleSimulation.java new file mode 100644 index 0000000..b029afb --- /dev/null +++ b/test/ch/epfl/xblast/simulation/ConsoleSimulation.java | |||
@@ -0,0 +1,23 @@ | |||
1 | package ch.epfl.xblast.simulation; | ||
2 | |||
3 | import ch.epfl.xblast.server.GameState; | ||
4 | import ch.epfl.xblast.server.debug.GameStatePrinter; | ||
5 | |||
6 | /** | ||
7 | * Random game simulation printed in the console. | ||
8 | * | ||
9 | * @author Pacien TRAN-GIRARD (261948) | ||
10 | */ | ||
11 | public class ConsoleSimulation extends Simulation { | ||
12 | |||
13 | @Override | ||
14 | void displayGameState(GameState gs) { | ||
15 | GameStatePrinter.printGameState(gs); | ||
16 | delay(); | ||
17 | } | ||
18 | |||
19 | public static void main(String[] args) { | ||
20 | (new ConsoleSimulation()).runSimulation(); | ||
21 | } | ||
22 | |||
23 | } | ||
diff --git a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java new file mode 100644 index 0000000..9329cc7 --- /dev/null +++ b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java | |||
@@ -0,0 +1,86 @@ | |||
1 | package ch.epfl.xblast.simulation; | ||
2 | |||
3 | import ch.epfl.xblast.PlayerAction; | ||
4 | import ch.epfl.xblast.PlayerID; | ||
5 | import ch.epfl.xblast.client.GameStateDeserializer; | ||
6 | import ch.epfl.xblast.client.KeyboardEventHandler; | ||
7 | import ch.epfl.xblast.client.XBlastComponent; | ||
8 | import ch.epfl.xblast.server.GameState; | ||
9 | import ch.epfl.xblast.server.GameStateSerializer; | ||
10 | import ch.epfl.xblast.server.Level; | ||
11 | import ch.epfl.xblast.server.painter.BoardPainter; | ||
12 | |||
13 | import javax.swing.*; | ||
14 | import java.awt.*; | ||
15 | import java.awt.event.KeyEvent; | ||
16 | import java.util.Collections; | ||
17 | import java.util.HashMap; | ||
18 | import java.util.List; | ||
19 | import java.util.Map; | ||
20 | import java.util.function.Consumer; | ||
21 | |||
22 | /** | ||
23 | * Graphical game simulation. | ||
24 | * | ||
25 | * @author Timothée FLOURE (257420) | ||
26 | * @author Pacien TRAN-GIRARD (261948) | ||
27 | */ | ||
28 | public class GraphicalSimulation extends Simulation { | ||
29 | |||
30 | private static final BoardPainter BOARD_PAINTER = Level.DEFAULT_LEVEL.painter(); | ||
31 | private static final Consumer<PlayerAction> PLAYER_ACTION_CONSUMER = System.out::println; | ||
32 | private static final PlayerID MAIN_PLAYER = PlayerID.PLAYER_1; | ||
33 | |||
34 | private static Map<Integer, PlayerAction> buildPlayerActionMap() { | ||
35 | Map<Integer, PlayerAction> playerActionMap = new HashMap<>(); | ||
36 | |||
37 | playerActionMap.put(KeyEvent.VK_UP, PlayerAction.MOVE_N); | ||
38 | playerActionMap.put(KeyEvent.VK_RIGHT, PlayerAction.MOVE_E); | ||
39 | playerActionMap.put(KeyEvent.VK_DOWN, PlayerAction.MOVE_S); | ||
40 | playerActionMap.put(KeyEvent.VK_LEFT, PlayerAction.MOVE_W); | ||
41 | playerActionMap.put(KeyEvent.VK_SPACE, PlayerAction.DROP_BOMB); | ||
42 | playerActionMap.put(KeyEvent.VK_SHIFT, PlayerAction.STOP); | ||
43 | |||
44 | return Collections.unmodifiableMap(playerActionMap); | ||
45 | } | ||
46 | |||
47 | private static ch.epfl.xblast.client.GameState translateToClientData(GameState gs) { | ||
48 | List<Byte> serializedGameState = GameStateSerializer.serialize(BOARD_PAINTER, gs); | ||
49 | return GameStateDeserializer.deserialize(serializedGameState); | ||
50 | } | ||
51 | |||
52 | private static JFrame buildFrame(Container content) { | ||
53 | JFrame frame = new JFrame(); | ||
54 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
55 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
56 | frame.setVisible(true); | ||
57 | |||
58 | frame.setContentPane(content); | ||
59 | frame.pack(); | ||
60 | return frame; | ||
61 | } | ||
62 | |||
63 | private void attachKeyboardHandler(Component comp) { | ||
64 | comp.addKeyListener(new KeyboardEventHandler(buildPlayerActionMap(), PLAYER_ACTION_CONSUMER)); | ||
65 | comp.requestFocusInWindow(); | ||
66 | } | ||
67 | |||
68 | private final XBlastComponent gui; | ||
69 | |||
70 | private GraphicalSimulation() { | ||
71 | this.gui = new XBlastComponent(); | ||
72 | buildFrame(this.gui); | ||
73 | attachKeyboardHandler(this.gui); | ||
74 | } | ||
75 | |||
76 | @Override | ||
77 | void displayGameState(GameState gs) { | ||
78 | this.gui.setGameState(translateToClientData(gs), MAIN_PLAYER); | ||
79 | delay(); | ||
80 | } | ||
81 | |||
82 | public static void main(String[] args) { | ||
83 | (new GraphicalSimulation()).runSimulation(); | ||
84 | } | ||
85 | |||
86 | } | ||
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 @@ | |||
1 | package ch.epfl.xblast.simulation; | ||
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; | ||
8 | import ch.epfl.xblast.server.Player; | ||
9 | 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 | |||
15 | /** | ||
16 | * Random game simulation. | ||
17 | * | ||
18 | * @author Pacien TRAN-GIRARD (261948) | ||
19 | */ | ||
20 | public class RandomSimulation { | ||
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. |