Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.21 KB | None | 0 0
  1. package millionaire;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4. public class Millionaire {
  5.  
  6. static Random rand = new Random(); //new random number generator
  7. static Scanner scanner = new Scanner(System.in); //new scanner
  8. 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
  9. static boolean walkedAway = false; // boolean that will track if the player chooses to walk away or not
  10.  
  11. public static void main(String[] args) {
  12.  
  13. 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
  14. "What leaf is featured on the Canadian flag?",
  15. "How many stars are on the American flag?",
  16. "In what country would you find the cities Glasgow and Edinburgh?",
  17. "What country has the second largest population in the world?",
  18. "Mount Fuji is the highest mountain in what country?",
  19. "What country is home to the Great Barrier Reef?",
  20. "Zurich is the largest city in what country?",
  21. "In what country would you find the islands Santorini and Mykonos?",
  22. "Excluding Gibraltar, how many countries does Spain border?",
  23. "One of these countries has a border ONLY with South Africa. Which one?",
  24. "Which country would you need to cross to get from Ecuador to Chile?",
  25. "In what country is the Yangtze River found?",
  26. "How many countries are there ending in 'stan'",
  27. "What country was known as Ceylon until 1972?"};
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. String[] Rquestions = {"How many continents are there?", //array which holds 3 different questions of varied difficulty for the replace question life line
  34. "How many more countries are there in the UK than in Great Britain?",
  35. "What is the sixth largest country in terms of land mass?"};
  36. String[] RchoiceA = {"6", "1", "China"}; //choice letter A for each corresponding question in the replace questions array
  37. String[] RchoiceB = {"7", "2", "Brazil"}; //choice letter B for each corresponding question in the replace questions array
  38. String[] RchoiceC = {"8", "3", "Australia"}; //choice letter C for each corresponding question in the replace questions array
  39. String[] RchoiceD = {"9", "4", "India"}; //choice letter D for each corresponding question in the replace questions array
  40. 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
  41. 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
  42. boolean pass; //boolean that will track whether or not the player has correctly answered the question and passes on to the next round
  43. String replay; //will be used to store an answer whether the player wants to play the game again
  44. int i; //will be used for for loops and other calculations
  45.  
  46. do { //do while loop, explained with the while portion down below
  47.  
  48. 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
  49.  
  50. pass = true; //sets pass to true so that the first question can be played
  51. 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
  52. 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
  53. 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
  54.  
  55. 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
  56.  
  57. 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]);
  58. // ^^ 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)
  59.  
  60. if (pass && i < 14) { //each time user answers the question correct and it is not the last question
  61.  
  62. System.out.println("Great job, you move on to the next round."); //the system congratulates the user on moving on to the next round
  63. //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
  64. }
  65. }
  66.  
  67. 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)
  68.  
  69. System.out.println("Congratulations, you have won the game and $1,000,000!"); //the player wins the game and a million dollars!
  70. }
  71.  
  72. else if (walkedAway == false && i > 10) { //if the user loses after the tenth question and hasn't walked away
  73.  
  74. System.out.println("Your earnings: $" + prizes[10]); //the user wins the checkpoint of $32,000
  75. }
  76.  
  77. else if (walkedAway == false && i > 5) { //if the user loses after the fifth question but before the tenth and hasn't walked away
  78.  
  79. System.out.println("Your earnings: $" + prizes[5]); //the user wins the checkpoint of $1000
  80. }
  81.  
  82. else if (walkedAway == false) { //if the user loses before hitting a money checkpoint (The fifth, $1000 question) and hasn't walked away
  83.  
  84. System.out.println("Your earnings: $" + prizes[0]); //the user loses and makes no money
  85. }
  86.  
  87. System.out.print("\nEnter 1 to play again: "); //asks the user if they want to play again
  88. replay = scanner.nextLine();
  89. }
  90. while (replay.equals("1")); //the do while loop runs again if the user decides to play again
  91. }
  92.  
  93. 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...
  94.  
  95. System.out.println("\nQuestion " + (round + 1) + " (for $" + prize + "):"); //prints to the screen the current round number and the monetary value for the current question
  96. 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
  97.  
  98. showLifelines(); //runs the method which displays the life lines available and still remaining
  99.  
  100. 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
  101.  
  102. 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)
  103.  
  104. System.out.println("You used a lifeline."); //tells the user they have used a life line
  105.  
  106. switch (choice) { //a switch determining and running the life line chosen by the user
  107.  
  108. case 6: useFiftyFifty (correct); //if the value is 6, it corresponds to the 50/50 life line and runs the 50/50 method
  109. break;
  110.  
  111. 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
  112. break;
  113.  
  114. 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
  115. correct = rCorrect;
  116. break;
  117. }
  118.  
  119. choice = acceptAnswer (true); //passes in true because now an answer must be made if a life line has been used or not
  120. }
  121.  
  122. if (choice >= 1 && choice <= 4) { //if the choice is an attempt at the answer
  123.  
  124. if (choice == correct) { //if the choice is the same as the correct answer
  125.  
  126. return true; //return a true value back up to the pass boolean to signify the question has been successfully answered
  127. }
  128.  
  129. else { //if the choice is incorrect
  130.  
  131. System.out.println("Sorry, that is an incorrect answer."); //tell the user they have answered incorrectly
  132. return false; //return a false value back up to the pass boolean to signify the game has ended, with the walkedAway set to false
  133. }
  134. }
  135.  
  136. else { //if the user hasn't answered using a choice they must have walked away
  137.  
  138. walkedAway = true; //sets the value of walkedAway to true to tell the program that the user decided to walk away
  139. System.out.println("You decided to walk away with $" + earnings + "."); //tells the user the amount of money they have won and walked away with
  140. return false; //returns a false value back up to the pass boolean to tell the program the game has ended
  141. }
  142. }
  143.  
  144. static char convertToLetter (int num) { //a method which converts a value to a letter, used in the 50/50 life line method
  145.  
  146. switch (num) { //switch for the value of num
  147.  
  148. case 1: return 'A'; //if the value of num is 1 then it returns the character A
  149. case 2: return 'B'; //if the value of num is 1 then it returns the character A
  150. case 3: return 'C'; //if the value of num is 1 then it returns the character A
  151. default: return 'D'; //if the value of num is 1 then it returns the character A
  152. }
  153. }
  154.  
  155. static int acceptAnswer (boolean lifelineUsed) { //method which accepts the answer from the user and determines what has been inputed
  156.  
  157. for (;;) { //for loop that runs forever until a value is returned
  158.  
  159. String input = scanner.nextLine(); //asks user for input (a,b,c,d,e,f or g)
  160. input = input.toUpperCase(); //changes the input to upper case letters just so tracking the asking value is easier
  161.  
  162. if (input.matches("[A-E]+") && input.length() == 1) { //if the input is one of the four choices or walk away
  163.  
  164. 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
  165. }
  166.  
  167. else if (input.equals("F") && lifelineUsed == false && fiftyFifty) { //if the user inputed to use the life line 50/50
  168.  
  169. return ((int) input.charAt(0)) - 64; //gets the asking value of "F" and subtracts 64, value returned should be 6
  170. }
  171.  
  172. else if (input.equals("G") && lifelineUsed == false && pollAudience) { //if the user inputed to use the life line Poll the Audience
  173.  
  174. return ((int) input.charAt(0)) - 64; //gets the asking value of "G" and subtracts 64, value returned should be 7
  175. }
  176.  
  177. else if (input.equals("H") && lifelineUsed == false && changeQuestion) { //if the user inputed to use the life line Replace Question
  178.  
  179. return ((int) input.charAt(0)) - 64; //gets the asking value of "H" and subtracts 64, value returned should be 8
  180. }
  181.  
  182. 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
  183.  
  184. 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
  185. }
  186.  
  187. 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
  188. }
  189. }
  190.  
  191. static void showLifelines() { //method which decides what life lines to display to the user
  192.  
  193. if (fiftyFifty || pollAudience || changeQuestion) { //if any life line is still available
  194.  
  195. System.out.println("\nLifelines: "); //the system prints out the life lines subtitle
  196. }
  197.  
  198. if (fiftyFifty) { //if the life line 50/50 is still available
  199.  
  200. System.out.println("F) Remove 2 Incorrect Options"); //the system prints out the option of using the 50/50 life line
  201. }
  202.  
  203. if (pollAudience) { //if the life line Poll the Audience is still available
  204.  
  205. System.out.println("G) Poll The Audience"); //the system prints out the option of using the Poll Audience life line
  206. }
  207.  
  208. if (changeQuestion) { //if the life line Replace Question is still available
  209.  
  210. System.out.println("H) Replace Current Question"); //the system prints out the option of using the Replace Question life line
  211. }
  212. }
  213.  
  214. static void useFiftyFifty (int correct) { //beginning of a life line method, 50/50
  215.  
  216. int[] nums = new int[2]; //creates an array which will store the two removed answers
  217.  
  218. fiftyFifty = false; //sets the FiftyFifty boolean to false because the life line has been used
  219.  
  220. do { //do while loop, explained at the while part
  221.  
  222. nums[0] = rand.nextInt(4) + 1; //randomly generates a number between 1 and 4 (letter choice between A and D)
  223. }
  224. 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
  225.  
  226. do { //do while loop, explained at the while part
  227.  
  228. nums[1] = rand.nextInt(4) + 1;
  229. }
  230. 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
  231.  
  232. 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
  233. }
  234.  
  235. static void usePollAudience (int correct, int round) { //beginning of a life line method, Poll the Audience
  236.  
  237. int[] percent = new int[4]; //array used to store the generated audience poll percent for each answer
  238.  
  239. pollAudience = false; //sets the pollAudience boolean to false because the life line has been used
  240.  
  241. 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
  242. percent[1] = rand.nextInt(100 - percent[0]); //generates the percentage for the second choice
  243. percent[2] = rand.nextInt(100 - percent[0] - percent[1]); //generates the percentage for the third choice
  244. percent[3] = 100 - (percent[0] + percent[1] + percent[2]); //generates the percentage for the fourth choice
  245.  
  246. percent[correct - 1] = percent[0]; // Moves the skewed value to the position of the correct answer
  247. percent[0] = 100 - (percent[1] + percent[2] + percent[3]); // Recalculates the values to ensure the sum is 100
  248.  
  249. System.out.println("We asked members of the audience what they would answer:"); //tells the user that this is what the audience would answer
  250. 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
  251. }
  252.  
  253. static void useChangeQuestion (String question, String choiceA, String choiceB, String choiceC, String choiceD) { //beginning of a life line method, Replace Question
  254.  
  255. changeQuestion = false; //sets the changeQuestion boolean to false because the life line has been used
  256.  
  257. System.out.println("\nTraded in the question for a new one:"); //tells the user they have exchanged their question
  258. 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
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement