From 2368d3b492e41da1598a7d26ecb1f8cfa9ad369d Mon Sep 17 00:00:00 2001 From: Wim Wenigerkind Date: Tue, 18 Feb 2025 13:23:39 +0100 Subject: [PATCH] zuul3 --- src/de/szut/zuul/Game.java | 45 ++++++++++++++++++------- src/de/szut/zuul/Room.java | 67 +++++--------------------------------- 2 files changed, 43 insertions(+), 69 deletions(-) diff --git a/src/de/szut/zuul/Game.java b/src/de/szut/zuul/Game.java index 713827f..0d94783 100644 --- a/src/de/szut/zuul/Game.java +++ b/src/de/szut/zuul/Game.java @@ -52,17 +52,40 @@ public class Game wizardsRoom = new Room("in a wizards room"); // initialise room exits - marketsquare.setExits(tavern, templePyramid, null, sacrificialSite, null, null); - templePyramid.setExits(hut, null, null, marketsquare, wizardsRoom, basement); - tavern.setExits(null, hut, marketsquare, null, null, null); - sacrificialSite.setExits(null, marketsquare, null , null, null, cave); - hut.setExits(null, jungle, templePyramid, tavern, null, null); - jungle.setExits(null, null, null, hut, null, null); - secretPassage.setExits(null, basement, null, cave, null, null); - cave.setExits(null, secretPassage, beach, null, sacrificialSite, null); - beach.setExits(cave, null, null, null, null, null); - basement.setExits(null, null, null, secretPassage, templePyramid, null); - wizardsRoom.setExits(null, null, null, null, null, templePyramid); + marketsquare.setExit("north", tavern); + marketsquare.setExit("east", templePyramid); + marketsquare.setExit("west", sacrificialSite); + + templePyramid.setExit("north", hut); + templePyramid.setExit("up", wizardsRoom); + templePyramid.setExit("down", basement); + templePyramid.setExit("west", marketsquare); + + tavern.setExit("east", hut); + 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 } diff --git a/src/de/szut/zuul/Room.java b/src/de/szut/zuul/Room.java index 53d2a59..84d5c22 100644 --- a/src/de/szut/zuul/Room.java +++ b/src/de/szut/zuul/Room.java @@ -1,5 +1,7 @@ package de.szut.zuul; +import java.util.HashMap; + /** * Class Room - a room in an adventure game. * @@ -17,12 +19,7 @@ package de.szut.zuul; public class Room { private String description; - private Room northExit; - private Room southExit; - private Room eastExit; - private Room westExit; - private Room upExit; - private Room downExit; + private HashMap roomExits; /** * Create a room described "description". Initially, it has @@ -33,38 +30,15 @@ public class Room public Room(String description) { this.description = description; + this.roomExits = new HashMap<>(); } /** * Define the exits of this room. Every direction either leads * 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) - { - 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; - } + public void setExit(String direction, Room neighbor) { + roomExits.put(direction, neighbor); } /** @@ -77,36 +51,13 @@ public class Room public String exitsToString() { StringBuilder exitsInString = new StringBuilder(); - if (northExit != null) { - exitsInString.append("north "); - } - 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 "); + for (String direction : roomExits.keySet()) { + exitsInString.append(direction).append(" "); } return exitsInString.toString().trim(); } public Room getExits(String direction) { - return switch (direction) { - case "north" -> northExit; - case "east" -> eastExit; - case "south" -> southExit; - case "west" -> westExit; - case "up" -> upExit; - case "down" -> downExit; - default -> null; - }; + return roomExits.get(direction); } }