我正在制作一款基于文本的冒险游戏。
这是我遇到的问题的部分;
- 添加一个 getter 方法(称为 getItems()),以便游戏的其他部分(例如 Game 类)可以访问每个房间内的项目。 (我们不需要setter方法,因为Java ArrayList对象是可变的,所以我们可以使用它的add和remove方法修改列表的内容)。
扩展 Room.describe() 方法,以便打印房间中的所有项目。提示:使用讲座中展示的特殊“for”循环:
for (String s:items) {System.out.println(" You can see: " + s);
在游戏构造函数中,向每个房间添加一两个有趣的项目,如下所示:
lounge.getItems().add("balloon");
确保您的所有项目名称均为小写。
这是游戏类:
package TextGame;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Game {
private Room hallway;
private Room lounge;
private Room bedroom;
private Room door;
private Room porch;
private Room kitchen;
private Room bathroom;
private Room basement;
private Room letter;
private Room current;
List<String> inventory = new ArrayList<String>();
public Game() {
hallway = new Room("You are in a dark hallway.\n"
+ "There is a table beside you, and a light to the west.\n");
lounge = new Room("You are in a brightly-lit lounge, with two red sofas.\n"
+ "A stereo is playing soft music.\n");
bedroom = new Room("You are in a bedroom.\n"
+ "You can see a bunk bed, two cricket bats, and an aquarium.\n");
door = new Room("There is a large door in front of you that leads into the house\n"
+ "You need a key to open it");
porch = new Room("You are standing outside the house.\n"
+ "There is a front door to the west and a letter box to the south");
kitchen = new Room("You're in a kitchen and make a sandwhich");
basement = new Room("You go down stairs to find a large dim-lit basement");
letter = new Room("You look inside the letterbox, there are keys in here!\n");
letter.items.add("house keys");
current = porch;
porch.connectWest(door);
letter.connectNorth(porch);
door.connectWest(hallway);
hallway.connectWest(lounge);
bathroom.connectNorth(hallway);
lounge.connectNorth(kitchen);
lounge.connectWest(bedroom);
basement.connectNorth(bedroom);
}
/**
* Play the interactive text game.
*
* Reads commands from the given input, line by line.
*
* @param in
*/
public void play(Scanner in) {
current.describe();
while (current != porch) {
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 {
System.out.println("Unknown command '" + line + "'. Try go/take/quit.");
}
}
}
public void take(String item) {
if(item.room == True) {
inventory.add(item);
}
}
public void drop(String item) {
if(item.inventory == True) {
inventory.remove(item);
}
}
/** Starts the whole game. */
public static void main(String[] args) {
Game game = new Game();
game.play(new Scanner(System.in));
}
}
这是房间等级;
package TextGame;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Room {
private String description;
private Room north;
private Room east;
private Room south;
private Room west;
private ArrayList<String> items = new ArrayList<>();
public getItems() {
}
public Room(String desc) {
description = desc;
}
public void connectWest(Room other) {
this.west = other;
other.east = this;
}
public void connectNorth(Room other) {
this.north = other;
other.south = this;
}
public void describe() {
System.out.println(description);
}
public Room move(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);
}
if (nextRoom == null) {
System.out.println("You cannot go " + dir + " from here.");
nextRoom = this;
} else {
}
return nextRoom;
}
}
最佳答案
这些是 Game 类的方法。您的一些任务描述有点模糊,但我尽力适应它们。
Add a getter method.
public List<String> getItems() {
return items;
}
Extend the
Room.describe()
method so that it prints all the items in the room.
public void describe() {
final StringBuilder itemsStr = new StringBuilder("");
for (String item : items) {
itemsStr.append(" ").append(item);
}
System.out.println("You can see:" + itemsStr.toString());
}
In your Game constructor, add one or two interesting items to each room, like this:
lounge.getItems().add("balloon");
Make sure that all your items have lowercase names.
public boolean addItem(String item) {
if (item != null) {
items.add(item.toLowerCase());
return true;
} else {
return false;
}
}
关于java - 在 Java 中添加和删除库存项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32086917/