This commit is contained in:
2025-02-18 14:21:39 +01:00
parent a7344d4543
commit 8bb33ccf2b
3 changed files with 69 additions and 2 deletions

View File

@@ -51,6 +51,17 @@ public class Game
basement = new Room("in a basement");
wizardsRoom = new Room("in a wizards room");
// add items to rooms
marketsquare.putItem(new Item("Bogen", "ein Bogen aus Holz", 0.5));
cave.putItem(new Item("Schatz", "eine kleine Schatztruhe mit Münzen", 7.5));
wizardsRoom.putItem(new Item("Pfeile", "ein Köcher mit diversen Pfeilen", 1));
jungle.putItem(new Item("Pflanze", "eine Heilpflanze", 0.5));
jungle.putItem(new Item("Kakao", "ein kleiner Kakaobaum", 5));
sacrificialSite.putItem(new Item("Messer", "ein sehr scharfes, großes Messer", 1));
hut.putItem(new Item("Speer", "ein Speer mit dazugehöriger Schleuder", 5.0));
tavern.putItem(new Item("Nahrung", "ein Teller mit deftigem Fleisch und Maisbrei", 0.5));
basement.putItem(new Item("Schmuck", "ein sehr hübscher Kopfschmuck", 1));
// initialise room exits
marketsquare.setExit("north", tavern);
marketsquare.setExit("east", templePyramid);

View File

@@ -0,0 +1,28 @@
package de.szut.zuul;
import java.util.HashMap;
public class Item {
private String name;
private String description;
private double weight;
public Item(String name, String description, double weight) {
this.name = name;
this.description = description;
this.weight = weight;
}
@Override
public String toString() {
return "Item{name='" + name + "', description='" + description + "', weight=" + weight + "}";
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public double getWeight() {
return weight;
}
}

View File

@@ -20,7 +20,7 @@ public class Room
{
private String description;
private HashMap<String, Room> roomExits;
private HashMap<String, Item> items;
/**
* Create a room described "description". Initially, it has
* no exits. "description" is something like "a kitchen" or
@@ -31,6 +31,7 @@ public class Room
{
this.description = description;
this.roomExits = new HashMap<>();
this.items = new HashMap<>();
}
/**
@@ -49,7 +50,7 @@ public class Room
return description;
}
public String getLongDescription() {
return "You are " + getDescription() + "\nExits: " + exitsToString();
return "You are " + getDescription() + "\nExits: " + exitsToString() + "\nItems in this room:\n" + getItemDescription();
}
public String exitsToString() {
StringBuilder exitsInString = new StringBuilder();
@@ -63,4 +64,31 @@ public class Room
public Room getExits(String direction) {
return roomExits.get(direction);
}
public void putItem(Item newItem) {
items.put(newItem.getName(), newItem);
}
public Item removeItem(String itemName) {
return items.remove(itemName);
}
private String getItemDescription() {
if (items.isEmpty()) {
return "No items in this room.";
}
StringBuilder itemDescription = new StringBuilder();
for (Item item : items.values()) {
itemDescription.append("- ")
.append(item.getName()).append(", ")
.append(item.getDescription()).append(", ")
.append(item.getWeight()).append("kg\n");
}
return itemDescription.toString().trim();
}
public String itemsToString() {
StringBuilder itemsInString = new StringBuilder();
for (String itemName : items.keySet()) {
itemsInString.append(itemName).append(" ");
}
return itemsInString.toString().trim();
}
}