From d7c29fd93719727544f22d079ca098750d98edb1 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 9 May 2016 14:08:32 +0200 Subject: Implement board tiles re-ordering --- .../epfl/xblast/client/GameStateDeserializer.java | 42 ++++++++++++++++++---- 1 file changed, 36 insertions(+), 6 deletions(-) (limited to 'src/ch/epfl') 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 @@ package ch.epfl.xblast.client; -import ch.epfl.xblast.Lists; -import ch.epfl.xblast.PlayerID; -import ch.epfl.xblast.RunLengthEncoder; -import ch.epfl.xblast.SubCell; +import ch.epfl.xblast.*; import ch.epfl.xblast.client.GameState.Player; import java.awt.*; import java.util.*; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.IntStream; /** * The client game state deserializer. @@ -116,6 +114,38 @@ public final class GameStateDeserializer { .collect(Collectors.toList()); } + /** + * Unserializes a spiral-ordered byte representation of a board. + * + * @param serializedBoard the run-length-compressed, serialized spiral ordered board + * @return the row-major-ordered board + * @throws IllegalArgumentException if the length of the serialized board is invalid + */ + private static List deserializeBoard(List serializedBoard) { + List spiralOrdered = deserializeImageChunk(serializedBoard, ImageCollection.BOARD_IMAGES); + + if (spiralOrdered.size() != Cell.SPIRAL_ORDER.size()) + throw new IllegalArgumentException(); + + Map imageMap = IntStream + .range(0, Cell.SPIRAL_ORDER.size()).mapToObj(i -> i) + .collect(Collectors.toMap(Cell.SPIRAL_ORDER::get, spiralOrdered::get)); + + return Cell.ROW_MAJOR_ORDER.stream() + .map(imageMap::get) + .collect(Collectors.toList()); + } + + /** + * Unserializes a byte representation of explosions and bombs. + * + * @param serializedExplosions the run-length-compressed, serialized explosions + * @return the explosions + */ + private static List deserializeExplosions(List serializedExplosions) { + return deserializeImageChunk(serializedExplosions, ImageCollection.EXPLOSIONS_IMAGES); + } + /** * Unserializes a byte representation of a Player. * @@ -176,10 +206,10 @@ public final class GameStateDeserializer { public static GameState deserialize(List serializedData) { VariableLengthChunk chunks = new VariableLengthChunk(serializedData); - List board = deserializeImageChunk(chunks.head(), ImageCollection.BOARD_IMAGES); + List board = deserializeBoard(chunks.head()); chunks = chunks.tail(); - List explosions = deserializeImageChunk(chunks.head(), ImageCollection.EXPLOSIONS_IMAGES); + List explosions = deserializeExplosions(chunks.head()); chunks = chunks.tail(); List players = deserializePlayers(chunks.head(SERIALIZED_PLAYERS_LENGTH)); -- cgit v1.2.3