From 5fd8ac7a92c89c31d836e0fe1650e723c0684425 Mon Sep 17 00:00:00 2001 From: Wim Wenigerkind Date: Thu, 20 Feb 2025 13:00:46 +0100 Subject: [PATCH] zuul7 --- src/de/szut/zuul/CommandWords.java | 2 +- src/de/szut/zuul/Game.java | 70 ++++++++++++++++++++++++++---- src/de/szut/zuul/Player.java | 68 +++++++++++++++++++++++++++++ src/de/szut/zuul/Room.java | 5 ++- 4 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 src/de/szut/zuul/Player.java diff --git a/src/de/szut/zuul/CommandWords.java b/src/de/szut/zuul/CommandWords.java index 628849d..ca71bec 100644 --- a/src/de/szut/zuul/CommandWords.java +++ b/src/de/szut/zuul/CommandWords.java @@ -15,7 +15,7 @@ public class CommandWords { // a constant array that holds all valid command words private static final String[] validCommands = { - "go", "quit", "help", "look" + "go", "quit", "help", "look", "status", "take", "drop" }; /** diff --git a/src/de/szut/zuul/Game.java b/src/de/szut/zuul/Game.java index 00f39e2..ce6eece 100644 --- a/src/de/szut/zuul/Game.java +++ b/src/de/szut/zuul/Game.java @@ -20,7 +20,7 @@ package de.szut.zuul; public class Game { private Parser parser; - private Room currentRoom; + private Player player; /** * Create the game and initialise its internal map. @@ -98,7 +98,7 @@ public class Game wizardsRoom.setExit("down", templePyramid); - currentRoom = marketsquare; // start game on marketsquare + player = new Player(marketsquare, 10, null); } /** @@ -129,7 +129,7 @@ public class Game System.out.println("World of Zuul is a new, incredibly boring adventure game."); System.out.println("Type 'help' if you need help."); System.out.println(); - printRoomInformation(currentRoom); + printRoomInformation(); } /** @@ -157,7 +157,16 @@ public class Game wantToQuit = quit(command); } else if (commandWord.equals("look")) { - look(currentRoom); + look(player.getCurrentRoom()); + } + else if (commandWord.equals("status")) { + showStatus(); + } + else if (commandWord.equals("take")) { + takeItem(command); + } + else if (commandWord.equals("drop")) { + dropItem(command); } return wantToQuit; @@ -196,14 +205,14 @@ public class Game // Try to leave current room. Room nextRoom = null; - nextRoom = currentRoom.getExits(direction); + nextRoom = player.getCurrentRoom().getExits(direction); if (nextRoom == null) { System.out.println("There is no door!"); } else { - currentRoom = nextRoom; - printRoomInformation(currentRoom); + player.goTo(nextRoom); + printRoomInformation(); } } @@ -222,13 +231,56 @@ public class Game return true; // signal that we want to quit } } - private void printRoomInformation(Room currentRoom) + private void printRoomInformation() { - System.out.print(currentRoom.getLongDescription()); + System.out.print(player.getCurrentRoom().getLongDescription()); System.out.println(); } private void look(Room currentRoom) { System.out.println(currentRoom.getLongDescription()); } + private void showStatus() + { + System.out.println(player.showStatus()); + printRoomInformation(); + } + private void takeItem(Command command) + { + if (!command.hasSecondWord()) { + System.out.println("Take what?"); + return; + } + + String itemName = command.getSecondWord(); + Item item = player.getCurrentRoom().removeItem(itemName); + if (item != null) { + if (player.takeItem(item)) { + System.out.println("You took the item: " + item.getName()); + } else { + System.out.println("You can't take this item. It's too heavy."); + player.getCurrentRoom().putItem(item); + } + } else { + System.out.println("There is no such item in this room."); + } + showStatus(); + } + private void dropItem(Command command) + { + if (!command.hasSecondWord()) { + System.out.println("Drop what?"); + return; + } + + String itemName = command.getSecondWord(); + Item item = player.dropItem(itemName); + if (item != null) { + player.getCurrentRoom().putItem(item); + System.out.println("You dropped the item: " + item.getName()); + } else { + System.out.println("You don't have this item."); + } + showStatus(); + } } diff --git a/src/de/szut/zuul/Player.java b/src/de/szut/zuul/Player.java new file mode 100644 index 0000000..7f2df85 --- /dev/null +++ b/src/de/szut/zuul/Player.java @@ -0,0 +1,68 @@ +package de.szut.zuul; + +import java.util.LinkedList; + +public class Player { + private Room currentRoom; + private double loadCapacity; + private LinkedList items; + + public Player(Room startingRoom, double loadCapacity, LinkedList items) { + this.currentRoom = startingRoom; + this.loadCapacity = loadCapacity; + this.items = (items != null) ? items : new LinkedList<>(); + } + + public Room getCurrentRoom() { + return currentRoom; + } + + public void goTo(Room newRoom) { + this.currentRoom = newRoom; + } + + public boolean takeItem(Item item) { + if (isTakePossible(item)) { + items.add(item); + return true; + } + return false; + } + + public Item dropItem(String itemName) { + for (Item item : items) { + if (item.getName().equals(itemName)) { + items.remove(item); + return item; + } + } + return null; + } + + private double calculateWeight() { + double load = 0; + for (Item item : items) { + load += item.getWeight(); + } + return load; + } + + private boolean isTakePossible(Item item) { + return item.getWeight() + calculateWeight() <= loadCapacity; + } + + public String showStatus() { + return "Status of the player:\n" + + "Load capacity: " + loadCapacity + " kg\n" + + "Taken items: " + itemsToString() + "\n" + + "Absorbed weight: " + calculateWeight() + " kg"; + } + + private String itemsToString() { + StringBuilder itemsInString = new StringBuilder(); + for (Item item : items) { + itemsInString.append(item.getName()).append(" ").append(item.getWeight()).append(" "); + } + return itemsInString.toString().trim(); + } +} \ No newline at end of file diff --git a/src/de/szut/zuul/Room.java b/src/de/szut/zuul/Room.java index af475cb..f19e0f0 100644 --- a/src/de/szut/zuul/Room.java +++ b/src/de/szut/zuul/Room.java @@ -69,7 +69,10 @@ public class Room } public Item removeItem(String itemName) { - return items.remove(itemName); + if (items.containsKey(itemName)) { + return items.remove(itemName); + } + return null; } private String getItemDescription() { if (items.isEmpty()) {