Skip to content

Commit 5da8a34

Browse files
committed
fixes
1 parent bfa3bb4 commit 5da8a34

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

src/ch/epfl/chacun/ActionEncoder.java

+24-20
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ public static StateAction withOccupantRemoved(GameState gameState, Occupant occu
130130
return new StateAction(gameState.withOccupantRemoved(occupant), Base32.encodeBits5(indexToEncode));
131131
}
132132

133+
private static <T> T getIndexOrThrows(List<T> list, int index) throws IllegalActionException {
134+
if (list.size() < index) throw new IllegalActionException();
135+
return list.get(index);
136+
}
137+
138+
private static void validateActionLength(String action, int length) throws IllegalActionException {
139+
if (action.length() != length) throw new IllegalActionException();
140+
}
141+
133142
/**
134143
* Decode and apply the given action encoded in Base32 to the given game state,
135144
* throwing an IllegalActionException if the action is invalid.
@@ -142,52 +151,47 @@ public static StateAction withOccupantRemoved(GameState gameState, Occupant occu
142151
*/
143152
private static StateAction decodeAndApplyWithException(GameState gameState, String action)
144153
throws IllegalActionException {
145-
Preconditions.checkValidAction(Base32.isValid(action));
154+
if (!Base32.isValid(action)) throw new IllegalActionException();
146155
// we decode the parameters of the given action differently
147156
// depending on the next action of the initial game state
148157
return switch (gameState.nextAction()) {
149158
case PLACE_TILE -> { //
150-
Preconditions.checkValidAction(action.length() == WITH_PLACED_TILE_ACTION_LENGTH);
159+
validateActionLength(action, WITH_PLACED_TILE_ACTION_LENGTH);
151160
int decoded = Base32.decode(action);
152161
int tileInFringeIdx = decoded >> WITH_PLACED_TILE_IDX_SHIFT;
153162
int rotationIdx = decoded & WITH_PLACED_TILE_ROTATION_MASK;
154-
List<Pos> fringeIndexes = fringeIndexes(gameState);
155-
Preconditions.checkValidAction(fringeIndexes.size() > tileInFringeIdx);
156-
Pos pos = fringeIndexes.get(tileInFringeIdx);
163+
Pos pos = getIndexOrThrows(fringeIndexes(gameState), tileInFringeIdx);
157164
Tile tile = gameState.tileToPlace();
158165
PlacedTile placedTile = new PlacedTile(
159-
tile, gameState.currentPlayer(), Rotation.ALL.get(rotationIdx), pos
166+
tile, gameState.currentPlayer(), Rotation.ALL.get(rotationIdx), pos
160167
);
161-
Preconditions.checkValidAction(gameState.board().canAddTile(placedTile));
168+
if (!gameState.board().canAddTile(placedTile)) throw new IllegalActionException();
162169
yield new StateAction(gameState.withPlacedTile(placedTile), action);
163170
}
164171
case OCCUPY_TILE -> {
165-
Preconditions.checkValidAction(action.length() == WITH_NEW_OCCUPANT_ACTION_LENGTH);
172+
validateActionLength(action, WITH_NEW_OCCUPANT_ACTION_LENGTH);
166173
int decoded = Base32.decode(action);
167174
if (decoded == WITH_NO_OCCUPANT) yield new StateAction(gameState.withNewOccupant(null), action);
168175
int kindIdx = decoded >> WITH_NEW_OCCUPANT_KIND_SHIFT;
169176
int localId = decoded & WITH_NEW_OCCUPANT_ZONE_MASK;
170177
Occupant.Kind kind = Occupant.Kind.ALL.get(kindIdx);
171178
Occupant occupant = gameState.lastTilePotentialOccupants().stream()
172-
.filter(occ -> occ.kind() == kind && Zone.localId(occ.zoneId()) == localId)
173-
.findFirst()
174-
.orElseThrow(IllegalActionException::new);
179+
.filter(occ -> occ.kind() == kind && Zone.localId(occ.zoneId()) == localId)
180+
.findFirst()
181+
.orElseThrow(IllegalActionException::new);
175182
yield new StateAction(gameState.withNewOccupant(occupant), action);
176183
}
177184
case RETAKE_PAWN -> {
178-
Preconditions.checkValidAction(action.length() == WITH_OCCUPANT_REMOVED_ACTION_LENGTH);
185+
validateActionLength(action, WITH_OCCUPANT_REMOVED_ACTION_LENGTH);
179186
int decoded = Base32.decode(action);
180187
if (decoded == WITH_NO_OCCUPANT) yield new StateAction(gameState.withOccupantRemoved(null), action);
181-
List<Occupant> occupants = gameState.board().occupants()
182-
.stream()
183-
.sorted(Comparator.comparingInt(Occupant::zoneId))
184-
.toList();
185-
Preconditions.checkValidAction(occupants.size() > decoded);
186-
Occupant occupant = occupants.get(decoded);
187-
Preconditions.checkValidAction(occupant.kind() == Occupant.Kind.PAWN);
188+
List<Occupant> occupants = gameState.board().occupants().stream()
189+
.sorted(Comparator.comparingInt(Occupant::zoneId)).toList();
190+
Occupant occupant = getIndexOrThrows(occupants, decoded);
191+
if (occupant.kind() != Occupant.Kind.PAWN) throw new IllegalActionException();
188192
PlayerColor currentPlayer = gameState.currentPlayer();
189193
PlayerColor occupantPlacer = gameState.board().tileWithId(Zone.tileId(occupant.zoneId())).placer();
190-
Preconditions.checkValidAction(currentPlayer == occupantPlacer);
194+
if (currentPlayer != occupantPlacer) throw new IllegalActionException();
191195
yield new StateAction(gameState.withOccupantRemoved(occupant), action);
192196
}
193197
default -> throw new IllegalActionException();

src/ch/epfl/chacun/Preconditions.java

-5
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,5 @@ public static void checkArgument(boolean shouldBeTrue) {
2020
if (!shouldBeTrue) throw new IllegalArgumentException();
2121
}
2222

23-
public static void checkValidAction(boolean shouldBeTrue) throws ActionEncoder.IllegalActionException {
24-
if (!shouldBeTrue) {
25-
throw new ActionEncoder.IllegalActionException();
26-
}
27-
}
2823
}
2924

src/ch/epfl/chacun/gui/MessageBoardUI.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public static Node create(
6262
vBox.getChildren().add(text);
6363
});
6464

65-
Platform.runLater(() -> scrollPane.setVvalue(1));
65+
scrollPane.layout();
66+
scrollPane.setVvalue(1);
6667
});
6768

6869
return scrollPane;

0 commit comments

Comments
 (0)