This commit is contained in:
parent
49725fe7df
commit
b18e7fba9e
3 changed files with 26 additions and 28 deletions
|
@ -35,18 +35,18 @@ public class Tile {
|
|||
private Tile(@NotNull final EnumSet<Direction> walls, final boolean solution) {
|
||||
for (final Direction direction : walls) {
|
||||
this.walls.set(direction);
|
||||
this.walls.harden(direction);
|
||||
this.walls.seal(direction);
|
||||
}
|
||||
this.visited = true;
|
||||
this.solution = solution;
|
||||
}
|
||||
|
||||
public void preventDiggingToOrFrom(@NotNull final Direction direction) {
|
||||
this.walls.harden(direction);
|
||||
this.walls.seal(direction);
|
||||
}
|
||||
|
||||
public void enableDiggingToOrFrom(@NotNull final Direction direction) {
|
||||
this.walls.unharden(direction);
|
||||
this.walls.unseal(direction);
|
||||
}
|
||||
|
||||
public boolean digFrom(@NotNull final Direction direction) {
|
||||
|
@ -66,7 +66,7 @@ public class Tile {
|
|||
}
|
||||
|
||||
public Option<Direction> getRandomAvailableDirection(@NotNull final Random random) {
|
||||
final Stream<Direction> availableDirections = this.walls.getUnhardenedSet();
|
||||
final Stream<Direction> availableDirections = this.walls.getUnsealedSet();
|
||||
if (availableDirections.isEmpty()) {
|
||||
return Option.none();
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ import java.util.TreeSet;
|
|||
@ToString
|
||||
public class Walls {
|
||||
private final SortedSet<Direction> directions = new TreeSet<>();
|
||||
private final Set<Direction> hardened = new HashSet<>();
|
||||
private final Set<Direction> sealed = new HashSet<>();
|
||||
|
||||
public void set(@NotNull final Direction direction) {
|
||||
this.directions.add(direction);
|
||||
|
@ -26,7 +26,7 @@ public class Walls {
|
|||
}
|
||||
|
||||
public boolean clear(@NotNull final Direction direction) {
|
||||
if (this.hardened.contains(direction)) {
|
||||
if (this.sealed.contains(direction)) {
|
||||
return false;
|
||||
}
|
||||
return this.directions.remove(direction);
|
||||
|
@ -36,19 +36,19 @@ public class Walls {
|
|||
return this.directions.contains(direction);
|
||||
}
|
||||
|
||||
public Stream<Direction> getUnhardenedSet() {
|
||||
public Stream<Direction> getUnsealedSet() {
|
||||
return Stream.ofAll(this.directions)
|
||||
.removeAll(this.hardened);
|
||||
.removeAll(this.sealed);
|
||||
}
|
||||
|
||||
public void harden(@NotNull final Direction direction) {
|
||||
public void seal(@NotNull final Direction direction) {
|
||||
if (!this.directions.contains(direction)) {
|
||||
throw new IllegalStateException("Trying to harden cleared Direction: " + direction);
|
||||
throw new IllegalStateException("Trying to seal cleared Direction: " + direction);
|
||||
}
|
||||
this.hardened.add(direction);
|
||||
this.sealed.add(direction);
|
||||
}
|
||||
|
||||
public void unharden(@NotNull final Direction direction) {
|
||||
this.hardened.remove(direction);
|
||||
public void unseal(@NotNull final Direction direction) {
|
||||
this.sealed.remove(direction);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package ch.fritteli.maze.generator.model;
|
||||
|
||||
import ch.fritteli.maze.generator.model.Direction;
|
||||
import ch.fritteli.maze.generator.model.Walls;
|
||||
import io.vavr.collection.Stream;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -65,14 +63,14 @@ class WallsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
void testHarden() {
|
||||
void testSeal() {
|
||||
// arrange
|
||||
final Walls sut = new Walls();
|
||||
sut.set(Direction.TOP);
|
||||
sut.set(Direction.RIGHT);
|
||||
|
||||
// act
|
||||
sut.harden(Direction.TOP);
|
||||
sut.seal(Direction.TOP);
|
||||
|
||||
// assert
|
||||
assertThat(sut)
|
||||
|
@ -88,34 +86,34 @@ class WallsTest {
|
|||
assertThat(result).isFalse();
|
||||
assertThat(sut.isSet(Direction.TOP)).isTrue();
|
||||
|
||||
// act / assert: try to harden un-set wall
|
||||
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> sut.harden(Direction.LEFT));
|
||||
// act / assert: try to seal un-set wall
|
||||
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> sut.seal(Direction.LEFT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUnharden() {
|
||||
void testUnseal() {
|
||||
// arrange
|
||||
final Walls sut = new Walls();
|
||||
sut.setAll();
|
||||
sut.harden(Direction.TOP);
|
||||
sut.seal(Direction.TOP);
|
||||
|
||||
// pre-assert: TOP can't be cleared while hardened
|
||||
assertThat(sut.clear(Direction.TOP)).isFalse();
|
||||
|
||||
// act
|
||||
sut.unharden(Direction.TOP);
|
||||
sut.unseal(Direction.TOP);
|
||||
|
||||
// assert: TOP can be cleared
|
||||
assertThat(sut.clear(Direction.TOP)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUnhardenedSet() {
|
||||
void testGetUnsealedSet() {
|
||||
// arrange
|
||||
final Walls sut = new Walls();
|
||||
|
||||
// act
|
||||
Stream<Direction> result = sut.getUnhardenedSet();
|
||||
Stream<Direction> result = sut.getUnsealedSet();
|
||||
|
||||
// assert
|
||||
assertThat(result).isEmpty();
|
||||
|
@ -125,16 +123,16 @@ class WallsTest {
|
|||
sut.set(Direction.LEFT);
|
||||
|
||||
// act
|
||||
result = sut.getUnhardenedSet();
|
||||
result = sut.getUnsealedSet();
|
||||
|
||||
// assert
|
||||
assertThat(result).containsExactly(Direction.TOP, Direction.LEFT);
|
||||
|
||||
// arrange: harden a direction
|
||||
sut.harden(Direction.TOP);
|
||||
// arrange: seal a direction
|
||||
sut.seal(Direction.TOP);
|
||||
|
||||
// act
|
||||
result = sut.getUnhardenedSet();
|
||||
result = sut.getUnsealedSet();
|
||||
|
||||
// assert
|
||||
assertThat(result).containsExactly(Direction.LEFT);
|
||||
|
|
Loading…
Reference in a new issue