Skip to content

Commit

Permalink
Fix Base32 decode
Browse files Browse the repository at this point in the history
  • Loading branch information
Mw3y committed May 8, 2024
1 parent 9a3e9ac commit a0e170d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
4 changes: 0 additions & 4 deletions src/ch/epfl/chacun/ActionEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
15 changes: 8 additions & 7 deletions src/ch/epfl/chacun/Base32.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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));
}

Expand All @@ -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);
}

/**
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/ch/epfl/chacun/gui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ private TileDecks createTileDecksWithSeed(String rawSeed) {
}

List<Tile> 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)));
Expand Down

0 comments on commit a0e170d

Please sign in to comment.