Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package millionaire;
- import java.util.Random;
- import java.util.Scanner;
- public class Millionaire {
- static Random rand = new Random(); //new random number generator
- static Scanner scanner = new Scanner(System.in); //new scanner
- static boolean fiftyFifty, pollAudience, changeQuestion; //three booleans which will to used to track the three life lines, 50/50, Poll the Audience and Replace Question
- static boolean walkedAway = false; // boolean that will track if the player chooses to walk away or not
- public static void main(String[] args) {
- String[] questions = {"What continent is the United States of America located in?", //array that contains all the questions for the game, going from easy to hard
- "What leaf is featured on the Canadian flag?",
- "How many stars are on the American flag?",
- "In what country would you find the cities Glasgow and Edinburgh?",
- "What country has the second largest population in the world?",
- "Mount Fuji is the highest mountain in what country?",
- "What country is home to the Great Barrier Reef?",
- "Zurich is the largest city in what country?",
- "In what country would you find the islands Santorini and Mykonos?",
- "Excluding Gibraltar, how many countries does Spain border?",
- "One of these countries has a border ONLY with South Africa. Which one?",
- "Which country would you need to cross to get from Ecuador to Chile?",
- "In what country is the Yangtze River found?",
- "How many countries are there ending in 'stan'",
- "What country was known as Ceylon until 1972?"};
- String[] choiceA = {"Europe", "Sycamore", "49", "Scotland", "China", "Japan", "Australia", "Latvia", "Italy", "1", "Swaziland", "Bolivia", "South Korea", "4", "Nauru"}; //choice letter A for each corresponding question in the questions array
- String[] choiceB = {"Antarctica", "Maple", "50", "England", "Russia", "China", "America", "Austria", "Greece", "2", "Botswana", "Argentina", "China", "5", "Sri Lanka"}; //choice letter B for each corresponding question in the questions array
- String[] choiceC = {"North America", "Oak", "51", "Ireland", "India", "Nepal", "New Zealand", "Germany", "Portugal", "3", "Letsotho", "Colombia", "Mongolia", "6", "Kiribati"}; //choice letter C for each corresponding question in the questions array
- String[] choiceD = {"South America", "Birch", "52", "Wales", "United States of America", "Andora", "India", "Switzerland", "Japan", "4", "Mozambique", "Peru", "Vietnam", "7", "Djibouti"}; //choice letter D for each corresponding question in the questions array
- int[] answers = {3,2,2,1,3,1,1,4,2,3,3,4,2,4,2}; //the answers for the questions in the questions array, 1 corresponds to choice A, 2 corresponds to choice B, 3 corresponds to choice C, 4 corresponds to choice D
- String[] Rquestions = {"How many continents are there?", //array which holds 3 different questions of varied difficulty for the replace question life line
- "How many more countries are there in the UK than in Great Britain?",
- "What is the sixth largest country in terms of land mass?"};
- String[] RchoiceA = {"6", "1", "China"}; //choice letter A for each corresponding question in the replace questions array
- String[] RchoiceB = {"7", "2", "Brazil"}; //choice letter B for each corresponding question in the replace questions array
- String[] RchoiceC = {"8", "3", "Australia"}; //choice letter C for each corresponding question in the replace questions array
- String[] RchoiceD = {"9", "4", "India"}; //choice letter D for each corresponding question in the replace questions array
- int[] Ranswers = {2,1,3}; //the answers for the questions in the replace questions array, 1 corresponds to choice A, 2 corresponds to choice B, 3 corresponds to choice C, 4 corresponds to choice D
- int[] prizes = {0,100,200,300,500,1000,2000,4000,8000,16000,32000,64000,125000,250000,500000,1000000}; //an array storing all the prize values that can be won in the game, from 0, 100 all the way to 100000000
- boolean pass; //boolean that will track whether or not the player has correctly answered the question and passes on to the next round
- String replay; //will be used to store an answer whether the player wants to play the game again
- int i; //will be used for for loops and other calculations
- do { //do while loop, explained with the while portion down below
- System.out.println(" ==== Rated 10/10 on IGN ====\n WHO WANTS TO BE A MILLIONAIRE \n ===== Best Game 2018 ====="); //name of the program ha ha
- pass = true; //sets pass to true so that the first question can be played
- fiftyFifty = true; //sets the 50/50 life line to true so that it may be reset and ready to be used, this resets the life line if user chose to play again
- pollAudience = true; //sets the Poll the Audience life line to true so that it may be reset and ready to be used, this resets the life line if user chose to play again
- changeQuestion = true; //sets the Replace Question life line to true so that it may be reset and ready to be used, this resets the life line if user chose to play again
- for (i = 0; i < 15 && pass; i++) { //for loop which will run if i has not yet reached fifteen (the amount of questions) and the user got the previous question correct
- pass = askQuestion (questions[i], choiceA[i], choiceB[i], choiceC[i], choiceD[i], answers[i], prizes[i], prizes[i + 1], i, Rquestions[i / 5], RchoiceA[i / 5], RchoiceB[i / 5], RchoiceC[i / 5], RchoiceD[i / 5], Ranswers[i / 5]);
- // ^^ runs the ask question method, biggest one of the program, this method will return the pass value each time the for loop loops to see if the user answered the previous question correctly, the replace arrays are divided by 5 in order to choose the correct difficulty if the replace question life line is used (0 for rounds 1-5, 1 for rounds 6-10 and 2 for rounds 11-15)
- if (pass && i < 14) { //each time user answers the question correct and it is not the last question
- System.out.println("Great job, you move on to the next round."); //the system congratulates the user on moving on to the next round
- //at this time the for loop moves on to the next question until all questions have been answered or, if the user did not pass a round the program continues below
- }
- }
- if (pass) { //if it exits out of the for loop above and the player has answered all questions correctly (because the pass boolean is still true)
- System.out.println("Congratulations, you have won the game and $1,000,000!"); //the player wins the game and a million dollars!
- }
- else if (walkedAway == false && i > 10) { //if the user loses after the tenth question and hasn't walked away
- System.out.println("Your earnings: $" + prizes[10]); //the user wins the checkpoint of $32,000
- }
- else if (walkedAway == false && i > 5) { //if the user loses after the fifth question but before the tenth and hasn't walked away
- System.out.println("Your earnings: $" + prizes[5]); //the user wins the checkpoint of $1000
- }
- else if (walkedAway == false) { //if the user loses before hitting a money checkpoint (The fifth, $1000 question) and hasn't walked away
- System.out.println("Your earnings: $" + prizes[0]); //the user loses and makes no money
- }
- System.out.print("\nEnter 1 to play again: "); //asks the user if they want to play again
- replay = scanner.nextLine();
- }
- while (replay.equals("1")); //the do while loop runs again if the user decides to play again
- }
- static boolean askQuestion (String question, String choiceA, String choiceB, String choiceC, String choiceD, int correct, int earnings, int prize, int round, String rQuestion, String rChoiceA, String rChoiceB, String rChoiceC, String rChoiceD, int rCorrect) { //beginning of the legendary askQuestions method...
- System.out.println("\nQuestion " + (round + 1) + " (for $" + prize + "):"); //prints to the screen the current round number and the monetary value for the current question
- System.out.println("\n" + question + "\n\n" + "A) " + choiceA + "\n" + "B) " + choiceB + "\n" + "C) " + choiceC + "\n" + "D) " + choiceD + "\n\n" + "E) Walk Away With $" + earnings); //gives the player the 4 multiple choice options along with the chance to walk away
- showLifelines(); //runs the method which displays the life lines available and still remaining
- int choice = acceptAnswer (false); //passes in false because at this stage a life line has not been used as it is the first time asking for input this round, please see the acceptAnswer method first for clarification
- if (choice > 5) { //if the user chose to use a life line in the acceptAnswer method (the value is either 6,7 or 8 so greater than 5)
- System.out.println("You used a lifeline."); //tells the user they have used a life line
- switch (choice) { //a switch determining and running the life line chosen by the user
- case 6: useFiftyFifty (correct); //if the value is 6, it corresponds to the 50/50 life line and runs the 50/50 method
- break;
- case 7: usePollAudience (correct, round); //if the value is 7, it corresponds to the Poll the Audience life line and runs the Poll the Audience method
- break;
- case 8: useChangeQuestion (rQuestion, rChoiceA, rChoiceB, rChoiceC, rChoiceD); //if the value is 8, it corresponds to the Replace Question life line and runs the Replace Question method
- correct = rCorrect;
- break;
- }
- choice = acceptAnswer (true); //passes in true because now an answer must be made if a life line has been used or not
- }
- if (choice >= 1 && choice <= 4) { //if the choice is an attempt at the answer
- if (choice == correct) { //if the choice is the same as the correct answer
- return true; //return a true value back up to the pass boolean to signify the question has been successfully answered
- }
- else { //if the choice is incorrect
- System.out.println("Sorry, that is an incorrect answer."); //tell the user they have answered incorrectly
- return false; //return a false value back up to the pass boolean to signify the game has ended, with the walkedAway set to false
- }
- }
- else { //if the user hasn't answered using a choice they must have walked away
- walkedAway = true; //sets the value of walkedAway to true to tell the program that the user decided to walk away
- System.out.println("You decided to walk away with $" + earnings + "."); //tells the user the amount of money they have won and walked away with
- return false; //returns a false value back up to the pass boolean to tell the program the game has ended
- }
- }
- static char convertToLetter (int num) { //a method which converts a value to a letter, used in the 50/50 life line method
- switch (num) { //switch for the value of num
- case 1: return 'A'; //if the value of num is 1 then it returns the character A
- case 2: return 'B'; //if the value of num is 1 then it returns the character A
- case 3: return 'C'; //if the value of num is 1 then it returns the character A
- default: return 'D'; //if the value of num is 1 then it returns the character A
- }
- }
- static int acceptAnswer (boolean lifelineUsed) { //method which accepts the answer from the user and determines what has been inputed
- for (;;) { //for loop that runs forever until a value is returned
- String input = scanner.nextLine(); //asks user for input (a,b,c,d,e,f or g)
- input = input.toUpperCase(); //changes the input to upper case letters just so tracking the asking value is easier
- if (input.matches("[A-E]+") && input.length() == 1) { //if the input is one of the four choices or walk away
- return ((int) input.charAt(0)) - 64; //returns the asking value of the character inputed and subtracts 64 to get to single digits, value returned should be between 1 and 4 if an answer was chosen and 5 for the walk away
- }
- else if (input.equals("F") && lifelineUsed == false && fiftyFifty) { //if the user inputed to use the life line 50/50
- return ((int) input.charAt(0)) - 64; //gets the asking value of "F" and subtracts 64, value returned should be 6
- }
- else if (input.equals("G") && lifelineUsed == false && pollAudience) { //if the user inputed to use the life line Poll the Audience
- return ((int) input.charAt(0)) - 64; //gets the asking value of "G" and subtracts 64, value returned should be 7
- }
- else if (input.equals("H") && lifelineUsed == false && changeQuestion) { //if the user inputed to use the life line Replace Question
- return ((int) input.charAt(0)) - 64; //gets the asking value of "H" and subtracts 64, value returned should be 8
- }
- else if (input.matches("[F-H]+") && lifelineUsed == true) { //of the input is asking to use a life line but a life line has already been used this round
- System.out.print("Invalid: Lifeline has already been used this round. Try again: "); //notifies the user that a life line has already been used this round
- }
- else System.out.print("Not a valid input. Try again: "); //if the input does not fall into any of the categories it is invalid and the system tells the user that the input is invalid
- }
- }
- static void showLifelines() { //method which decides what life lines to display to the user
- if (fiftyFifty || pollAudience || changeQuestion) { //if any life line is still available
- System.out.println("\nLifelines: "); //the system prints out the life lines subtitle
- }
- if (fiftyFifty) { //if the life line 50/50 is still available
- System.out.println("F) Remove 2 Incorrect Options"); //the system prints out the option of using the 50/50 life line
- }
- if (pollAudience) { //if the life line Poll the Audience is still available
- System.out.println("G) Poll The Audience"); //the system prints out the option of using the Poll Audience life line
- }
- if (changeQuestion) { //if the life line Replace Question is still available
- System.out.println("H) Replace Current Question"); //the system prints out the option of using the Replace Question life line
- }
- }
- static void useFiftyFifty (int correct) { //beginning of a life line method, 50/50
- int[] nums = new int[2]; //creates an array which will store the two removed answers
- fiftyFifty = false; //sets the FiftyFifty boolean to false because the life line has been used
- do { //do while loop, explained at the while part
- nums[0] = rand.nextInt(4) + 1; //randomly generates a number between 1 and 4 (letter choice between A and D)
- }
- while (nums[0] == correct); //runs while the randomly generated number is equal to the correct answer so an incorrect number is sure to be generated
- do { //do while loop, explained at the while part
- nums[1] = rand.nextInt(4) + 1;
- }
- while (nums[1] == correct || nums[1] == nums[0]); //runs while the randomly generated number is equal to the correct answer or if the new random number is equal to the first random number so a BRAND NEW incorrect number is sure to be generated
- System.out.println("Eliminated 2 incorrect answers: " + convertToLetter(nums[0]) + " and " + convertToLetter(nums[1])); //prints out the letter (using the convertToLetter method) of the two eliminated incorrect answers to the user
- }
- static void usePollAudience (int correct, int round) { //beginning of a life line method, Poll the Audience
- int[] percent = new int[4]; //array used to store the generated audience poll percent for each answer
- pollAudience = false; //sets the pollAudience boolean to false because the life line has been used
- percent[0] = rand.nextInt(25) + (15 - round) * 5; //randomly generates the first percentage but making the odds of generating the correct answer greater or lower corresponding to the round number
- percent[1] = rand.nextInt(100 - percent[0]); //generates the percentage for the second choice
- percent[2] = rand.nextInt(100 - percent[0] - percent[1]); //generates the percentage for the third choice
- percent[3] = 100 - (percent[0] + percent[1] + percent[2]); //generates the percentage for the fourth choice
- percent[correct - 1] = percent[0]; // Moves the skewed value to the position of the correct answer
- percent[0] = 100 - (percent[1] + percent[2] + percent[3]); // Recalculates the values to ensure the sum is 100
- System.out.println("We asked members of the audience what they would answer:"); //tells the user that this is what the audience would answer
- System.out.println("A: " + percent[0] + "%\nB: " + percent[1] + "%\nC: " + percent[2] + "%\nD: " + percent[3] + "%"); //prints out the generated audience poll results for each choice
- }
- static void useChangeQuestion (String question, String choiceA, String choiceB, String choiceC, String choiceD) { //beginning of a life line method, Replace Question
- changeQuestion = false; //sets the changeQuestion boolean to false because the life line has been used
- System.out.println("\nTraded in the question for a new one:"); //tells the user they have exchanged their question
- System.out.println(question + "\nA) " + choiceA + "\nB) " + choiceB + "\nC) " + choiceC + "\nD) " + choiceD); //prints the new question out to the user and the appropriate choices as well
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement