Refactor Directions.

This commit is contained in:
Manuel Friedli 2020-09-30 22:29:06 +02:00
parent 3958b3504d
commit 8c68ea4844
2 changed files with 6 additions and 21 deletions

View file

@ -11,7 +11,7 @@ import lombok.experimental.FieldDefaults;
public class Tile { public class Tile {
// FIXME remove me; only for debugging // FIXME remove me; only for debugging
@Getter @Getter
final Directions walls = new Directions(); final Walls walls = new Walls();
@Getter @Getter
boolean visited = false; boolean visited = false;
@ -44,7 +44,7 @@ public class Tile {
} }
public Option<Direction> getRandomAvailableDirection() { public Option<Direction> getRandomAvailableDirection() {
return Stream.ofAll(this.walls.getSet(true)).shuffle().headOption(); return Stream.ofAll(this.walls.getUnhardenedSet()).shuffle().headOption();
} }
public boolean hasWallAt(@NonNull final Direction direction) { public boolean hasWallAt(@NonNull final Direction direction) {

View file

@ -1,17 +1,14 @@
package ch.fritteli.labyrinth; package ch.fritteli.labyrinth;
import io.vavr.collection.Stream; import io.vavr.collection.Stream;
import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
public class Directions { public class Walls {
private final Set<Direction> directions = new HashSet<>(); private final Set<Direction> directions = new HashSet<>();
// FIXME remove getter, only for debugging
@Getter
private final Set<Direction> hardened = new HashSet<>(); private final Set<Direction> hardened = new HashSet<>();
public boolean set(@NonNull final Direction direction) { public boolean set(@NonNull final Direction direction) {
@ -29,25 +26,13 @@ public class Directions {
return this.directions.remove(direction); return this.directions.remove(direction);
} }
public void clearAll() {
this.directions.clear();
this.directions.addAll(this.hardened);
}
public boolean isSet(@NonNull final Direction direction) { public boolean isSet(@NonNull final Direction direction) {
return this.directions.contains(direction); return this.directions.contains(direction);
} }
public Iterable<Direction> getSet() { public Iterable<Direction> getUnhardenedSet() {
return this.getSet(false); return Stream.ofAll(this.directions)
} .removeAll(this.hardened);
public Iterable<Direction> getSet(final boolean onlyBrittleWalls) {
if (onlyBrittleWalls) {
return Stream.ofAll(this.directions)
.removeAll(this.hardened);
}
return Stream.ofAll(this.directions);
} }
public void harden(@NonNull final Direction direction) { public void harden(@NonNull final Direction direction) {