diff --git a/src/ch/epfl/chacun/ActionEncoder.java b/src/ch/epfl/chacun/ActionEncoder.java index 59bf682..e2dcf4d 100644 --- a/src/ch/epfl/chacun/ActionEncoder.java +++ b/src/ch/epfl/chacun/ActionEncoder.java @@ -160,10 +160,6 @@ public static StateAction decodeAndApply(GameState gameState, String action) { * @throws IllegalActionException if the action is illegal */ private static StateAction unsafeDecodeAndApply(GameState gameState, String action) throws IllegalActionException { - // Check if the received action is valid - if (!Base32.isValid(action) || action.isEmpty()) - throw new IllegalActionException(); - int decodedAction = Base32.decode(action); // Execute the provided action based on the next action context return switch (gameState.nextAction()) { diff --git a/src/ch/epfl/chacun/Base32.java b/src/ch/epfl/chacun/Base32.java index 54ed717..572b20e 100644 --- a/src/ch/epfl/chacun/Base32.java +++ b/src/ch/epfl/chacun/Base32.java @@ -14,9 +14,9 @@ public class Base32 { public static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; /** - * The size of the base 32 alphabet. + * The number of bits to represent a symbol in base 32. */ - public static final int ALPHABET_SIZE = ALPHABET.length(); + private static final int BASE_32_SYMBOL_BITS = 5; /** * Checks if the given string is valid in base 32. @@ -36,7 +36,7 @@ public static boolean isValid(String string) { */ public static String encodeBits5(int value) { // Keep only the 5 less significand bits - int fiveLSB = value & ((1 << 5) - 1); + int fiveLSB = value & ((1 << BASE_32_SYMBOL_BITS) - 1); return String.valueOf(ALPHABET.charAt(fiveLSB)); } @@ -48,7 +48,7 @@ public static String encodeBits5(int value) { */ public static String encodeBits10(int value) { // Encoded independently two 5-bit parts and merge them - return encodeBits5(value) + encodeBits5(value >> 5); + return encodeBits5(value >> BASE_32_SYMBOL_BITS) + encodeBits5(value); } /** @@ -60,11 +60,12 @@ public static String encodeBits10(int value) { * @return the integer corresponding to the decoded value */ public static int decode(String value) { + Preconditions.checkArgument(value.length() == 1 || value.length() == 2); + Preconditions.checkArgument(isValid(value)); int decodedValue = 0; - // For each value, add its index in the base 32 alphabet times the corresponding power of 32 - // to the decoded integer value + // For each value, add its index in the base 32 alphabet times for (int i = 0; i < value.length(); ++i) { - decodedValue += (int) (ALPHABET.indexOf(value.charAt(i)) * Math.pow(ALPHABET_SIZE, i)); + decodedValue = decodedValue << BASE_32_SYMBOL_BITS | ALPHABET.indexOf(value.charAt(i)); } return decodedValue; } diff --git a/src/ch/epfl/chacun/gui/Main.java b/src/ch/epfl/chacun/gui/Main.java index 26e4bb3..4949017 100644 --- a/src/ch/epfl/chacun/gui/Main.java +++ b/src/ch/epfl/chacun/gui/Main.java @@ -188,10 +188,10 @@ private TileDecks createTileDecksWithSeed(String rawSeed) { } List tiles = new ArrayList<>(Tiles.TILES); - for (int i = 79; i <= 94; ++i) { - if (i != 88) - tiles.remove(Tiles.TILES.get(i)); - } + // for (int i = 79; i <= 94; ++i) { + // if (i != 88) + // tiles.remove(Tiles.TILES.get(i)); + /// } Collections.shuffle(tiles, shuffler); // Group tiles by kind to create the decks return new TileDecks(tiles.stream().collect(Collectors.groupingBy(Tile::kind)));