aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothée Floure2016-04-30 17:33:41 +0200
committerTimothée Floure2016-04-30 17:33:41 +0200
commitd60cd3c353b4132e3d37122a274ecc3cbe43b78e (patch)
tree8e3c468e29b00e1468231453f012e76bd524f656
parent8b92f6267c09c2e8936a853f28ddb6155e3d4ddc (diff)
downloadxblast-d60cd3c353b4132e3d37122a274ecc3cbe43b78e.tar.gz
Fix the GameStateSerializer (comply with the subject)
-rw-r--r--src/ch/epfl/xblast/RunLengthEncoder.java2
-rw-r--r--src/ch/epfl/xblast/server/GameStateSerializer.java50
-rw-r--r--src/ch/epfl/xblast/server/Level.java8
-rw-r--r--src/ch/epfl/xblast/server/painter/ExplosionPainter.java5
-rw-r--r--test/ch/epfl/xblast/GameStateSerializerTest.java44
5 files changed, 82 insertions, 27 deletions
diff --git a/src/ch/epfl/xblast/RunLengthEncoder.java b/src/ch/epfl/xblast/RunLengthEncoder.java
index ecb9f4e..d7ab106 100644
--- a/src/ch/epfl/xblast/RunLengthEncoder.java
+++ b/src/ch/epfl/xblast/RunLengthEncoder.java
@@ -189,4 +189,4 @@ public final class RunLengthEncoder {
189 .collect(Collectors.toList()); 189 .collect(Collectors.toList());
190 } 190 }
191 191
192} 192} \ No newline at end of file
diff --git a/src/ch/epfl/xblast/server/GameStateSerializer.java b/src/ch/epfl/xblast/server/GameStateSerializer.java
index 998ff21..049e993 100644
--- a/src/ch/epfl/xblast/server/GameStateSerializer.java
+++ b/src/ch/epfl/xblast/server/GameStateSerializer.java
@@ -47,13 +47,17 @@ public final class GameStateSerializer {
47 * @param bombedCells cells containing a bomb 47 * @param bombedCells cells containing a bomb
48 * @param blastedCells cells containing a blast 48 * @param blastedCells cells containing a blast
49 * @param cell cell hosting the explosion 49 * @param cell cell hosting the explosion
50 * @param board actual Board
50 * @return the serialized explosion 51 * @return the serialized explosion
51 */ 52 */
52 private static byte serializeExplosion(Map<Cell, Bomb> bombedCells, Set<Cell> blastedCells, Cell cell) { 53 private static byte serializeExplosion(Map<Cell, Bomb> bombedCells, Set<Cell> blastedCells, Cell cell, Board board) {
53 if (bombedCells.containsKey(cell)) 54 if (bombedCells.containsKey(cell))
54 return ExplosionPainter.byteForBomb(bombedCells.get(cell)); 55 return ExplosionPainter.byteForBomb(bombedCells.get(cell));
55 else 56
57 if (blastedCells.contains(cell) && board.blockAt(cell).isFree())
56 return serializeBlast(blastedCells, cell); 58 return serializeBlast(blastedCells, cell);
59
60 return ExplosionPainter.INVALID_EXPLOSION_IMAGE_ID;
57 } 61 }
58 62
59 /** 63 /**
@@ -61,15 +65,29 @@ public final class GameStateSerializer {
61 * 65 *
62 * @param bombedCells cells containing a bomb 66 * @param bombedCells cells containing a bomb
63 * @param blastedCells cells containing a blast 67 * @param blastedCells cells containing a blast
68 * @param board actual Board
64 * @return the serialized explosions 69 * @return the serialized explosions
65 */ 70 */
66 private static List<Byte> serializeExplosions(Map<Cell, Bomb> bombedCells, Set<Cell> blastedCells) { 71 private static List<Byte> serializeExplosions(Map<Cell, Bomb> bombedCells, Set<Cell> blastedCells, Board board) {
67 return RunLengthEncoder.encode(Cell.ROW_MAJOR_ORDER.stream() 72 return RunLengthEncoder.encode(Cell.ROW_MAJOR_ORDER.stream()
68 .map(c -> serializeExplosion(bombedCells, blastedCells, c)) 73 .map(c -> serializeExplosion(bombedCells, blastedCells, c, board))
69 .collect(Collectors.toList())); 74 .collect(Collectors.toList()));
70 } 75 }
71 76
72 /** 77 /**
78 * Prepend its size to a Byte List.
79 *
80 * @param list Byte List to be prefixed
81 * @return the Byte List prefixed with its size
82 */
83 private static List<Byte> prefixByteListWithSize(List<Byte> list) {
84 List<Byte> prefixedList = new ArrayList<>();
85 prefixedList.add((byte) list.size());
86 prefixedList.addAll(list);
87 return prefixedList;
88 }
89
90 /**
73 * Serialize a Player. 91 * Serialize a Player.
74 * 92 *
75 * @param player player to be serialized 93 * @param player player to be serialized
@@ -96,17 +114,27 @@ public final class GameStateSerializer {
96 */ 114 */
97 public static List<Byte> serialize(BoardPainter boardPainter, GameState gameState) { 115 public static List<Byte> serialize(BoardPainter boardPainter, GameState gameState) {
98 List<Byte> output = new ArrayList<>(); 116 List<Byte> output = new ArrayList<>();
99 List<Byte> data = new ArrayList<>();
100 117
101 data.addAll(serializeBoard(boardPainter, gameState.board())); // Serialize the Board 118 // Add the serialized Board to the output
102 data.addAll(serializeExplosions(gameState.bombedCells(), gameState.blastedCells())); // Serialize Explosions 119 output.addAll(
120 prefixByteListWithSize(
121 serializeBoard(boardPainter, gameState.board())
122 )
123 );
124
125 // Add the serialized Explosions to the output
126 output.addAll(
127 prefixByteListWithSize(
128 serializeExplosions(gameState.bombedCells(), gameState.blastedCells(), gameState.board())
129 )
130 );
131
132 // Add the serialized Players to the output
103 for (Player player : gameState.players()) { 133 for (Player player : gameState.players()) {
104 data.addAll(serializePlayer(player, gameState.ticks())); // Serialize each Player 134 output.addAll(serializePlayer(player, gameState.ticks())); // Serialize each Player
105 } 135 }
106 136
107 data.add((byte) (gameState.remainingTime() / 2)); // Add the state Tick to the data 137 output.add((byte) (gameState.remainingTime() / 2)); // Add the state Tick to the output
108 output.add((byte) data.size()); // Get the size of the whole data chunk and add it as first element of the output
109 output.addAll(data); // Add all the data to the output
110 138
111 return output; 139 return output;
112 } 140 }
diff --git a/src/ch/epfl/xblast/server/Level.java b/src/ch/epfl/xblast/server/Level.java
index 81bc85b..155b99a 100644
--- a/src/ch/epfl/xblast/server/Level.java
+++ b/src/ch/epfl/xblast/server/Level.java
@@ -13,15 +13,15 @@ import java.util.HashMap;
13public final class Level { 13public final class Level {
14 14
15 /** Players' initial parameters **/ 15 /** Players' initial parameters **/
16 private static final int PLAYER_INITIAL_LIVES = 5; 16 private static final int PLAYER_INITIAL_LIVES = 3;
17 private static final int PLAYER_INITIAL_BOMB_MAXIMUM = 5; 17 private static final int PLAYER_INITIAL_BOMB_MAXIMUM = 5;
18 private static final int PLAYER_INITIAL_BOMB_RANGE = 5; 18 private static final int PLAYER_INITIAL_BOMB_RANGE = 5;
19 19
20 /** Players' initial positions (Ugly!) **/ 20 /** Players' initial positions (Ugly!) **/
21 private static final Cell PLAYER_1_INITIAL_POSITION = new Cell(1,1); 21 private static final Cell PLAYER_1_INITIAL_POSITION = new Cell(1,1);
22 private static final Cell PLAYER_2_INITIAL_POSITION = new Cell(16,1); 22 private static final Cell PLAYER_2_INITIAL_POSITION = new Cell(13,1);
23 private static final Cell PLAYER_3_INITIAL_POSITION = new Cell(16,14); 23 private static final Cell PLAYER_3_INITIAL_POSITION = new Cell(13,11);
24 private static final Cell PLAYER_4_INITIAL_POSITION = new Cell(1,14); 24 private static final Cell PLAYER_4_INITIAL_POSITION = new Cell(1,11);
25 25
26 /** Level values **/ 26 /** Level values **/
27 private final BoardPainter painter; 27 private final BoardPainter painter;
diff --git a/src/ch/epfl/xblast/server/painter/ExplosionPainter.java b/src/ch/epfl/xblast/server/painter/ExplosionPainter.java
index 63181e9..83c6176 100644
--- a/src/ch/epfl/xblast/server/painter/ExplosionPainter.java
+++ b/src/ch/epfl/xblast/server/painter/ExplosionPainter.java
@@ -15,6 +15,11 @@ public final class ExplosionPainter {
15 private static final byte WEST_EXPLOSION = 0b0001; 15 private static final byte WEST_EXPLOSION = 0b0001;
16 16
17 /** 17 /**
18 * Image ID corresponding to an explosion-free (and bomb-free) cell.
19 */
20 public static final byte INVALID_EXPLOSION_IMAGE_ID = (byte) 16;
21
22 /**
18 * Return the image corresponding to a bomb depending on its fuse length. 23 * Return the image corresponding to a bomb depending on its fuse length.
19 * 24 *
20 * @param bomb given bomb 25 * @param bomb given bomb
diff --git a/test/ch/epfl/xblast/GameStateSerializerTest.java b/test/ch/epfl/xblast/GameStateSerializerTest.java
index a701d2b..881b1ec 100644
--- a/test/ch/epfl/xblast/GameStateSerializerTest.java
+++ b/test/ch/epfl/xblast/GameStateSerializerTest.java
@@ -5,9 +5,11 @@ import ch.epfl.xblast.server.Level;
5import org.junit.Assert; 5import org.junit.Assert;
6import org.junit.Test; 6import org.junit.Test;
7 7
8import java.lang.reflect.Array;
8import java.util.ArrayList; 9import java.util.ArrayList;
9import java.util.Arrays; 10import java.util.Arrays;
10import java.util.List; 11import java.util.List;
12import java.util.stream.Collectors;
11 13
12/** 14/**
13 * @author Timothée FLOURE (257420) 15 * @author Timothée FLOURE (257420)
@@ -15,25 +17,45 @@ import java.util.List;
15public class GameStateSerializerTest { 17public class GameStateSerializerTest {
16 18
17 @Test 19 @Test
18 public void GameStateSerializerTest() { 20 public void IntialGameStateSerializationTest() {
19 List<Integer> integerExpectedValues = Arrays.asList(121, -50, 2, 1, -2, 0, 3, 1, 3, 1, -2, 0, 1, 1, 3, 1, 3, 21 List<Integer> sourceValues = Arrays.asList(
22 // Serialized Board
23 121, -50, 2, 1, -2, 0, 3, 1, 3, 1, -2, 0, 1, 1, 3, 1, 3,
20 1, 3, 1, 1, -2, 0, 1, 3, 1, 3, -2, 0, -1, 1, 3, 1, 3, 1, 24 1, 3, 1, 1, -2, 0, 1, 3, 1, 3, -2, 0, -1, 1, 3, 1, 3, 1,
21 3, 1, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 25 3, 1, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
22 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 26 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2,
23 3, 1, 0, 0, 3, 1, 3, 1, 0, 0, 1, 1, 3, 1, 1, 0, 0, 1, 3, 27 3, 1, 0, 0, 3, 1, 3, 1, 0, 0, 1, 1, 3, 1, 1, 0, 0, 1, 3,
24 1, 3, 0, 0, -1, 1, 3, 1, 1, -5, 2, 3, 2, 3, -5, 2, 3, 2, 28 1, 3, 0, 0, -1, 1, 3, 1, 1, -5, 2, 3, 2, 3, -5, 2, 3, 2,
25 3, 1, -2, 0, 3, -2, 0, 1, 3, 2, 1, 2, 4, -128, 16, -63, 29 3, 1, -2, 0, 3, -2, 0, 1, 3, 2, 1, 2,
26 16, 3, 24, 24, 6, 3, -40, 24, 26, 3, -40, -72, 46, 3, 24, 30 // Explosions (blasts & bombs)
27 -72, 66, 60); 31 4, -128, 16, -63, 16,
28 List<Byte> expectedValues = new ArrayList<>(); 32 // Players
33 3, 24, 24, 6,
34 3, -40, 24, 26,
35 3, -40, -72, 46,
36 3, 24, -72, 66,
37 // Ticks
38 60);
29 39
30 for (Integer i : integerExpectedValues) { 40 // Build a List of Bytes from the Expected Values
31 expectedValues.add((byte) i.intValue()); 41 List<Byte> expectedValues = sourceValues.stream().map(i -> (byte) i.intValue()).collect(Collectors.toList());
32 }