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