zuul7
This commit is contained in:
@@ -15,7 +15,7 @@ public class CommandWords
|
|||||||
{
|
{
|
||||||
// a constant array that holds all valid command words
|
// a constant array that holds all valid command words
|
||||||
private static final String[] validCommands = {
|
private static final String[] validCommands = {
|
||||||
"go", "quit", "help", "look"
|
"go", "quit", "help", "look", "status", "take", "drop"
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ package de.szut.zuul;
|
|||||||
public class Game
|
public class Game
|
||||||
{
|
{
|
||||||
private Parser parser;
|
private Parser parser;
|
||||||
private Room currentRoom;
|
private Player player;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the game and initialise its internal map.
|
* Create the game and initialise its internal map.
|
||||||
@@ -98,7 +98,7 @@ public class Game
|
|||||||
|
|
||||||
wizardsRoom.setExit("down", templePyramid);
|
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("World of Zuul is a new, incredibly boring adventure game.");
|
||||||
System.out.println("Type 'help' if you need help.");
|
System.out.println("Type 'help' if you need help.");
|
||||||
System.out.println();
|
System.out.println();
|
||||||
printRoomInformation(currentRoom);
|
printRoomInformation();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -157,7 +157,16 @@ public class Game
|
|||||||
wantToQuit = quit(command);
|
wantToQuit = quit(command);
|
||||||
}
|
}
|
||||||
else if (commandWord.equals("look")) {
|
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;
|
return wantToQuit;
|
||||||
@@ -196,14 +205,14 @@ public class Game
|
|||||||
// Try to leave current room.
|
// Try to leave current room.
|
||||||
Room nextRoom = null;
|
Room nextRoom = null;
|
||||||
|
|
||||||
nextRoom = currentRoom.getExits(direction);
|
nextRoom = player.getCurrentRoom().getExits(direction);
|
||||||
|
|
||||||
if (nextRoom == null) {
|
if (nextRoom == null) {
|
||||||
System.out.println("There is no door!");
|
System.out.println("There is no door!");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
currentRoom = nextRoom;
|
player.goTo(nextRoom);
|
||||||
printRoomInformation(currentRoom);
|
printRoomInformation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,13 +231,56 @@ public class Game
|
|||||||
return true; // signal that we want to quit
|
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();
|
System.out.println();
|
||||||
}
|
}
|
||||||
private void look(Room currentRoom)
|
private void look(Room currentRoom)
|
||||||
{
|
{
|
||||||
System.out.println(currentRoom.getLongDescription());
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
68
src/de/szut/zuul/Player.java
Normal file
68
src/de/szut/zuul/Player.java
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package de.szut.zuul;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class Player {
|
||||||
|
private Room currentRoom;
|
||||||
|
private double loadCapacity;
|
||||||
|
private LinkedList<Item> items;
|
||||||
|
|
||||||
|
public Player(Room startingRoom, double loadCapacity, LinkedList<Item> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -69,8 +69,11 @@ public class Room
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Item removeItem(String itemName) {
|
public Item removeItem(String itemName) {
|
||||||
|
if (items.containsKey(itemName)) {
|
||||||
return items.remove(itemName);
|
return items.remove(itemName);
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
private String getItemDescription() {
|
private String getItemDescription() {
|
||||||
if (items.isEmpty()) {
|
if (items.isEmpty()) {
|
||||||
return "No items in this room.";
|
return "No items in this room.";
|
||||||
|
|||||||
Reference in New Issue
Block a user