Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Political-themed Hangman Game Please finish my Java code to give the user two options A and B. The code already randomly picks a word from

Political-themed Hangman Game

Please finish my Java code to give the user two options A and B. The code already randomly picks a word from a string array, hides the letters using symbols, then unveils commonly used letter RSTLNE as a hint. The next part should output a statement asking the player if they would like to A) guess the entire word for no cash prize or B) Guess letters until the puzzle is solved. For both options, the player will encounter "Game over" message if three wrong answers are given.

Choice A

//Guess the word //Allow user to type in up to 3 wrong guesses //Three wrong guesses results in Game Over //Correct guess results in entire word being revealed and output "You Win!"

Choice B //Guess letters one at a time until the puzzle if solved //Three wrong answers results in Game Over //Each correct guess results in an asterisk being replaced by the letter until the entire word is revealed //Each correct letter also results in a the player winning a cash prize for each correct letter guessed. Ex: if the random prize was $10, then each correct guess of of the letter "C" in "Democratic" would be 10*Letter= 2

// //Correct guess results in entire word being revealed and output "You Win!" + cash prize value

//Begin Program

import java.util.*; import java.lang.*; import java.util.Random;

public class PolicalWordGuess { static String wordList[] = { "GOVERNMENT" , "POLITICO" , "LEGISLATURE" , "DEMOCRATIC" , "REPUBLICAN" , "SENATE" , "BIPARTISAN" , "REPUBLICAN" , "LIBERTY" , "REPRESENTATION"}; static String cashPrize[] = {"$10", "$15" , "$20" , "$25", "$30"}; static Scanner scan = new Scanner(System.in); static Random random = new Random(); static int index = random.nextInt(wordList.length); static String str1 = wordList[index]; static String results = "";

public static void main(String args[]) { System.out.println( " Word: " + str1.replaceAll("[A-Za-z0-9]", "*")); System.out.println( " Please press enter to see the prize."); scan.nextLine();

int index2 = random.nextInt(cashPrize.length);

String str2 = cashPrize[index2]; System.out.println( " Prize: " + str2); System.out.println( " Please hit press for a hint"); scan.nextLine(); for (int i = 0; i < str1.length(); i++) { if (str1.charAt(i) == 'S' || str1.charAt(i) == 's') { results = results + str1.charAt(i); } else if (str1.charAt(i) == 'T' || str1.charAt(i) == 't') { results = results + str1.charAt(i); } else if (str1.charAt(i) == 'R' || str1.charAt(i) == 'r') { results = results + str1.charAt(i); } else if (str1.charAt(i) == 'L' || str1.charAt(i) == 'l') { results = results + str1.charAt(i); } else if (str1.charAt(i) == 'N' || str1.charAt(i) == 'n') { results = results + str1.charAt(i); } else if (str1.charAt(i) == 'E' || str1.charAt(i) == 'e') { results = results + str1.charAt(i); } else if (String.valueOf(str1.charAt(i)).equals(":")) { results = results + str1.charAt(i); } else if (str1.charAt(i) == ' ' || str1.charAt(i) == ' ') { results = results + str1.charAt(i); } else { results = results + "_"; } } System.out.println(" Hint: "); System.out.println(" " + results); //Gives users two options A or B. This is the part that need to be completed. Please feel free to change anything after this point. System.out.println(" Select A) Solve the puzzle for $0 OR B) Guess a letter. You only get three guesses to solve the puzzle. Failure to solve results in Game Over! "); int choice = scan.nextInt(); if (choice == 'A') System.out.println("Please enter a letter: "); //Guess the word //Allow user to type in up to 3 guesses //Three wrong guesses results in Game Over //Correct guess results in entire word being revealed

if (choice == 'B') System.out.println("Please solve the puzzle: "); //Guess letters one at a time until the puzzle if solved //Three wrong answers results in Game Over //Each correct guess results in an asterisk being replaced by a letter until the entire word is revealed //Each correct letter also results in a the player winning a cash prize for each correct letter guessed. Ex: if the random prize was $10, then each correct guess of of the letter "C" in "Democratic" would be 10*Letter= 20 } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions