transfer - locked rooms

This commit is contained in:
jslightham
2019-05-26 23:22:51 -04:00
parent d597a1489b
commit 5a5f4fc06a
4 changed files with 46 additions and 21 deletions

View File

@@ -52,3 +52,6 @@ novel, item
mop, item
bucket, item
towels, item
open, verb
i, verb
unlock, verb

View File

@@ -23,7 +23,13 @@ public class Player extends Entity{
}
public void removeFromInventory(Item item){
inventory.remove(item);
for(int i =0; i<inventory.size(); i++) {
if(item.equals(inventory.get(i))) {
inventory.remove(i);
return;
}
}
}

View File

@@ -54,8 +54,8 @@ class Game {
// Read the Description
String roomDescription = roomScanner.nextLine();
room.setDescription(roomDescription.split(":")[1].replaceAll("<br>", "\n").trim());
// Read the Description
boolean locked = Boolean.parseBoolean(roomScanner.nextLine());
// Read the locked state
boolean locked = Boolean.parseBoolean(roomScanner.nextLine().split(":")[1].replaceAll("<br>", "\n").trim());
room.setLocked(locked);
// Read the Items
String items = roomScanner.nextLine();
@@ -168,6 +168,30 @@ class Game {
}
String commandWord = command.getCommandWord();
switch(commandWord) {
case "open": case "unlock":
boolean hasLockPick = false;
for(int i =0; i<player.getInventory().size(); i++) {
if(player.getInventory().get(i).equals(new Lockpick())) {
hasLockPick = true;
break;
}
}
if(command.hasDirection() && hasLockPick) {
Room nextRoom = currentRoom.nextRoom(command.getDirection());
if(nextRoom.getLocked()) {
nextRoom.setLocked(false);
player.removeFromInventory(new Lockpick());
System.out.println("With great effort, you unlocked the door!");
}else{
System.out.println("That door is already unlocked!");
}
}else if(!command.hasDirection()){
System.out.println("You must specify a direction!");
}else {
System.out.println("You need a lockpick!");
}
break;
case "go": case "n": case "s": case "e": case "w": case "north": case "south": case "west": case "east": case "up": case "down": case "d": case "u":
goRoom(command);
break;
@@ -244,20 +268,6 @@ class Game {
System.out.println("You have nothing on you. Try and find some items.");
}
break;
case "open":
boolean hasLockpick = false;
for(int i =0; i<player.getInventory().size(); i++) {
if(player.getInventory().get(i).equals(new Lockpick())) {
hasLockpick = true;
player.removeFromInventory(new Lockpick());
break;
}
}
if(command.hasDirection()) {
Room nextRoom = currentRoom.nextRoom(command.getDirection());
nextRoom.setLocked(false);
}
break;
default:
return false;
}

View File

@@ -35,6 +35,7 @@ class Parser {
String verb = "";
String direction = "";
String item = "";
boolean open = false;
//String word2;
ArrayList<String> words = new ArrayList<String>();
ArrayList<String> otherWords = new ArrayList<String>();
@@ -50,6 +51,9 @@ class Parser {
words.add(tokenizer.nextToken());
}
for(int i=0; i<words.size(); i++) {
if(words.get(i).equals("open") || words.get(i).equals("unlock")) {
open = true;
}
if(CommandWords.isCommand(words.get(i))) {
verb = words.get(i);
if(CommandWords.isDirection(words.get(i))) {
@@ -57,14 +61,16 @@ class Parser {
}
}else if(CommandWords.isItem(words.get(i))){
item = words.get(i);
}
else {
}else{
otherWords.add(words.get(i));
}
}
//System.out.println(verb);
if (CommandWords.isCommand(verb))
if(!open)
return new Command(verb, otherWords, direction, item);
else
return new Command("open", otherWords, direction, item);
else
return new Command(null, otherWords, direction, item);
}