This commit is contained in:
2025-02-18 13:23:39 +01:00
parent dc93484457
commit 2368d3b492
2 changed files with 43 additions and 69 deletions

View File

@@ -52,17 +52,40 @@ public class Game
wizardsRoom = new Room("in a wizards room"); wizardsRoom = new Room("in a wizards room");
// initialise room exits // initialise room exits
marketsquare.setExits(tavern, templePyramid, null, sacrificialSite, null, null); marketsquare.setExit("north", tavern);
templePyramid.setExits(hut, null, null, marketsquare, wizardsRoom, basement); marketsquare.setExit("east", templePyramid);
tavern.setExits(null, hut, marketsquare, null, null, null); marketsquare.setExit("west", sacrificialSite);
sacrificialSite.setExits(null, marketsquare, null , null, null, cave);
hut.setExits(null, jungle, templePyramid, tavern, null, null); templePyramid.setExit("north", hut);
jungle.setExits(null, null, null, hut, null, null); templePyramid.setExit("up", wizardsRoom);
secretPassage.setExits(null, basement, null, cave, null, null); templePyramid.setExit("down", basement);
cave.setExits(null, secretPassage, beach, null, sacrificialSite, null); templePyramid.setExit("west", marketsquare);
beach.setExits(cave, null, null, null, null, null);
basement.setExits(null, null, null, secretPassage, templePyramid, null); tavern.setExit("east", hut);
wizardsRoom.setExits(null, null, null, null, null, templePyramid); tavern.setExit("south", marketsquare);
sacrificialSite.setExit("east", marketsquare);
sacrificialSite.setExit("down", cave);
hut.setExit("east", jungle);
hut.setExit("south", templePyramid);
hut.setExit("west", tavern);
jungle.setExit("west", hut);
secretPassage.setExit("east", basement);
secretPassage.setExit("west", cave);
cave.setExit("east", secretPassage);
cave.setExit("south", beach);
cave.setExit("up", sacrificialSite);
beach.setExit("north", cave);
basement.setExit("west", secretPassage);
basement.setExit("up", templePyramid);
wizardsRoom.setExit("down", templePyramid);
currentRoom = marketsquare; // start game on marketsquare currentRoom = marketsquare; // start game on marketsquare
} }

View File

@@ -1,5 +1,7 @@
package de.szut.zuul; package de.szut.zuul;
import java.util.HashMap;
/** /**
* Class Room - a room in an adventure game. * Class Room - a room in an adventure game.
* *
@@ -17,12 +19,7 @@ package de.szut.zuul;
public class Room public class Room
{ {
private String description; private String description;
private Room northExit; private HashMap<String, Room> roomExits;
private Room southExit;
private Room eastExit;
private Room westExit;
private Room upExit;
private Room downExit;
/** /**
* Create a room described "description". Initially, it has * Create a room described "description". Initially, it has
@@ -33,38 +30,15 @@ public class Room
public Room(String description) public Room(String description)
{ {
this.description = description; this.description = description;
this.roomExits = new HashMap<>();
} }
/** /**
* Define the exits of this room. Every direction either leads * Define the exits of this room. Every direction either leads
* to another room or is null (no exit there). * to another room or is null (no exit there).
* @param north The north exit.
* @param east The east east.
* @param south The south exit.
* @param west The west exit.
* @param up The up exit.
* @param down The down exit.
*/ */
public void setExits(Room north, Room east, Room south, Room west, Room up, Room down) public void setExit(String direction, Room neighbor) {
{ roomExits.put(direction, neighbor);
if(north != null) {
northExit = north;
}
if(east != null) {
eastExit = east;
}
if(south != null) {
southExit = south;
}
if(west != null) {
westExit = west;
}
if(up != null) {
upExit = up;
}
if(down != null) {
downExit = down;
}
} }
/** /**
@@ -77,36 +51,13 @@ public class Room
public String exitsToString() { public String exitsToString() {
StringBuilder exitsInString = new StringBuilder(); StringBuilder exitsInString = new StringBuilder();
if (northExit != null) { for (String direction : roomExits.keySet()) {
exitsInString.append("north "); exitsInString.append(direction).append(" ");
}
if (eastExit != null) {
exitsInString.append("east ");
}
if (southExit != null) {
exitsInString.append("south ");
}
if (westExit != null) {
exitsInString.append("west ");
}
if (upExit != null) {
exitsInString.append("up ");
}
if (downExit != null) {
exitsInString.append("down ");
} }
return exitsInString.toString().trim(); return exitsInString.toString().trim();
} }
public Room getExits(String direction) { public Room getExits(String direction) {
return switch (direction) { return roomExits.get(direction);
case "north" -> northExit;
case "east" -> eastExit;
case "south" -> southExit;
case "west" -> westExit;
case "up" -> upExit;
case "down" -> downExit;
default -> null;
};
} }
} }