use java code thx On the TV game, there are three players who try and guess a ph
ID: 3854721 • Letter: U
Question
use java code thx
On the TV game, there are three players who try and guess a phrase. They get to spin a wheel with different dollar amounts (e.g., $200, $500, $1000, etc). Then they guess a letter. If the letter is in the hidden phrase they get the value that they spun multiplied by the number of the guessed letter in the phrase. For example, if the person spun $200 and guessed 'S' for the hidden phrase "Star Wars", the person would win $400 because there were two letters 'S' in the phrase. The winner of the game is the person who guesses the phrase first and they win all the money that they accumulated during the game.
You are going to write a very simple text version of this game. There will only be one player and three dollar amounts ($200, $500 and $1000) that the player will 'spin' before guessing a letter. The player's jackpot (the amount of money they win on each guess) will keep increasing while they guess letters up to a maximum of 6 letter guesses. If they select a letter that is not in the phrase they will not win any money on that turn (but it counts as a turn) and they cannot try to guess the phrase. The game continues until one of the following occurs:
the player correctly guesses the phrase and they win their 'jackpot', or
the player incorrectly guesses the phrase in which case the game is over, the phrase will be revealed and the player does not win any money, or
the player uses their last of 6 letter guesses - the player gets to select no more than 6 letters, and if they cannot guess the phrase after the 6th letter guess then the game is over, the phrase is revealed, and the player does not win any money.
The Game class will have the following attributes:
The secret phrase (an array of char)
The disguised phrase (another array of char), in which each unknown letter in the secret phrase is replaced with a blank line ( _ ). For example, if the secret phrase is computer science and the letters c and s have been guessed, the disguised phrase would print as
C_ _ _ _ _ _ _ SC _ _ _ C_
The number of guesses made
The number of incorrect guesses
The jackpot amount
The Game class will have the following methods:
spinWheel( ) which returns a random int of 100, 200 or 500
guessLetter(c) guesses that character c (letter c) in the phrase and returns the number of times that letter is in the phrase.
GetDisguisedPhrase( )returns a String containing correctly guessed letters in the correct positions and unknown letters replaced with_.
getSecretPhrase( ) returns the secret phrase.
increaseJackpot(int amt) which increases the jackpot by the amount that turn (number of times the guessed letter is in the phrase x the money amount from the spin)
getGuessCount( ) returns the number of guesses made.
isFound( ) returns true if the hidden phrase has been discovered.
Appropriate constructor and get and set methods (and any others that make sense). Implement the Game class.
Next write a demo class that demonstrates the game. For this class, first create an array of song titles (these titles can be hardcoded as a String array). Select one song title at random from the array as the secret phrase. The following program shows you how to use java.util.Random to select a phrase at random from an array of song titles.
import java.util.Scanner; import java.util.Random; public class RandomWord
{
public static void main(String[] args)
{
String[] songs = {"shake it off", "satisfaction", "mr jones", "uptown funk"}; //note this is a very short list as an example
Random rand = new Random();
int i = rand.nextInt(songs.length); System.out.println(songs[i]);
}
}
Note: You can also use the Random class to determine the money value from the spin (200, 500, or 1000).
Some pointers:
Keep track of the letters that were guessed (correctly or incorrectly). If the user guesses a letter that they have already guessed, it still decreases their letter guesses. Also, make sure that your program can handle both upper and lower case letters.
You should keep a limit (6) on the number of letter guesses, and quit the game if the user doesn’t guess the phrase within the limit. After the sixth letter guess, if the user has not able to guess the phrase the game is over and the user loses (wins no money).
Recall the conversion options between a char array and a String:
String s = new String(phrase);
will convert a char array phrase into a String s.
Similarly,
char[] phrase = s.toCharArray();
will convert a String s into a char array phrase.
You can use the following template for your main method. You should replace the phrase array of songs to a set of song titles of your choice (use at least 8).
import java.util.Scanner; import java.util.Random; public class GameDemo
{
public static void main(String[] args)
{
String[] songs = {"shake it off ", "satisfaction", "mr jones", "uptown funk"}; //note this is a very short list as an example
Random rand = new Random();
int i = rand.nextInt(songs.length); String secretword = songs[i];
//continue the code here.
}
}
Round 3: After spinning the wheel, you got $1000. 1 ase gue s s a letter : s+' There is/are 1 s.. Here's how a sample run of what th ike: e program would look Welcome to the Guessing Game. The topic is song titles. You have 6 guesses. Your puzzle has the following letters: Round 1: After spinning the wheel, you got $200. Please guess a letter: e There is/are 1 e.* Do you know the song? (y or n): n. Do you know the song? (y or n) ne Your jackpot is $200- You have 5 guesses left. Your jackpot is $1200 You have 3 guesses left. Round 2:- After spinning the wheel, you got $500. Peas guess a letter av There is no a. Round 4:- After spinning the wheel, you got 1000. Please guess a letter a Too bad - you already guessed a and there is no a. Do you know the song? (y or n): n* Your jackpot is $200- You have 4 guesses left.Explanation / Answer
//Game.java
import java.util.Random;
public class Game{
//instance variables
private char[] lettersEntered = new char [6];
private char [] secretPhrase;
private int guessesMade;
public int jackpotAmount= 0;
private char lastLetterGuessMade;
private int guessesLeft;
char [] disguisedPhrase = new char[100];
private String disguisedPhraseString="";
private int guessesInWord;
private int guessesOf1stInWord;
private int guessesOf2ndInWord;
private int guessesOf3rdInWord;
private int guessesOf4thInWord;
private int guessesOf5thInWord;
private int guessesOf6thInWord;
//constructer
public Game(String secretPhrase){
//creates Game object, sets guessesMade to 0 and guessesLeft to 6.
//sets secretPhrase, and all correct guesses of each letter guesses to 0.
guessesMade = 0;
guessesLeft = 6;
this.secretPhrase = secretPhrase.toUpperCase().toCharArray();
guessesOf1stInWord=0;
guessesOf2ndInWord=0;
guessesOf3rdInWord=0;
guessesOf4thInWord=0;
guessesOf5thInWord=0;
guessesOf6thInWord=0;
}
//accessor methods
public int getJackpotAmount(){//gets total jackpot amount
return jackpotAmount;
}
public char getLastCharEntered(){//returns last character entered
return lastLetterGuessMade;
}
public int getGuessesForLetter(){
//returns how many times a certain letter is correct
return guessLetter(getLastCharEntered());
}
public int getGuessCount(){//returns the guesses already made
return guessesMade;
}
public int getGuessesLeft(){//returns the guesses user has left
return guessesLeft;
}
public String getSecretPhraseString(){//returns the secretPhrase has a string
String s = new String(secretPhrase);
return s;
}
public char getLastLetterGuessed(int i){//returns certain letter guesses, depending on input
return lettersEntered[i];
}
//mutator methods
public void setLastLetterGuessMade(char c){//sets last letter guess
lastLetterGuessMade = c;
}
public void setGuessesMade(){//increments guesses made by one
guessesMade++;
}
//Other methods
public int spinWheel(){//returns a int randomly that is 100, 200 or 500
Random rand = new Random();
int i = rand.nextInt(3);
if(i==0)
return 100;
else if(i==1)
return 200;
else
return 500;
}
public int guessLetter(char c){
//sees if char c is in the secret word, and if it is it will increase counter by 1
//It will then return the counter
int counter = 0;
for(int i=0; i<secretPhrase.length; i++){
if(c==secretPhrase[i])
counter++;
}
return counter;
}
public char[] getSecretPhrase(){//returns secretPhrase as a char array
return secretPhrase;
}
public String getDisguisedPhrase(){
//displays to the user what the clue of the word is by placing an underscore for
//every letter, and a space for every space in secretPhrase
for(int i=0; i<secretPhrase.length; i++){
if(secretPhrase[i]==' '){
disguisedPhrase[i] = ' ';
disguisedPhraseString += disguisedPhrase[i] + " ";
}
else{
disguisedPhrase[i] = '_';
disguisedPhraseString += "" + disguisedPhrase[i] + " " ;
}
}
disguisedPhraseString = disguisedPhraseString;
return " " + disguisedPhraseString +" ";
}
public void increaseJackpot(int amt, int i){
/*This method will increase the jackpot amount accordingly.
This method contains a for loop, within a while loop. The for loop will
increase 'count' by one every time that the character that is meant to be tested
(indicated by i), and the jackpot will then be equal to the 'count' value
multiplied by 'amt' in paramaters.
The while loop ensures that it will be broken, therefore ensuring that the
for loop will never happen, if ANY of the letters entered are the same as any of the
other letters entered. This is to ensure that the jackpot will not increase
for letters already entered.
*/
boolean state = true;
while(state){
int count = 0;
int x = i;
if(getGuessCount() == 2 && (getLastLetterGuessed(1) == getLastLetterGuessed(0)))
break;
if(getGuessCount() == 3 && (getLastLetterGuessed(2) == getLastLetterGuessed(0) ||
getLastLetterGuessed(2) == getLastLetterGuessed(1)))
break;
if(getGuessCount() == 4 && (getLastLetterGuessed(3) == getLastLetterGuessed(0) ||
getLastLetterGuessed(3) == getLastLetterGuessed(1) || getLastLetterGuessed(3) == getLastLetterGuessed(2)))
break;
if(getGuessCount() == 5 && (getLastLetterGuessed(4) == getLastLetterGuessed(0) ||
getLastLetterGuessed(4) == getLastLetterGuessed(1) || getLastLetterGuessed(4) == getLastLetterGuessed(2)
|| getLastLetterGuessed(4) == getLastLetterGuessed(3)))
break;
if(getGuessCount() == 6 && (getLastLetterGuessed(5) == getLastLetterGuessed(0) ||
getLastLetterGuessed(5) == getLastLetterGuessed(1) || getLastLetterGuessed(5) == getLastLetterGuessed(2)
|| getLastLetterGuessed(5) == getLastLetterGuessed(3) || getLastLetterGuessed(5) == getLastLetterGuessed(4)))
break;
for(int j=0; j<getSecretPhrase().length; j++){
if(getLastLetterGuessed(i)==getSecretPhrase()[j])
count++;
}
jackpotAmount += (amt * count);
state = false;//ends loop if not broken
}
}
public boolean isFound(){
//method converts both secretPhrase and disguised phrase arrays to strings
String secretPhraseOfCharArr = "";
String disguisedPhraseOfCharArr = "";
for(int i=0; i<secretPhrase.length; i++){
secretPhraseOfCharArr += "" + secretPhrase[i];
}
for(int i=0; i<disguisedPhrase.length; i++){
disguisedPhraseOfCharArr += "" + disguisedPhrase[i];
}
//method then determines if the disguised phrase is the same as the secretPhrase
//and returns true or false
if(secretPhraseOfCharArr.equals(disguisedPhraseOfCharArr))
return true;
else
return false;
}
public boolean guessWord(String attemptAtPuzzle){
//If user attemps to guess the entire word, this will return whether or not
//the user is correct
String CharToString = new String(getSecretPhrase());
if(attemptAtPuzzle.equals(CharToString))
return true;
else
return false;
}
private void addToLettersEntered(char guess, int i){
//sets the lettersEntered char array index of i to char
lettersEntered[i] = guess;
}
public void findCorrectlyGuessedLetters(char guess, int i){
//This method will display to the user which of their letters guessed are were correct.
guessesMade++;
addToLettersEntered(guess, i);
String toDisplayToUser = "";
int counter=0; //keeps track of how many characters are in toDisplayToUser
boolean keepGoing = true;
//For loop will run while keepGoing is true
while(keepGoing){
for(int it=0; it<getSecretPhrase().length; it++){
if(counter == getSecretPhrase().length){
//ensures loop will be end if counter is equal to the length of the secretPhrase array
keepGoing = false;
}
if(secretPhrase[it]==' '){
//if character at index 'it' is a space, it will add two spaces to display
toDisplayToUser += " ";
counter = counter + 2;
}
else if(lettersEntered[0] == secretPhrase[it]){
//if first char entered is equal to index of secretPhrase being tested,
//it will be added to 'toDisplayToUser', counter and guessesOf1stWord will be increased
toDisplayToUser += lettersEntered[0];
counter++;
guessesOf1stInWord++;
}
else if(lettersEntered[1] == secretPhrase[it]){
//if second char entered is equal to index of secretPhrase being tested,
//it will be added to 'toDisplayToUser', counter and guessesOf2ndWord will be increased
toDisplayToUser += lettersEntered[1];
counter++;
guessesOf2ndInWord++;
}
else if(lettersEntered[2] == secretPhrase[it]){
//if third char entered is equal to index of secretPhrase being tested,
//it will be added to 'toDisplayToUser', counter and guessesOf3rdWord will be increased
toDisplayToUser += lettersEntered[2];
counter++;
guessesOf3rdInWord++;
}
else if(lettersEntered[3] == secretPhrase[it]){
//if fourth char entered is equal to index of secretPhrase being tested,
//it will be added to 'toDisplayToUser', counter and guessesOf4thWord will be increased
toDisplayToUser += lettersEntered[3];
counter++;
guessesOf4thInWord++;
}
else if(lettersEntered[4] == secretPhrase[it]){
//if fifth char entered is equal to index of secretPhrase being tested,
//it will be added to 'toDisplayToUser', counter and guessesOf5thWord will be increased
toDisplayToUser += lettersEntered[4];
counter++;
guessesOf5thInWord++;
}
else if(lettersEntered[5] == secretPhrase[it]){
//if sixth char entered is equal to index of secretPhrase being tested,
//it will be added to 'toDisplayToUser', counter and guessesO6thWord will be increased
toDisplayToUser += lettersEntered[5];
counter++;
guessesOf6thInWord++;
}
else{
//otherwise print '_ '
toDisplayToUser += "_ ";
counter = counter + 2;
}
}
//The following if else statements will display only one message indicating how many times
//the last letter guessed was in the secretPhrase. It will only print a message for this if
//the guessesMade are what they are where indicated (to work for all rounds).
//If there is more than 0 of the particular letter want being test at appropiate time, it will
//send arguments to the letInPhrase method, which will return how many times the letter appears in
//the word. Other wise, it will say there are none of these left.
if(guessesMade == 1 && guessesOf1stInWord != 0){
letInPhrase(guessesOf1stInWord, 0);
}
else if(guessesMade == 1){
System.out.print("Sorry, there are no " + getLastLetterGuessed(0) + "'s. ");
}
if(guessesMade == 2 && guessesOf2ndInWord != 0){
letInPhrase(guessesOf2ndInWord, 1);
}
else if(guessesMade == 2){
System.out.print("Sorry, there are no " + getLastLetterGuessed(1) + "'s. ");
}
if(guessesMade == 3 && guessesOf3rdInWord != 0){
letInPhrase(guessesOf3rdInWord, 2);
}
else if(guessesMade == 3){
System.out.print("Sorry, there are no " + getLastLetterGuessed(2) + "'s. ");
}
if(guessesMade == 4 && guessesOf4thInWord != 0){
letInPhrase(guessesOf4thInWord, 3);
}
else if(guessesMade == 4){
System.out.print("Sorry, there are no " + getLastLetterGuessed(3) + "'s. ");
}
if(guessesMade == 5 && guessesOf5thInWord != 0){
letInPhrase(guessesOf5thInWord, 4);
}
else if(guessesMade == 5){
System.out.print("Sorry, there are no " + getLastLetterGuessed(4) + "'s. ");
}
if(guessesMade == 6 && guessesOf6thInWord != 0){
letInPhrase(guessesOf6thInWord, 5);
}
else if(guessesMade == 6){
System.out.print("Sorry, there are no " + getLastLetterGuessed(5) + "'s. ");
}
System.out.print(toDisplayToUser);//displays message
keepGoing = false;//ends loop
}
}
public void letInPhrase(int num, int i){
//displays how many times the last letter guessed is in the secretPhrase
if(num>0){
System.out.print(" There is/are " + num + " " + getLastLetterGuessed(i) + ". ");
}
}
}
==============================================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.