From dc934844572d882386635e9e65ff6f42f3b1f142 Mon Sep 17 00:00:00 2001 From: Wim Wenigerkind Date: Tue, 18 Feb 2025 13:02:38 +0100 Subject: [PATCH] zuul2 --- src/de/szut/zuul/Game.java | 39 +++---------------------------- src/de/szut/zuul/Room.java | 48 ++++++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/de/szut/zuul/Game.java b/src/de/szut/zuul/Game.java index 6414933..713827f 100644 --- a/src/de/szut/zuul/Game.java +++ b/src/de/szut/zuul/Game.java @@ -158,24 +158,8 @@ public class Game // Try to leave current room. Room nextRoom = null; - if(direction.equals("north")) { - nextRoom = currentRoom.northExit; - } - if(direction.equals("east")) { - nextRoom = currentRoom.eastExit; - } - if(direction.equals("south")) { - nextRoom = currentRoom.southExit; - } - if(direction.equals("west")) { - nextRoom = currentRoom.westExit; - } - if(direction.equals("up")) { - nextRoom = currentRoom.upExit; - } - if(direction.equals("down")) { - nextRoom = currentRoom.downExit; - } + + nextRoom = currentRoom.getExits(direction); if (nextRoom == null) { System.out.println("There is no door!"); @@ -205,24 +189,7 @@ public class Game { System.out.println("You are " + currentRoom.getDescription()); System.out.print("Exits: "); - if(currentRoom.northExit != null) { - System.out.print("north "); - } - if(currentRoom.eastExit != null) { - System.out.print("east "); - } - if(currentRoom.southExit != null) { - System.out.print("south "); - } - if(currentRoom.westExit != null) { - System.out.print("west "); - } - if(currentRoom.upExit != null) { - System.out.print("up "); - } - if(currentRoom.downExit != null) { - System.out.print("down "); - } + System.out.print(currentRoom.exitsToString()); System.out.println(); } } diff --git a/src/de/szut/zuul/Room.java b/src/de/szut/zuul/Room.java index 579bee1..53d2a59 100644 --- a/src/de/szut/zuul/Room.java +++ b/src/de/szut/zuul/Room.java @@ -16,13 +16,13 @@ package de.szut.zuul; */ public class Room { - public String description; - public Room northExit; - public Room southExit; - public Room eastExit; - public Room westExit; - public Room upExit; - public Room downExit; + private String description; + private Room northExit; + private Room southExit; + private Room eastExit; + private Room westExit; + private Room upExit; + private Room downExit; /** * Create a room described "description". Initially, it has @@ -74,5 +74,39 @@ public class Room { return description; } + 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 "); + } + + 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; + }; + } }