Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:基于文本的RPG:GameOver逻辑_Java_Game Engine_Text Based - Fatal编程技术网

Java:基于文本的RPG:GameOver逻辑

Java:基于文本的RPG:GameOver逻辑,java,game-engine,text-based,Java,Game Engine,Text Based,我想我的问题很简单。我正在阅读一本面向初学者的Java书籍,并决定根据我迄今为止所学的内容制作一个有趣的小游戏(我主要是在对象前一章阅读这本书,所以请原谅我的初学者类型设计) 我正试图在比赛结束条件满足时结束比赛。这有点像一本选择你自己的冒险书。。。猜错了,你就死了。。。回到第一章。到目前为止,我所掌握的情况如下: import java.util.*; public class Princess { /* Generate a random number between (and i

我想我的问题很简单。我正在阅读一本面向初学者的Java书籍,并决定根据我迄今为止所学的内容制作一个有趣的小游戏(我主要是在对象前一章阅读这本书,所以请原谅我的初学者类型设计)

我正试图在比赛结束条件满足时结束比赛。这有点像一本选择你自己的冒险书。。。猜错了,你就死了。。。回到第一章。到目前为止,我所掌握的情况如下:

import java.util.*;

public class Princess {


  /* Generate a random number between (and including) 1 and 2.
   * Use this number to set which option is the "good" option this time around. 
   */
  public static String generateRandom() {
    Random random = new Random();
    int randomNumber = random.nextInt(2) + 1;
    String goodChoice = "option" + randomNumber;
    return goodChoice; 
  }


  /* Checks user's choice.
   * Does all the heavy lifting for the program.
   */
  public static void guessCheck(String message, String option1,
                           String option2, String badStuff) {

    String goodChoice = generateRandom();
    System.out.println(message);
    Scanner in = new Scanner(System.in);
    String userSelection = in.next();


    /*Hashmap loading up the options*/
    HashMap<String, String> hmap = new HashMap<String, String>();
    hmap.put("option1", option1);
    hmap.put("option2", option2);
    /* Get values based on key (i.e. the first "column" in the hash map) */
    String correctChoice = hmap.get(goodChoice);
    System.out.println(" *********** Hash Map **************");
    System.out.println("value at selected index: " + correctChoice);
    /* ********************************************** */

   System.out.println("You entered: " + userSelection); 

    if (!userSelection.equals(hmap.get("option1")) && !userSelection.equals(hmap.get("option2"))) {
      //String word = in.next(); // Consumes the invalid input
      System.out.println(hmap.get("option1"));
      System.out.println(hmap.get("option2"));
      System.err.println("What does that mean? Type the correct option!.");
    } 
    else if (userSelection.equals(correctChoice)) {
     System.out.println("Good choice! You selected " + correctChoice);
     return;
    } 
    else {
     System.out.println(badStuff);
     return;
    }
    guessCheck(message, option1, option2, badStuff);
   } 


  // *** Chapter 1 **************************************
  /* These chapter methods simply just provide the above guessCheck
   * method with the values for the variables it uses.
   */
  public static void chapter_1(){
    System.out.println("***** Chapter 1 *****");

    // Chapter message
    String message = "You go through a forest.\n";
    message = message + "You see two doors; one on the right, one on the left. \n";
    message = message + "Which one do you choose?\n";

    // Available options that the user can select.
    String option1 = "l"; // Left door
    String option2 = "r"; // Right door

    // Consequence of choosing the wrong option.
    String badStuff = "Wrong choice. You dead.";

    // Send these options to guessCheck to do the processing.
    guessCheck(message, option1, option2, badStuff); 
  }
  //*****************************************************************************


  // *** Chapter 2 **************************************
  /* These chapter methods simply just provide the above guessCheck
   * method with the values for the variables it uses.
   */
  public static void chapter_2(){
    System.out.println("***** Chapter 2 *****");
   }
  //*****************************************************************************


  //*** Game Over ***********************************   
  public static void gameOver(){
    System.out.println("Game Over");
  }


  //*** START ***********************************  
  public static void main(String[] args) {    
    chapter_1();
    chapter_2();
    gameOver();
  }
}
import java.util.*;
公务舱公主{
/*生成一个介于(包括)1和2之间的随机数。
*使用此数字可设置这次哪个选项是“好”选项。
*/
公共静态字符串生成器dom(){
随机=新随机();
int randomNumber=random.nextInt(2)+1;
字符串goodChoice=“option”+随机数;
回归美好选择;
}
/*检查用户的选择。
*为项目做所有繁重的工作。
*/
公共静态无效猜测检查(字符串消息,字符串选项1,
字符串选项2,字符串坏东西){
字符串goodChoice=generateradom();
System.out.println(消息);
扫描仪输入=新扫描仪(系统输入);
字符串userSelection=in.next();
/*Hashmap加载选项*/
HashMap hmap=新的HashMap();
hmap.put(“选项1”,选项1);
hmap.put(“选项2”,选项2);
/*基于键(即哈希映射中的第一个“列”)获取值*/
字符串correctChoice=hmap.get(goodChoice);
System.out.println(“***************哈希映射****************”);
System.out.println(“选定索引处的值:“+correctChoice”);
/* ********************************************** */
System.out.println(“您输入:“+userSelection”);
如果(!userSelection.equals(hmap.get(“option1”)&&!userSelection.equals(hmap.get(“option2”)){
//String word=in.next();//使用无效的输入
System.out.println(hmap.get(“option1”);
System.out.println(hmap.get(“option2”);
System.err.println(“这是什么意思?键入正确的选项!”);
} 
else if(userSelection.equals(correctChoice)){
System.out.println(“好的选择!您选择了”+正确的选择);
返回;
} 
否则{
System.out.println(坏东西);
返回;
}
猜测检查(消息、选项1、选项2、坏消息);
} 
//***第1章**************************************
/*这些方法只是提供了上面的猜测
*方法及其使用的变量的值。
*/
公共静态无效第1章(){
System.out.println(“****第1章*******”);
//章节信息
String message=“您穿过一片森林。\n”;
message=message+“您看到两扇门;一扇在右边,一扇在左边。\n”;
message=message+“您选择哪一个?\n”;
//用户可以选择的可用选项。
字符串option1=“l”//左门
字符串option2=“r”//右门
//选择错误选项的后果。
String badStuff=“选择错了,你们死定了。”;
//将这些选项发送到guessCheck以进行处理。
猜测检查(消息、选项1、选项2、坏消息);
}
//*****************************************************************************
//***第2章**************************************
/*这些方法只是提供了上面的猜测
*方法及其使用的变量的值。
*/
公共静态无效第二章(){
System.out.println(“****第2章*******”);
}
//*****************************************************************************
//***游戏结束************************************************************
公共静态void gameOver(){
System.out.println(“游戏结束”);
}
//***开始******************************************************
公共静态void main(字符串[]args){
第1章();
第二章();
gameOver();
}
}

您可以将整个程序封装在一个while循环中

public static void main(String[] args) {
    while (true) {
        if(!chapter_1()) {
            continue;
        }
        if(!chapter_2()) {
            continue;
        }
        // etc.
        break; // This is important, once you reach the end of your game, you need to break out!
    }
}
您需要更改
章节
方法,以返回一个
布尔值
,指示选择了正确(
true
)还是错误(
false

但是这是一个非常糟糕的设计,因为有很多重复的代码,尤其是当你添加了很多章节的时候

您可以使用run()方法或类似方法将章节设置为类:

public class Chapter {
    public boolean run();
}
并使用您希望在章节中执行的所有不同操作覆盖run()

然后你可以重复所有的章节

while (true) {
    for (Chapter chapter : chapters) {
        if(!chapter.run()) {
            continue;
        }
    }

    break;
}

但这仍然不是最好的,因为这意味着你的故事是线性的,根据我的记忆,选择你自己的冒险故事可能会让你跳得到处都是。

谢谢你们的帮助。你们都很了不起。这就是我目前所拥有的。它很管用,不漂亮,但很管用。下一步,我将用一种更基于类的设计来转换它:

import java.util.*;

public class Princess {


  /* Generate a random number between (and including) 1 and 2.
   * Use this number to set which option is the "good" option this time around. 
   */
  public static String generateRandom() {
    Random random = new Random();
    int randomNumber = random.nextInt(2) + 1;
    String goodChoice = "option" + randomNumber;
    return goodChoice; 
  }


  /* Checks user's choice.
   * Does all the heavy lifting for the program.
   */
  public static Boolean guessCheck(String message, String option1,
                           String option2, String badStuff) {

    String goodChoice = generateRandom();
    System.out.println(message);
    Scanner in = new Scanner(System.in);
    String userSelection = in.next();


    /*Hashmap loading up the options*/
    HashMap<String, String> hmap = new HashMap<String, String>();
    hmap.put("option1", option1);
    hmap.put("option2", option2);
    /* Get values based on key (i.e. the first "column" in the hash map) */
    String correctChoice = hmap.get(goodChoice);
    System.out.println(" *********** Hash Map **************");
    System.out.println("value at selected index: " + correctChoice);
    /* ********************************************** */

   System.out.println("You entered: " + userSelection); 

    if (!userSelection.equals(hmap.get("option1")) && !userSelection.equals(hmap.get("option2"))) {
      //String word = in.next(); // Consumes the invalid input
      System.out.println(hmap.get("option1"));
      System.out.println(hmap.get("option2"));
      System.err.println("What does that mean? Type the correct option!.");
    } 
    else if (userSelection.equals(correctChoice)) {
     System.out.println("Good choice! You selected " + correctChoice);
     return;
    } 
    else {
     System.out.println(badStuff);
     return;
    }
    return guessCheck(message, option1, option2, badStuff);
   } 


  // *** Chapter 1 **************************************
  /* These chapter methods simply just provide the above guessCheck
   * method with the values for the variables it uses.
   */
  public static Boolean chapter_1(){
    System.out.println("***** Chapter 1 *****");

    // Chapter message
    String message = "You go through a forest.\n";
    message = message + "You see two doors; one on the right, one on the left. \n";
    message = message + "Which one do you choose?\n";

    // Available options that the user can select.
    String option1 = "l"; // Left door
    String option2 = "r"; // Right door

    // Consequence of choosing the wrong option.
    String badStuff = "Wrong choice. You dead.";

    // Send these options to guessCheck to do the processing.
    return guessCheck(message, option1, option2, badStuff); 
  }
  //*****************************************************************************


  // *** Chapter 2 **************************************
  /* These chapter methods simply just provide the above guessCheck
   * method with the values for the variables it uses.
   */
  public static Boolean chapter_2(){
    System.out.println("***** Chapter 2 *****");
    //Put this chapter's variables and junk here.
   }
  //*****************************************************************************



  //*** START ***********************************  
    public static void main(String[] args) {

        while (true) {
            if (!chapter_1()) {
                break;
            }
            if (!chapter_2()) {
                break;
            }
            break;
            }

            System.out.println("break: Game Over. Do you want to play again?");
        }
import java.util.*;
公务舱公主{
/*生成一个介于(包括)1和2之间的随机数。
*使用此数字可设置这次哪个选项是“好”选项。
*/
公共静态字符串生成器dom(){
随机=新随机();
int randomNumber=random.nextInt(2)+1;
字符串goodChoice=“option”+随机数;
回归美好选择;
}
/*检查用户的选择。
*为项目做所有繁重的工作。
*/
公共静态布尔猜测检查(字符串消息,字符串选项1,
字符串选项2,字符串坏东西){
字符串goodChoice=generateradom();
System.out.println(消息);
扫描仪输入=新扫描仪(系统输入);
字符串userSelection=in.next();
/*Hashmap加载选项*/
HashMap hmap=新的HashMap();
hmap.put(“选项1”,选项1);
hmap.put(“选项2”,选项2);
/*基于键(即哈希映射中的第一个“列”)获取值*/
字符串correctChoice=hmap.get(goodChoice);