diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/Player.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/Player.java index 9bb0336..923a2f6 100644 --- a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/Player.java +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/Player.java @@ -5,7 +5,9 @@ import lombok.Setter; import thb.jeanluc.adventure.model.item.Item; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.Optional; +import java.util.Set; /** * The player avatar. Tracks the current room, an ordered inventory, and a @@ -15,9 +17,11 @@ import java.util.Optional; public class Player { /** Room the player is currently standing in. */ - @Setter private Room currentRoom; + /** Ids of rooms the player has entered, in first-visit order (for the map). */ + private final Set visitedRoomIds = new LinkedHashSet<>(); + /** * Inventory keyed by item id. {@link LinkedHashMap} keeps insertion * order for stable {@code inventory} listings while still providing @@ -44,6 +48,17 @@ public class Player { } this.currentRoom = startRoom; this.gold = startGold; + this.visitedRoomIds.add(startRoom.getId()); + } + + /** + * Moves the player into a room and records it as visited. + * + * @param room the room now occupied; must not be null + */ + public void setCurrentRoom(Room room) { + this.currentRoom = room; + this.visitedRoomIds.add(room.getId()); } /** diff --git a/Semesterprojekt/src/test/java/thb/jeanluc/adventure/model/PlayerTest.java b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/model/PlayerTest.java index 0b4fc6c..8a26e90 100644 --- a/Semesterprojekt/src/test/java/thb/jeanluc/adventure/model/PlayerTest.java +++ b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/model/PlayerTest.java @@ -54,4 +54,20 @@ class PlayerTest { assertThat(p.getCurrentRoom()).isEqualTo(b); } + + @Test + void startRoomIsVisitedFromTheStart() { + Room start = new Room("kitchen", "Kitchen", "desc"); + Player p = new Player(start, 0); + assertThat(p.getVisitedRoomIds()).containsExactly("kitchen"); + } + + @Test + void movingMarksTheNewRoomVisited() { + Room start = new Room("kitchen", "Kitchen", "desc"); + Room hall = new Room("hallway", "Hallway", "desc"); + Player p = new Player(start, 0); + p.setCurrentRoom(hall); + assertThat(p.getVisitedRoomIds()).containsExactly("kitchen", "hallway"); + } }