Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Homework 04 (((((USE DRJAVA PROGRAM)))) Choose Your Own Adventure! 09/22/2017 at

ID: 3888253 • Letter: H

Question

Homework 04 (((((USE DRJAVA PROGRAM))))

Choose Your Own Adventure!

09/22/2017 at 11:55PM

Objective:

Write a program that allows the user to play a “Choose Your Own Adventure” game. In this game the user will be prompted with making decisions, which will then lead them to different paths with different results.

Requirements:

There must be at least 9 different endings.

Each ending requires at least two decisions.

To get 3 of the endings there must be at least 3 decisions the player has to make.

There must be at least one example of:

Numeric comparison, such as equals to, less than etc.

String comparison

A compound Boolean expression

These do not have to be a part of reaching each ending they just have to be in there somewhere

Be creative!

Example Dialog:

Welcome to the land of Donkeydwarf! Will you go to the inn, look for trouble, or talk to the shady looking elf standing near the entrance? Enter “inn”, “trouble”, or “elf”.

INN

You enter the musty inn, and approach the inn keeper. “Aye you looking to rent a room? Yes or No?”, she rasps.

Yes

“The cheap rooms are filled up, but if you do me a favor, then I’ll let you have a nice suit.” Enter yes or no.

Yes

“There’s this dragon ya see. He stole all my pickles. I want him to pay! Go make him pay!” You venture out and find the pickle pilfering dragon. The dragon will give you the pickles if you guess the number it is thinking between 1 and 10. Enter that number.

5

“That was not the number I thought of”. The dragon roasts you alive and you are consumed with the rest of the stolen pickles. THE END.

Explanation / Answer

import java.util.*;

//Class OwnAdventureGame definition

public class OwnAdventureGame

{

//Creates questions

String question1[] = {"Are you Hungry? Enter yes or no: ", "Looking for hotel. Enter yes or no: ",

"What to do? Enter In or Out: ", "Like to cook food: Enter yes or no: ",

"Beg for food. Enter yes or no: ", "Pay my guss amount to leave you. Enter the guess number(1-5): "};

String question2[] = {"Are you Male? Enter yes or no: ", "Would you like to dance? Enter yes or no: ", "Enter into the dace floor. Enter In or Out: ", "Pay my guss amount to leave you. Enter the guess number(1-5): "};

//Creates options

String Options[] = {"Yes", "No", "In", "Out"};

//To store question number

int questionNumber;

//Method to return next question from the question list

int getQuestion(int currentNo)

{

//Scanner object created to accept data

Scanner sc = new Scanner(System.in);

//Counter is initialized to current question number

int counter = currentNo;

//Accepts the answer

String answer = sc.next();

//Checks the question set

switch(questionNumber)

{

//Question set 2

case 1:

//Checks if the option entered by the user is yes

if(answer.equalsIgnoreCase(Options[0]) || answer.equalsIgnoreCase(Options[2]))

//Increase the counter by one

counter++;

//Checks if the option entered by the user is no

else if(answer.equalsIgnoreCase(Options[1]) || answer.equalsIgnoreCase(Options[3]))

//Set the counter for the last question

counter = question1.length - 1;

/* //Checks if the option entered by the user is in

else if(answer.equalsIgnoreCase(Options[2]))

//Increase the counter by one

counter++;

//Checks if the option entered by the user is out

else if(answer.equalsIgnoreCase(Options[3]))

//Set the counter for the last question

counter = question1.length - 1;*/

break;

//Question set 2

case 2:

//Checks if the option entered by the user is yes

if(answer.equalsIgnoreCase(Options[0]) || answer.equalsIgnoreCase(Options[2]))

//Increase the counter by one

counter++;

else if(answer.equalsIgnoreCase(Options[1]) || answer.equalsIgnoreCase(Options[3]))

//Set the counter for the last question

counter = question2.length - 1;

/*else if(answer.equalsIgnoreCase(Options[2]) && questionNumber == 1)

counter++;

else if(answer.equalsIgnoreCase(Options[3])&& questionNumber == 1)

counter = question2.length - 1;*/

break;

}//End of switch - case

//Returns the next question number

return counter;

}//End of method

//Method to play the game

void playGame()

{

//Random class object created

Random rand = new Random();

//Scanner class object created to accept data from user

Scanner scc = new Scanner(System.in);

//To store next question counter

int val = 0;

//To store amount entered by the user and to store the random amount generated

int amtR = 0, amtE;

//Checks the question set

switch(questionNumber)

{

//Question set 1

case 1:

//Loops till last question in question set

do

{

//Displays the question

System.out.println(question1[val]);

//Extracts next question from question set

val = getQuestion(val);

//Checks for the last question

if(val == question1.length -1)

{

System.out.println(question1[val]);

//Generates random amount

amtR = rand.nextInt(1)+5;

//Accepts user entered amount

amtE = scc.nextInt();

//Displays the question

//Checks both the amount is equal or not and displays the result

if(amtR == amtE)

System.out.println("Correct");

else

System.out.println("Not Correct");

System.out.println("THE END");

break;

}//End of if

}while(true); //End of do - while loop

break;

//Question set 2

case 2:

//Loops till last question in question set

do

{

//Displays the question

System.out.println(question2[val]);

//Extracts next question from question set

val = getQuestion(val);

System.out.println(question2[val]);

//Checks for the last question

if(val == question2.length -1)

{

//Generates random amount

amtR = rand.nextInt(1)+5;

//Accepts user entered amount

amtE = scc.nextInt();

//Checks both the amount is equal or not and displays the result

if(amtR == amtE)

System.out.println("Correct");

else

System.out.println("Not Correct");

System.out.println("THE END");

break;

}//End of if

}while(true); //End of do - while loop

break;

}//End of switch - case

}//End of method

//Main method

public static void main(String[] args)

{

//OwnAdventureGame class object created

OwnAdventureGame og = new OwnAdventureGame();

//Generates a random number for question set

og.questionNumber = (int)(Math.random()*1)+1;

//Start game

og.playGame();

}//End of main method

}//End of class

Sample Run:

Are you Hungry? Enter yes or no:
yes
Looking for hotel. Enter yes or no:
yes
What to do? Enter In or Out:
in
Like to cook food: Enter yes or no:
no
Pay my guss amount to leave you. Enter the guess number(1-5):
5
Correct
THE END