我正在为我的游戏进行库存扫描,如果“飞行扫帚”存在(如果它是用另一种方法收集并且上载代码太长),它基本上会搜索用户库存(如果没有)再次运行方法Challengedragon();否则,如果存在该项目,它将继续进行下一个挑战。我曾想过将插入方法作为参数,但不可能。这就是我现在所拥有的。 :
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>();
你们有什么建议吗?
答案 0 :(得分:1)
是否还有其他步骤来填充清单(变量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的返回值来决定是否循环。
答案 1 :(得分:1)
当前,您每次播放器执行某项操作时都会嵌套调用方法,这意味着您迟早会耗尽堆栈。基于文本的冒险框架的一个更好的主意是对当前游戏的状态进行某种描述。状态可以表示为包含以下信息的对象:
然后,可以将代码编写为执行以下操作的简单循环: