-4

I think my question is simple. I'm going through a Java book for beginners and decided to make a fun little game based on what I had learned so far (I am mostly through the book at the chapter before Objects so forgive my beginner-type design).

I'm trying to have the game end when the game-over condition is met. It'll sort of be like a Choose Your Own Adventure book... guess wrong, and you die... go back to the first chapter. Well, here is what I have so far:

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();
  }
}
Mike Wills
  • 20,959
  • 28
  • 93
  • 149
RodNICE
  • 25
  • 1
  • 7

2 Answers2

0

You could encase your whole program in a while loop.

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!
    }
}

And you'll need to change your chapter methods to return a boolean indicating whether the right (true) or wrong (false) choice was selected.

But this is pretty poor design, as there is a lot of repeated code, especially once you add a lot of chapters.

You could make a chapter a class with a run() method or something like that:

public class Chapter {
    public boolean run();
}

And override run() with all the different stuff you want to do in your chapters.

Then you could just iterate through all the Chapters

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

    break;
}

But this is still not the greatest, as it means your story is pretty linear and from what I remember, choose-your-own adventure stories could result in you jumping all over the place.

Fodder
  • 564
  • 1
  • 10
  • 23
  • Thank you for that answer. I will tinker with the code and post my results. I do agree, this design could be enhanced with classes and such but I was not yet there in the Think Java book I was using to learn programming. I have learned so far why classes and data-encapsulation via passing objects as parameters is so important. – RodNICE May 01 '17 at 17:06
0

Thank you guys for all your help. You were all amazing. This is what I have so far. It works, not pretty, but it works. Next, I'll task myself with converting this with a more class-based design:

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

}

RodNICE
  • 25
  • 1
  • 7