我正在努力处理我的游戏库存扫描,如果存在"飞行扫帚",它基本上会搜索用户清单太长),如果没有的话,它将再次运行挑战ragon()的方法;否则,如果存在该项目,它将继续进行下一个挑战。我认为插入方法是参数,但不可能。这就是我现在拥有的。:
public class Main {
String Flyingbroom = "Flying broom";
public static void main(String[] args) {
Player_inventory p = new Player_inventory();
challengedragon();
}
public void challengedragon() {
System.out.println("a Hungarian Horntail dragon! Let's start the battle! You have four options to beat the dragon: ");
System.out.println("1: Fly away with your broom");
System.out.println("2: Fight the dragon");
System.out.println("3: Just run to the egg and get it");
System.out.println("4: Hide behind a rock");
System.out.println("5: Go back to Hogwart");
System.out.println("Your choice is: ");
Scanner in = new Scanner(System.in);
int dragonfightchoice = in .nextInt();
if (dragonfightchoice == 1) {
{
p.Scanitem(Flyingbroom,
"Good choice! You managed to kill the Hungarian Horntail dragon and to get the golden egg",
"You dont have the broom. Try to search for the broom",
playerHP);
proceedtonextchallengelake();
} else if (dragonfightchoice == 2) {
System.out.println("The Hungarian Horntail dragon fired you. - 70HP. ");
playerHP -= 70;
challengedragon();
} else if (dragonfightchoice == 3) {
System.out.println("Bad idea... You lose 100 HP");
playerHP -= 100;
challengedragon();
} else if (dragonfightchoice == 4) {
System.out.println("The dragon found you. You lose 30 HP");
playerHP -= 30;
challengedragon();
} else if (dragonfightchoice == 5) {
Hogwart();
} else {
invalid();
challengedragon();
}
}
对于我的库存类:
public void Scanitem(String item, String trueouputext, String textifconditionisnotmet) {
if (inv.contains(item) == true) {
System.out.println(trueouputext);
} else if (inv.contains(item) == false) {
System.out.println(textifconditionisnotmet);
}
public static ArrayList<String> inv = new ArrayList<String>();
你们有什么建议吗?
还有其他步骤可以填充库存(变量inv
)?
另外,根据是否找到该物品,您是否不希望ScaniTem回答True或False?然后,您将拥有类似的东西:
public boolean scanitem(String item) {
return ( inv.contains(item) );
}
if ( p.scanItem(flyingBroom) ) {
System.out.println("Good choice! You managed to kill the Hungarian Horntail dragon and to get the golden egg");
} else {
System.out.println("You dont have the broom. Try to search for the broom");
}
这将使您更接近想要的东西。但是,您还需要将其他两个问题放入您的代码中:
您需要某种循环,而不是从自身内部调用challengeDragon
。
以某种方式,必须使用ScanItem的返回值来决定是否循环。
当前,每次玩家做某事时,您都会对方法进行嵌套调用,这意味着您迟早会用完堆栈。对于基于文本的冒险框架,更好的想法是对当前游戏的状态进行某种描述。状态可以表示为包含以下信息的对象:
- 当前的播放器在哪里(在哪个步骤中,"越过"等)
- 玩家的统计数据(HP,可用技能等)
- 玩家库存的内容
- 一些先前影响游戏的选择
然后,代码可以写成一个简单的循环,可以执行以下操作:
- 流程播放器的输入
- 根据玩家的输入来更改状态
- 根据新状态向玩家提供可用选项
- 等待下一个输入
- 重复