在Java中添加和删除库存项目
我正在做一个基于文本的冒险游戏 这是我一直坚持的部分问题在Java中添加和删除库存项目,java,Java,我正在做一个基于文本的冒险游戏 这是我一直坚持的部分问题 添加一个getter方法(称为getItems()),以便游戏的其他部分(如game类)可以访问每个房间内的项目。(我们不需要setter方法,因为Java ArrayList对象是可变的,所以我们可以使用其add和remove方法修改列表的内容) 扩展Room.descripe()方法,以便打印文件室中的所有项目。提示:使用讲座中显示的特殊“for”循环: for (String s:items) {System.out.println
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));
}
}
打包文本游戏;
导入java.util.Scanner;
导入java.util.List;
导入java.util.ArrayList;
公开课游戏{
私人房间走廊;
私人休息室;
包间卧室;
私人房间门;
私人房间门廊;
私人厨房;
私人浴室;
私人房间地下室;
私人房间信件;
私人房间电流;
列表清单=新的ArrayList();
公共游戏(){
走廊=新房间(“您在黑暗的走廊中。\n”
+“您旁边有一张桌子,西边有一盏灯。\n”);
lounge=新房间(“您在灯光明亮的休息室,有两张红色沙发。\n”
+“立体声正在播放柔和的音乐。\n”);
卧室=新房间(“您在卧室里。\n”
+“您可以看到一张双层床、两只蟋蟀蝙蝠和一个水族馆。\n”);
门=新房间(“前面有一扇通向房子的大门\n”
+“你需要一把钥匙才能打开它”);
门廊=新房间(“你站在房子外面。\n”
+“西面有一扇前门,南面有一个信箱”);
厨房=新房间(“你在厨房里做一个沙盘”);
地下室=新房间(“你下楼找到一个灯光昏暗的大地下室”);
字母=新房间(“你看信箱里面,这里有钥匙!\n”);
信函。项目。添加(“房屋钥匙”);
电流=门廊;
门廊。西(门);
信。北(门廊);
西大门(走廊);
走廊。西区(休息室);
浴室,北面(走廊);
休息室.北区(厨房);
休息室.西区(卧室);
地下室。北面(卧室);
}
/**
*玩互动文字游戏。
*
*逐行读取给定输入中的命令。
*
*@param-in
*/
公共无效播放(扫描仪输入){
current.descripe();
while(当前!=门廊){
String line=in.nextLine().toLowerCase();
如果(行等于(“退出”)){
System.out.println(“你放弃了!”);
打破
}else if(第行开始使用(“go”)){
当前=当前移动(行子字符串(3));
current.descripe();
}else if(line.startsWith(“take”)){
取(行、子串(5));
}否则{
System.out.println(“未知命令“+”行“+”。尝试go/take/quit。”);
}
}
}
公共作废获取(字符串项){
如果(item.room==True){
存货.增加(项);
}
}
公共作废删除(字符串项){
如果(item.inventory==True){
库存。移除(项目);
}
}
/**开始整个游戏*/
公共静态void main(字符串[]args){
游戏=新游戏();
游戏(新扫描器(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;
}
}
打包文本游戏;
导入java.util.Scanner;
导入java.util.List;
导入java.util.ArrayList;
公共教室{
私有字符串描述;
北包房;
包房东;
南部包房;
包房西;
private ArrayList items=new ArrayList();
公共getItems(){
}
公共房间(字符串描述){
描述=描述;
}
西公共空间(其他房间){
this.west=其他;
other.east=这个;
}
北面公共空间(其他房间){
this.north=其他;
other.south=这个;
}
公共空间描述(){
系统输出打印项次(说明);
}
公共房间移动(字符串方向){
房间nextRoom=null;
如果(方向等于(“北”)){
nextRoom=北;
}else if(方向等于(“东”)){
nextRoom=东部;
}else if(方向等于(“南”)){
nextRoom=南部;
}else if(dir.equals(“west”)){
nextRoom=西部;
}否则{
System.out.println(“错误:方向未知”+dir);
}
如果(nextRoom==null){
System.out.println(“您不能从这里转到”+dir+”);
nextRoom=这个;
}否则{
}
返回下一个房间;
}
}
这些是游戏类的方法。你的一些任务描述有点模糊,但我试着去适应它们
添加一个getter方法
在您的游戏构造器中,向每个构造器添加一个或两个有趣的项目
房间,如下所示:lounge.getItems().add(“气球”)代码>确保所有
您的项目具有小写名称
谢谢你的帮助,我很感激,是的,他们非常不精确,这是一个痛苦的哈哈
public List<String> getItems() {
return items;
}
public void describe() {
final StringBuilder itemsStr = new StringBuilder("");
for (String item : items) {
itemsStr.append(" ").append(item);
}
System.out.println("You can see:" + itemsStr.toString());
}
public boolean addItem(String item) {
if (item != null) {
items.add(item.toLowerCase());
return true;
} else {
return false;
}
}