我正在制作基于文本的冒险游戏。
以下是我坚持的问题部分;
扩展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;
}
}
答案 0 :(得分:2)
这些是Game类的方法。你的一些任务描述有点模糊,但我试图适应它们。
添加一个getter方法。
public List<String> getItems() {
return items;
}
扩展
Room.describe()
方法,以便打印所有项目 房间。
public void describe() {
final StringBuilder itemsStr = new StringBuilder("");
for (String item : items) {
itemsStr.append(" ").append(item);
}
System.out.println("You can see:" + itemsStr.toString());
}
在你的游戏构造函数中,为每个构建器添加一个或两个有趣的项目 房间,像这样:
lounge.getItems().add("balloon");
确保所有 您的商品名称为小写。
public boolean addItem(String item) {
if (item != null) {
items.add(item.toLowerCase());
return true;
} else {
return false;
}
}