aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-10 17:16:34 +0200
committerPacien TRAN-GIRARD2016-05-10 17:16:34 +0200
commit634a063e523e6f902b574aefe710d4869f3033de (patch)
tree0b306fa6e7acd1965c130f02229b8db5181160c5 /test
parent400a818b298bbb95771a5c63b330f0276114ab3e (diff)
parent53bd5fc4c164eb45d5e0aa34dc103579c9619a32 (diff)
downloadxblast-634a063e523e6f902b574aefe710d4869f3033de.tar.gz
Merge branch '10_graphical_interface'
Conflicts: src/ch/epfl/xblast/Lists.java
Diffstat (limited to 'test')
-rw-r--r--test/ch/epfl/xblast/ListsTest.java47
-rw-r--r--test/ch/epfl/xblast/simulation/ConsoleSimulation.java23
-rw-r--r--test/ch/epfl/xblast/simulation/GraphicalSimulation.java86
-rw-r--r--test/ch/epfl/xblast/simulation/RandomSimulation.java80
-rw-r--r--test/ch/epfl/xblast/simulation/Simulation.java54
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
3import org.junit.Test; 3import org.junit.Test;
4 4
5import java.util.ArrayList; 5import java.util.*;
6import java.util.Arrays;
7import java.util.LinkedList;
8import java.util.List;
9import java.util.stream.Collectors; 6import java.util.stream.Collectors;
10import java.util.stream.IntStream; 7import 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 @@
1package ch.epfl.xblast.simulation;
2
3import ch.epfl.xblast.server.GameState;
4import ch.epfl.xblast.server.debug.GameStatePrinter;
5
6/**
7 * Random game simulation printed in the console.
8 *
9 * @author Pacien TRAN-GIRARD (261948)
10 */
11public 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 @@
1package ch.epfl.xblast.simulation;
2
3import ch.epfl.xblast.PlayerAction;
4import ch.epfl.xblast.PlayerID;
5import ch.epfl.xblast.client.GameStateDeserializer;
6import ch.epfl.xblast.client.KeyboardEventHandler;
7import ch.epfl.xblast.client.XBlastComponent;
8import ch.epfl.xblast.server.GameState;
9import ch.epfl.xblast.server.GameStateSerializer;
10import ch.epfl.xblast.server.Level;
11import ch.epfl.xblast.server.painter.BoardPainter;
12
13import javax.swing.*;
14import java.awt.*;
15import java.awt.event.KeyEvent;
16import java.util.Collections;
17import java.util.HashMap;
18import java.util.List;
19import java.util.Map;
20import java.util.function.Consumer;
21
22/**
23 * Graphical game simulation.
24 *
25 * @author Timothée FLOURE (257420)
26 * @author Pacien TRAN-GIRARD (261948)
27 */
28public 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 @@
1package ch.epfl.xblast.simulation;
2
3import ch.epfl.xblast.Cell;
4import ch.epfl.xblast.PlayerID;
5import ch.epfl.xblast.server.Board;
6import ch.epfl.xblast.server.BoardTest;
7import ch.epfl.xblast.server.GameState;
8import ch.epfl.xblast.server.Player;
9import ch.epfl.xblast.server.debug.GameStatePrinter;
10import ch.epfl.xblast.server.debug.RandomEventGenerator;
11
12import java.util.Arrays;
13import java.util.List;
14
15/**
16 * Random game simulation.
17 *
18 * @author Pacien TRAN-GIRARD (261948)
19 */
20public 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 }