zuul7
This commit is contained in:
@@ -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"
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
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,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()) {
|
||||
|
||||
Reference in New Issue
Block a user