|
| 1 | +import java.util.Scanner; |
| 2 | + |
| 3 | +public class TextAdventure { |
| 4 | + |
| 5 | + private static final Scanner scanner = new Scanner(System.in); |
| 6 | + |
| 7 | + private static Room currentRoom; |
| 8 | + |
| 9 | + public static void main(String[] args) { |
| 10 | + currentRoom = new Room("Starting room"); |
| 11 | + |
| 12 | + gameLoop(); |
| 13 | + } |
| 14 | + |
| 15 | + private static void gameLoop() { |
| 16 | + while (true) { |
| 17 | + System.out.println("You are in " + currentRoom.getName()); |
| 18 | + System.out.println("What do you want to do?"); |
| 19 | + |
| 20 | + String command = scanner.nextLine(); |
| 21 | + |
| 22 | + switch (command) { |
| 23 | + case "go north": |
| 24 | + currentRoom = currentRoom.getNorthRoom(); |
| 25 | + break; |
| 26 | + case "go south": |
| 27 | + currentRoom = currentRoom.getSouthRoom(); |
| 28 | + break; |
| 29 | + case "go east": |
| 30 | + currentRoom = currentRoom.getEastRoom(); |
| 31 | + break; |
| 32 | + case "go west": |
| 33 | + currentRoom = currentRoom.getWestRoom(); |
| 34 | + break; |
| 35 | + case "examine": |
| 36 | + System.out.println(currentRoom.getDescription()); |
| 37 | + break; |
| 38 | + default: |
| 39 | + System.out.println("I don't understand that command."); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private static class Room { |
| 45 | + |
| 46 | + private final String name; |
| 47 | + private final String description; |
| 48 | + private final Room northRoom; |
| 49 | + private final Room southRoom; |
| 50 | + private final Room eastRoom; |
| 51 | + private final Room westRoom; |
| 52 | + |
| 53 | + public Room(String name) { |
| 54 | + this.name = name; |
| 55 | + this.description = ""; |
| 56 | + this.northRoom = null; |
| 57 | + this.southRoom = null; |
| 58 | + this.eastRoom = null; |
| 59 | + this.westRoom = null; |
| 60 | + } |
| 61 | + |
| 62 | + public String getName() { |
| 63 | + return name; |
| 64 | + } |
| 65 | + |
| 66 | + public String getDescription() { |
| 67 | + return description; |
| 68 | + } |
| 69 | + |
| 70 | + public Room getNorthRoom() { |
| 71 | + return northRoom; |
| 72 | + } |
| 73 | + |
| 74 | + public Room getSouthRoom() { |
| 75 | + return southRoom; |
| 76 | + } |
| 77 | + |
| 78 | + public Room getEastRoom() { |
| 79 | + return eastRoom; |
| 80 | + } |
| 81 | + |
| 82 | + public Room getWestRoom() { |
| 83 | + return westRoom; |
| 84 | + } |
| 85 | + |
| 86 | + public void setNorthRoom(Room northRoom) { |
| 87 | + this.northRoom = northRoom; |
| 88 | + } |
| 89 | + |
| 90 | + public void setSouthRoom(Room southRoom) { |
| 91 | + this.southRoom = southRoom; |
| 92 | + } |
| 93 | + |
| 94 | + public void setEastRoom(Room eastRoom) { |
| 95 | + this.eastRoom = eastRoom; |
| 96 | + } |
| 97 | + |
| 98 | + public void setWestRoom(Room westRoom) { |
| 99 | + this.westRoom = westRoom; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments