Java:基于文本的RPG:GameOver Logic

时间:2017-04-30 22:56:51

标签: java game-engine text-based

我认为我的问题很简单。我正在为初学者阅读一本Java书,并决定根据我迄今学到的东西制作一个有趣的小游戏(我主要通过在Objects之前的那一章中的书,原谅我的初学者类型设计)。

我正试图在满足游戏结束条件时让游戏结束。它有点像选择你自己的冒险书......猜错了,你死了......回到第一章。嗯,这是我到目前为止所做的:

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();
  }
}

2 个答案:

答案 0 :(得分:0)

你可以将整个程序包含在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!
    }
}

并且您需要更改chapter方法以返回boolean,表明是选择了正确的(true)还是错误的(false)选项

但这是非常差的设计,因为有很多重复的代码,特别是一旦你添加了很多章节。

你可以用一个run()方法或类似的东西创建一个章节:

public class Chapter {
    public boolean run();
}

用你想要在章节中做的所有不同的东西覆盖run()。

然后你可以遍历所有的章节

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

    break;
}

但这仍然不是最伟大的,因为这意味着你的故事非常线性,而且从我记忆中,选择你自己的冒险故事可能会导致你到处跳跃。< / p>

答案 1 :(得分:0)

谢谢大家的帮助。你们都很棒。这就是我到目前为止所拥有的。它工作,不漂亮,但它的工作原理。接下来,我将使用更基于类的设计来转换它:

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?");
        }

}