Java 从列表中删除、添加和使用项目
我正在尝试为家庭作业创建一个文本游戏,在执行take和drop命令时遇到问题Java 从列表中删除、添加和使用项目,java,arrays,Java,Arrays,我正在尝试为家庭作业创建一个文本游戏,在执行take和drop命令时遇到问题 如何创建锁定的门并使用物品(如钥匙)打开它们 请帮忙 有两门课——游戏课和房间课;这就是我到目前为止所做的: 游戏 package text_game; import java.util.List; import java.util.ArrayList; import java.util.Scanner; public class Game { private Room yard; private Room
- 如何创建锁定的门并使用物品(如钥匙)打开它们
package text_game;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
public class Game {
private Room yard;
private Room frontdoor;
private Room hallway;
private Room bathroom;
private Room lounge;
private Room kitchen;
private Room bedroom;
private Room basementdoor;
private Room basement;
private Room exit;
private Room current;
//create an inventory
private ArrayList<String> inventory = new ArrayList<String>();
public Game() {
yard = new Room("You're standing in open area.\n"
+ "There is a house to the north.\n");
frontdoor = new Room("You come to an old timber door, but it's locked.\n"
+ "There's a key on the ground. Talk about security...\n"
+ "There's a yard to the south.\n");
frontdoor.getItems().add("Front Door Key");
hallway = new Room("A hall stretches out into darkness.\n"
+ "There is a small flicker of light to the North.\n"
+ "There's a faint dripping sound to the west"
+ "There's a strange smell comming from the east");
bathroom = new Room("You enter what looks like a bathroom. It doesn't look like anyones cleaned it it months.\n"
+ "There's four numbers scratched into the wall - '1517'.\n"
+ "The hall is to the east.");
kitchen = new Room("You come into a kitchen, there's moldy food still in grocery bags on the bench.\n"
+ "You see a sandwhich and pick it up to investigate. Fortunately it looks fresh, and you're pretty hungry.\n"
+ "The hall is back to the west.");
kitchen.getItems().add("Mouldy Sandwhich?");
lounge = new Room("You come to a dimly lit loungeroom.\n"
+ "The couches have been toppled over, a book case lies fallen on the floor. The embers are still hot and flickering in the fireplace.\n"
+ "There's a door on the western wall.");
bedroom = new Room("You walk into a bedroom.\n"
+ "There's snoring comming from a huddled lump on the bed. Better be quiet.\n"
+ "There's a large metal door on the southern wall");
basementdoor = new Room("There's no breaking through this thing, it's too solid.\n"
+ "There's a small keypad next to the door. Hmm?\n");
basement = new Room("The code unlocks the door. You climb down a dimly lit staircase into what looks like a basement.\n"
+ "Suddenly, you hear some yelling from upstairs and some footsteps making their way down the staircase!\n"
+ "There's a window to north, quick!\n");
exit = new Room("You crawl through the window, out into the open and run down the street.\n"
+ "Why did you even go into that house in first place?\n");
current = yard;
//Connect the rooms
yard.connectNorth(frontdoor);
frontdoor.connectNorth(hallway);
hallway.connectNorth(lounge);
hallway.connectWest(bathroom);
kitchen.connectWest(hallway);
lounge.connectWest(bedroom);
basementdoor.connectNorth(bedroom);
basement.connectNorth(basementdoor);
basement.connectNorth(exit);
}
/**
* Play the interactive text game.
*
* Reads commands from the given input, line by line.
*
* @param in
*/
private void take(String item) {
if (current.getItems().contains(item)== true) {
Room.al.items().remove(item); // can't get this to work
inventory.add(item);
System.out.println("You picked up the " + item + "and stashed it away");
} else {
System.out.println("Unknown object '" + item + "'.");
}
}
private void drop(String item) {
if (current.getItems().contains(item) == true) {
inventory.remove(item);
Room.al.items().add(item); // or this
System.out.println("You dropped the " + item + "and left it in the " + current);
} else {
System.out.println("Unknown object '" + item + "'.");
}
}
public void play(Scanner in) {
current.describe();
while (current != exit) {
String line = in.nextLine().toLowerCase();
if (line.equals("quit")) {
System.out.println("You gave up!");
break;
} else if (line.startsWith("go ")) {
current = current.move(line.substring(3));
current.describe();
} else if (line.startsWith("take ")) {
take(line.substring(5));
} else if (line.startsWith("print inventory")) {
System.out.print(inventory);
} else {
System.out.println("Unknown command '" + line + "'. Try go/take/quit.");
}
}
}
/** Starts the whole game. */
public static void main(String[] args) {
Game game = new Game();
game.play(new Scanner(System.in));
}
}
package text\u游戏;
导入java.util.List;
导入java.util.ArrayList;
导入java.util.Scanner;
公开课游戏{
私房庭院;
私人房间前门;
私人房间走廊;
私人浴室;
私人休息室;
私人厨房;
包间卧室;
私人房间地下室门;
私人房间地下室;
私人房间出口;
私人房间电流;
//创建一个清单
私有ArrayList清单=新ArrayList();
公共游戏(){
院子=新房间(“您正站在空地上。\n”
+“北面有一所房子。\n”);
前门=新房间(“你来到一扇旧木门前,但它是锁着的。\n”
+“地上有一把钥匙。谈谈安全问题…\n”
+“南面有一个院子。\n”);
frontdoor.getItems().add(“前门钥匙”);
走廊=新房间(“大厅延伸到黑暗中。\n”
+“北面有微弱的灯光闪烁。\n”
+“西边有一种微弱的滴水声”
+“从东方传来一股奇怪的气味”);
浴室=新房间(“你进入一个看起来像浴室的房间。看起来没有人打扫过它几个月。\n”
+“墙上有四个数字——‘1517’。\n”
+“大厅在东边。”);
厨房=新房间(“你走进厨房,长凳上的食品袋里还有发霉的食物。\n”
+您看到一只沙子,就把它捡起来调查。幸运的是,它看起来很新鲜,您很饿。\n
+“大厅在西面。”);
kitchen.getItems().add(“发霉的沙子是什么?”);
lounge=新房间(“您来到灯光昏暗的休息室。\n”
+沙发被掀翻了,一个书柜倒在地上。壁炉里的余烬仍然很热,还在摇曳。\n
+“西墙上有一扇门。”);
卧室=新房间(“你走进卧室。\n”
+“床上蜷缩着的一个肿块在打鼾。最好安静。\n”
+“南墙上有一扇大金属门”);
basementdoor=新房间(“这东西太坚固了,无法突破。\n”
+“门旁边有一个小键盘。嗯?\n”);
地下室=新房间(“代码打开了门。你爬下灯光昏暗的楼梯进入看起来像地下室的地方。\n”
+“突然,你听到楼上有人喊叫,有人走下楼梯!\n”
+“北边有一扇窗户,快点!\n”);
exit=新房间(“你爬过窗户,爬到户外,沿街跑。\n”
+“你当初为什么要进那所房子?\n”);
电流=码;
//连接房间
车场北(前门);
前门。连接北面(走廊);
走廊,北面(休息室);
走廊,西面(浴室);
厨房。西(走廊);
休息室.西区(卧室);
地下室门。连接北面(卧室);
地下室。连接北面(地下室门);
地下室。北面(出口);
}
/**
*玩互动文字游戏。
*
*逐行读取给定输入中的命令。
*
*@param-in
*/
私有无效获取(字符串项){
if(current.getItems().contains(item)==true){
Room.al.items().remove(item);//无法使其工作
存货.增加(项);
System.out.println(“您捡起了“+项+”并将其隐藏起来”);
}否则{
System.out.println(“未知对象“+”项“+”);
}
}
专用作废删除(字符串项){
if(current.getItems().contains(item)==true){
库存。移除(项目);
Room.al.items().add(item);//或此
System.out.println(“您删除了“+项+”并将其保留在“+当前项”中);
}否则{
System.out.println(“未知对象“+”项“+”);
}
}
公共无效播放(扫描仪输入){
current.descripe();
while(当前!=退出){
String line=in.nextLine().toLowerCase();
如果(行等于(“退出”)){
System.out.println(“你放弃了!”);
打破
}else if(第行开始使用(“go”)){
当前=当前移动(行子字符串(3));
current.descripe();
}else if(line.startsWith(“take”)){
取(行、子串(5));
}else if(第行startsWith(“打印库存”)){
系统输出打印(库存);
}否则{
System.out.println(“未知命令“+”行“+”。尝试go/take/quit。”);
}
}
}
/**开始整个游戏*/
公共静态void main(字符串[]args){
游戏=新游戏();
游戏(新扫描器(System.in));
}
}
房间
package text\u游戏;
导入java.util.List;
导入java.util.ArrayList;
/**
*
*@作者
*
*/
公共教室{
私有字符串描述;
北包房;
包房东;
南部包房;
包房西;
//将空项目列表添加到每个房间
private ArrayList items=new ArrayList();
私有字符串密钥;//null表示没有密钥
公共房间(字符串描述){
描述=描述;
}
/**
*将此房间的西出口连接到“其他”房间。
*它会自动在两个方向建立连接。
*
*@param其他
*/
西公共空间(其他房间){
this.west=其他;
other.east=这个;
}
北面公共空间(其他房间){
this.north=其他;
other.south=这个;
}
/**
*打印此房间及其内容的描述。
*/
公共空间描述(){
最终StringBuilder itemsString=新StringBuilder(“”);
用于(字符串项:项){
项目字符串
package text_game;
import java.util.List;
import java.util.ArrayList;
/**
*
* @author
*
*/
public class Room {
private String description;
private Room north;
private Room east;
private Room south;
private Room west;
//add a list of empty items to each room
private ArrayList<String> items = new ArrayList<>();
private String key; //null means no key
public Room(String desc) {
description = desc;
}
/**
* Connect the west exit of this room to the 'other' room.
* It automatically makes the connection in both directions.
*
* @param other
*/
public void connectWest(Room other) {
this.west = other;
other.east = this;
}
public void connectNorth(Room other) {
this.north = other;
other.south = this;
}
/**
* Prints a description of this room and its contents.
*/
public void describe() {
final StringBuilder itemsString = new StringBuilder("");
for (String item : items) {
itemsString.append(" ").append(item);
}
System.out.println(itemsString.toString());
System.out.println(description);
}
public List<String> getItems() {
return(items);
}
/**
* Try to move in the given direction from this room.
*
* @param dir Should be "north", "east", "south" or "west".
* @return the new room.
*/
public Room move(String dir, ArrayList<String> inventory) {
Room nextRoom = null;
if (dir.equals("north") && current.equals("frontdoor") { // i don't know what i'm doing or if i'm even in the right class
if (inventory("Front Door Key") == True) {
nextRoom
}
}
if (dir.equals("north")) {
nextRoom = north;
} else if (dir.equals("east")) {
nextRoom = east;
} else if (dir.equals("south")) {
nextRoom = south;
} else if (dir.equals("west")) {
nextRoom = west;
} else {
System.out.println("Error: unknown direction " + dir);
}
if (nextRoom == null) {
System.out.println("You cannot go " + dir + " from here.");
nextRoom = this; // stay in this room
} else {
// TODO: check to see if a 'key' is required to enter nextRoom?
}
return nextRoom;
}
}
current.getItems().remove(item);
current.getItems().add(item);
boolean contains(Object)
boolean remove(Object)
private void take(String item) {
if (current.contains(item) == true) {
current.removeItem(item); // can't get this to work
inventory.add(item);
System.out.println("You picked up the " + item + "and stashed it away");
} else {
System.out.println("Unknown object '" + item + "'.");
}
}
private void drop(String item) {
if (current.contains(item) == true) {
inventory.remove(item);
current.addItem(item); // or this
System.out.println("You dropped the " + item + "and left it in the " + current);
} else {
System.out.println("Unknown object '" + item + "'.");
}
}
public class Room {
//...
public boolean isLocked() {
return key != null;
}
public String requiredKey() {
return key;
}
/**
* Try to move in the given direction from this room.
*
* @param dir Should be "north", "east", "south" or "west".
* @return the new room.
*/
public Room getNextRoom(String dir) {
Room nextRoom = null;
if (dir.equals("north")) {
nextRoom = north;
} else if (dir.equals("east")) {
nextRoom = east;
} else if (dir.equals("south")) {
nextRoom = south;
} else if (dir.equals("west")) {
nextRoom = west;
} else {
System.out.println("Error: unknown direction " + dir);
}
return nextRoom;
}
public boolean contains(String item) {
return items.contains(item);
}
public void addItem(String item) {
items.add(item);
}
public void removeItem(String item) {
items.remove(item);
}
}
} else if (line.startsWith("go ")) {
String dir = line.substring(3);
Room next = current.getNextRoom(dir);
if (next == null) {
System.out.println("You cannot go " + dir + " from here.");
} else {
if (next.isLocked() && inventory.contains(next.requiredKey())) {
current = next;
System.out.println("You use the " + next.requiredKey() + " to unlock the next room...");
current.describe();
} else if (next.isLocked()) {
System.out.println("That room is locked and you don't have the required key");
} else {
current = next;
current.describe();
}
}
} else if (line.startsWith("take ")) {
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Game {
private Room yard;
private Room frontdoor;
private Room hallway;
private Room bathroom;
private Room lounge;
private Room kitchen;
private Room bedroom;
private Room basementdoor;
private Room basement;
private Room exit;
private Room current;
//create an inventory
private ArrayList<String> inventory = new ArrayList<String>();
public Game() {
yard = new Room("You're standing in open area.\n"
+ "There is a house to the north.\n");
frontdoor = new Room("You come to an old timber door, but it's locked.\n"
+ "There's a key on the ground. Talk about security...\n"
+ "There's a yard to the south.\n");
frontdoor.setKey("Front door key");
hallway = new Room("A hall stretches out into darkness.\n"
+ "There is a small flicker of light to the North.\n"
+ "There's a faint dripping sound to the west"
+ "There's a strange smell comming from the east");
bathroom = new Room("You enter what looks like a bathroom. It doesn't look like anyones cleaned it it months.\n"
+ "There's four numbers scratched into the wall - '1517'.\n"
+ "The hall is to the east.");
kitchen = new Room("You come into a kitchen, there's moldy food still in grocery bags on the bench.\n"
+ "You see a sandwhich and pick it up to investigate. Fortunately it looks fresh, and you're pretty hungry.\n"
+ "The hall is back to the west.");
kitchen.addItem("Mouldy Sandwhich?");
lounge = new Room("You come to a dimly lit loungeroom.\n"
+ "The couches have been toppled over, a book case lies fallen on the floor. The embers are still hot and flickering in the fireplace.\n"
+ "There's a door on the western wall.");
bedroom = new Room("You walk into a bedroom.\n"
+ "There's snoring comming from a huddled lump on the bed. Better be quiet.\n"
+ "There's a large metal door on the southern wall");
basementdoor = new Room("There's no breaking through this thing, it's too solid.\n"
+ "There's a small keypad next to the door. Hmm?\n");
basement = new Room("The code unlocks the door. You climb down a dimly lit staircase into what looks like a basement.\n"
+ "Suddenly, you hear some yelling from upstairs and some footsteps making their way down the staircase!\n"
+ "There's a window to north, quick!\n");
exit = new Room("You crawl through the window, out into the open and run down the street.\n"
+ "Why did you even go into that house in first place?\n");
current = yard;
//Connect the rooms
yard.connectNorth(frontdoor);
frontdoor.connectNorth(hallway);
hallway.connectNorth(lounge);
hallway.connectWest(bathroom);
kitchen.connectWest(hallway);
lounge.connectWest(bedroom);
basementdoor.connectNorth(bedroom);
basement.connectNorth(basementdoor);
basement.connectNorth(exit);
}
/**
* Play the interactive text game.
*
* Reads commands from the given input, line by line.
*
* @param in
*/
private void take(String item) {
if (current.contains(item) == true) {
current.removeItem(item); // can't get this to work
inventory.add(item);
System.out.println("You picked up the " + item + "and stashed it away");
} else {
System.out.println("Unknown object '" + item + "'.");
}
}
private void drop(String item) {
if (current.contains(item) == true) {
inventory.remove(item);
current.addItem(item); // or this
System.out.println("You dropped the " + item + "and left it in the " + current);
} else {
System.out.println("Unknown object '" + item + "'.");
}
}
public void play(Scanner in) {
current.describe();
while (current != exit) {
String line = in.nextLine().toLowerCase();
if (line.equals("quit")) {
System.out.println("You gave up!");
break;
} else if (line.startsWith("go ")) {
String dir = line.substring(3);
Room next = current.getNextRoom(dir);
if (next == null) {
System.out.println("You cannot go " + dir + " from here.");
} else {
if (next.isLocked() && inventory.contains(next.requiredKey())) {
current = next;
System.out.println("You use the " + next.requiredKey() + " to unlock the next room...");
current.describe();
} else if (next.isLocked()) {
System.out.println("That room is locked and you don't have the required key");
} else {
current = next;
current.describe();
}
}
} else if (line.startsWith("take ")) {
take(line.substring(5));
} else if (line.startsWith("print inventory")) {
System.out.print(inventory);
} else {
System.out.println("Unknown command '" + line + "'. Try go/take/quit.");
}
}
}
/**
* Starts the whole game.
*/
public static void main(String[] args) {
Game game = new Game();
game.play(new Scanner(System.in));
}
public class Room {
private String description;
private Room north;
private Room east;
private Room south;
private Room west;
//add a list of empty items to each room
private ArrayList<String> items = new ArrayList<>();
private String key; //null means no key
public Room(String desc) {
description = desc;
}
public void setKey(String key) {
this.key = key;
}
/**
* Connect the west exit of this room to the 'other' room. It automatically
* makes the connection in both directions.
*
* @param other
*/
public void connectWest(Room other) {
this.west = other;
other.east = this;
}
public void connectNorth(Room other) {
this.north = other;
other.south = this;
}
/**
* Prints a description of this room and its contents.
*/
public void describe() {
final StringBuilder itemsString = new StringBuilder("");
for (String item : items) {
itemsString.append(" ").append(item);
}
System.out.println(itemsString.toString());
System.out.println(description);
}
public boolean isLocked() {
return key != null;
}
public String requiredKey() {
return key;
}
/**
* Try to move in the given direction from this room.
*
* @param dir Should be "north", "east", "south" or "west".
* @return the new room.
*/
public Room getNextRoom(String dir) {
Room nextRoom = null;
if (dir.equals("north")) {
nextRoom = north;
} else if (dir.equals("east")) {
nextRoom = east;
} else if (dir.equals("south")) {
nextRoom = south;
} else if (dir.equals("west")) {
nextRoom = west;
} else {
System.out.println("Error: unknown direction " + dir);
}
return nextRoom;
}
public boolean contains(String item) {
return items.contains(item);
}
public void addItem(String item) {
items.add(item);
}
public void removeItem(String item) {
items.remove(item);
}
}
}