aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-09 14:08:32 +0200
committerPacien TRAN-GIRARD2016-05-09 14:08:32 +0200
commitd7c29fd93719727544f22d079ca098750d98edb1 (patch)
treed2c23690a9254f76985d736293f21558e66936e3 /src/ch/epfl
parente50e68d0523ece7cdf772b12904a6c0dafa2ec4a (diff)
downloadxblast-d7c29fd93719727544f22d079ca098750d98edb1.tar.gz
Implement board tiles re-ordering
Diffstat (limited to 'src/ch/epfl')
-rw-r--r--src/ch/epfl/xblast/client/GameStateDeserializer.java42
1 files changed, 36 insertions, 6 deletions
diff --git a/src/ch/epfl/xblast/client/GameStateDeserializer.java b/src/ch/epfl/xblast/client/GameStateDeserializer.java
index 20b44f9..143641d 100644
--- a/src/ch/epfl/xblast/client/GameStateDeserializer.java
+++ b/src/ch/epfl/xblast/client/GameStateDeserializer.java
@@ -1,15 +1,13 @@
1package ch.epfl.xblast.client; 1package ch.epfl.xblast.client;
2 2
3import ch.epfl.xblast.Lists; 3import ch.epfl.xblast.*;
4import ch.epfl.xblast.PlayerID;
5import ch.epfl.xblast.RunLengthEncoder;
6import ch.epfl.xblast.SubCell;
7import ch.epfl.xblast.client.GameState.Player; 4import ch.epfl.xblast.client.GameState.Player;
8 5
9import java.awt.*; 6import java.awt.*;
10import java.util.*; 7import java.util.*;
11import java.util.List; 8import java.util.List;
12import java.util.stream.Collectors; 9import java.util.stream.Collectors;
10import java.util.stream.IntStream;
13 11
14/** 12/**
15 * The client game state deserializer. 13 * The client game state deserializer.
@@ -117,6 +115,38 @@ public final class GameStateDeserializer {
117 } 115 }
118 116
119 /** 117 /**
118 * Unserializes a spiral-ordered byte representation of a board.
119 *
120 * @param serializedBoard the run-length-compressed, serialized spiral ordered board
121 * @return the row-major-ordered board
122 * @throws IllegalArgumentException if the length of the serialized board is invalid
123 */
124 private static List<Image> deserializeBoard(List<Byte> serializedBoard) {
125 List<Image> spiralOrdered = deserializeImageChunk(serializedBoard, ImageCollection.BOARD_IMAGES);
126
127 if (spiralOrdered.size() != Cell.SPIRAL_ORDER.size())
128 throw new IllegalArgumentException();
129
130 Map<Cell, Image> imageMap = IntStream
131 .range(0, Cell.SPIRAL_ORDER.size()).mapToObj(i -> i)
132 .collect(Collectors.toMap(Cell.SPIRAL_ORDER::get, spiralOrdered::get));
133
134 return Cell.ROW_MAJOR_ORDER.stream()
135 .map(imageMap::get)
136 .collect(Collectors.toList());
137 }
138
139 /**
140 * Unserializes a byte representation of explosions and bombs.
141 *
142 * @param serializedExplosions the run-length-compressed, serialized explosions
143 * @return the explosions
144 */
145 private static List<Image> deserializeExplosions(List<Byte> serializedExplosions) {
146 return deserializeImageChunk(serializedExplosions, ImageCollection.EXPLOSIONS_IMAGES);
147 }
148
149 /**
120 * Unserializes a byte representation of a Player. 150 * Unserializes a byte representation of a Player.
121 * 151 *
122 * @param id the PlayerID 152 * @param id the PlayerID
@@ -176,10 +206,10 @@ public final class GameStateDeserializer {
176 public static GameState deserialize(List<Byte> serializedData) { 206 public static GameState deserialize(List<Byte> serializedData) {
177 VariableLengthChunk chunks = new VariableLengthChunk(serializedData); 207 VariableLengthChunk chunks = new VariableLengthChunk(serializedData);
178 208
179 List<Image> board = deserializeImageChunk(chunks.head(), ImageCollection.BOARD_IMAGES); 209 List<Image> board = deserializeBoard(chunks.head());
180 chunks = chunks.tail(); 210 chunks = chunks.tail();
181 211
182 List<Image> explosions = deserializeImageChunk(chunks.head(), ImageCollection.EXPLOSIONS_IMAGES); 212 List<Image> explosions = deserializeExplosions(chunks.head());
183 chunks = chunks.tail(); 213 chunks = chunks.tail();
184 214
185 List<Player> players = deserializePlayers(chunks.head(SERIALIZED_PLAYERS_LENGTH)); 215 List<Player> players = deserializePlayers(chunks.head(SERIALIZED_PLAYERS_LENGTH));