feat(game): Pathfinder BFS over visited rooms (locks + light aware)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-01 20:08:11 +02:00
parent 7c1cdc545e
commit 663a78ed9e
2 changed files with 164 additions and 0 deletions

View File

@@ -0,0 +1,88 @@
package thb.jeanluc.adventure.game;
import org.junit.jupiter.api.Test;
import thb.jeanluc.adventure.io.TestIO;
import thb.jeanluc.adventure.model.Condition;
import thb.jeanluc.adventure.model.Direction;
import thb.jeanluc.adventure.model.ExitLock;
import thb.jeanluc.adventure.model.Player;
import thb.jeanluc.adventure.model.Room;
import thb.jeanluc.adventure.model.World;
import thb.jeanluc.adventure.model.item.Item;
import thb.jeanluc.adventure.model.item.PlainItem;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
class PathfinderTest {
private Room room(String id) {
return new Room(id, id, "d");
}
private GameContext ctx(Room start, List<Room> all, Set<String> visited) {
Map<String, Room> rooms = new LinkedHashMap<>();
for (Room r : all) {
rooms.put(r.getId(), r);
}
World w = new World(rooms, Map.of(), Map.of(), "t", "w");
Player p = new Player(start, 0);
p.getVisitedRoomIds().addAll(visited);
return new GameContext(w, p, new TestIO());
}
@Test
void findsShortestPathOverVisitedRooms() {
Room a = room("a"), b = room("b"), c = room("c");
a.addExit(Direction.NORTH, b);
b.addExit(Direction.NORTH, c);
GameContext ctx = ctx(a, List.of(a, b, c), Set.of("a", "b", "c"));
assertThat(Pathfinder.findPath(ctx, c)).containsExactly(b, c);
}
@Test
void willNotRouteThroughUnvisitedRoom() {
Room a = room("a"), b = room("b"), c = room("c");
a.addExit(Direction.NORTH, b);
b.addExit(Direction.NORTH, c);
GameContext ctx = ctx(a, List.of(a, b, c), Set.of("a", "c")); // b not visited
assertThat(Pathfinder.findPath(ctx, c)).isEmpty();
}
@Test
void respectsLockedExit() {
Room a = room("a"), b = room("b");
a.addExit(Direction.NORTH, b);
a.addExitLock(Direction.NORTH,
new ExitLock(List.of(new Condition(Condition.Type.FLAG, "open")), "locked"));
GameContext ctx = ctx(a, List.of(a, b), Set.of("a", "b"));
assertThat(Pathfinder.findPath(ctx, b)).isEmpty();
ctx.getState().set("open");
assertThat(Pathfinder.findPath(ctx, b)).containsExactly(b);
}
@Test
void respectsDarkness() {
Room a = room("a");
Room dark = room("dark");
dark.setDark(true);
a.addExit(Direction.NORTH, dark);
GameContext ctx = ctx(a, List.of(a, dark), Set.of("a", "dark"));
assertThat(Pathfinder.findPath(ctx, dark)).isEmpty();
Item torch = PlainItem.builder().id("lit").name("Lit").description("d").light(true).build();
ctx.getPlayer().addItem(torch);
assertThat(Pathfinder.findPath(ctx, dark)).containsExactly(dark);
}
@Test
void unreachableTargetReturnsEmpty() {
Room a = room("a");
Room island = room("island"); // no connecting exits
GameContext ctx = ctx(a, List.of(a, island), Set.of("a", "island"));
assertThat(Pathfinder.findPath(ctx, island)).isEmpty();
}
}