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

In this assignment, you will write a jumble game. You will repeatedly pick a ran

ID: 3585887 • Letter: I

Question

In this assignment, you will write a jumble game. You will repeatedly pick a random word, randomly jumble it, and let the user guess what it is. The user will also be able to get hints in case they have trouble figuring out the word. Here is an example run of the program (user input is in red). Note that your program should handle upperor lower-case menu options, and that it should print an error for any invalid menu option. Current puzzle: ayncra Current points for word: 10 Enter (9)uess, new word, (h)int, or (4)uit: h The letter at spot 3 is n Current puzzle: ayncra Current points for word: 5 Enter (9)uess, new word, (h)int, or (4)uit: g Enter your guess : canary You guessed it! Score for word: 5 Total score: 5 Current puzzle: ufrryl Current points for word: 10 Enter (9)uess, new word, (h)int, or (4)uit: g Enter your guess: furyrl Nope , sorry Current puzzle: ufrryl Current points for word: 9 Enter (9)uess, (n)ew word, (h)int, or quit: g Enter your guess: flurry You guessed it! Score for word: 9 Total score: 14 Current puzzle: crsock Current points for word: 10 Enter (9)uess, new word, (h)int, or (4)uit: X Invalid option. Current puzzle: crsock Current points for word: 10 Enter (9)uess, new word, (h)int, or (4)uit: N Current puzzle: adrtre Current points for word: 10 Enter (9)uess, (n)ew word, (h)int, or cauit: Q OOD Final score: 14

Explanation / Answer

import java.util.Scanner;

import java.util.concurrent.ThreadLocalRandom;

//Class WordJumbleGame definition

public class WordJumbleGame

{

//Creates an array of string to store the original words

private static final String[] WORDS = new String[]

{ "superman","jungle","paper","letter","house","computer", "mobile" };

//Main method definition

public static void main(String[] args)

{

//Creates an object of the class WordJumbleGame

WordJumbleGame wjg = new WordJumbleGame();

//Calls the method to start the game

wjg.startGame();

}//End of main method

//Method to display menu, accept user choice and return it

char menu()

{

//Scanner class object created

Scanner sc = new Scanner(System.in);

//To store user choice

char choice;

//Accept user choice

System.out.println("Enter (g)uess, (n)ew word, (h)int, or (q)uit: ");

choice = sc.next().charAt(0);

//Returns user choice

return choice;

}//End of menu method

/* This method is doing the following tasks

* Gets a random word from the WORDS array

* Shuffle the characters for the selected word

* Displays the jumbled word

* Accepts the user guess word

* Repeats till correct guess or user choice is 'Q' or 'q'

* Calculates and displays score

*/

private void startGame()

{

//To store current point initializes it to 10

int currentPoint = 10;

//To store final calculated score

int finalScore = 0;

//To store user choice

char choice;

//Calls the method to return a random word

String original = selectRandomWord();

//Calls the method to shuffle the character of the word

String shuffled = getShuffledWord(original);

//Loops till user choice is not 'q' or 'Q'

while(true)

{

//Displays the shuffled word and current point

System.out.println(" Current puzzle: " + shuffled);

System.out.println("Current points for word: " + currentPoint);

//Calls the method to display menu

choice = menu();

//Switch case begins

switch(choice)

{

case 'g':

case 'G':

//Calls the method to accept user guess word

String userGuess = getUserGuess();

//Checks if user guess word and original word are same

if(original.equalsIgnoreCase(userGuess))

{

//Adds the current score to the final score

finalScore += currentPoint;

//Displays the information

System.out.println("You guessed it! ");

System.out.println("Score for Word: " + currentPoint);

System.out.println("Total Score: " + finalScore);

//Calls the method to return a random word

original = selectRandomWord();

//Calls the method to shuffle the character of the word

shuffled = getShuffledWord(original);

//Reset the currentPoint to 10

currentPoint = 10;

}//End of if

//Otherwise if not equal

else

{

//Displays message

System.out.println("Nope, sorry");

//Subtract one point from the current point

currentPoint--;

}//End of else

break;

case 'n':

case 'N':

//Calls the method to return a random word

original = selectRandomWord();

//Calls the method to shuffle the character of the word

shuffled = getShuffledWord(original);

break;

case 'h':

case 'H':

//Displays the hint

System.out.println("The letter at sopt 3 " + " is " + original.charAt(2));

//Subtracts 5 points from the current point

currentPoint -= 5;

break;

case 'q':

case 'Q':

//Displays the final score

System.out.println("Good Bye! Final Socre: " + finalScore);

System.exit(0);

default:

System.out.println("Invalid Option.");

}//End of switch - case

}//End of while

}//End of method

//Method to accept user guess word and return it

public String getUserGuess()

{

//Scanner class object created

Scanner sn = new Scanner(System.in);

//Accepts user guess

System.out.println("Enter your guess: ");

//Returns user guess

return sn.nextLine();

}//End of method

//Returns the random word selected from WORD array

public String selectRandomWord()

{

//Gets a random word

int rPos = ThreadLocalRandom.current().nextInt(0, WORDS.length);

//Returns random word

return WORDS[rPos];

}//End of method

//Shuffles the characters of the word and returns it

public String getShuffledWord(String original)

{

//Stores the original word in shuffleWord

String shuffledWord = original;

//Calculates the length and stores it

int wordSize = original.length();

//Shuffles the letters 10 times

for(int c = 0; c < 10; c++)

{

//Swap the letters

int index1 = ThreadLocalRandom.current().nextInt(0, wordSize);

int index2 = ThreadLocalRandom.current().nextInt(0, wordSize);

shuffledWord = swap(shuffledWord, index1, index2);

}//End of for loop

//Returns shuffle word

return shuffledWord;

}//End of method

//Method to swap the characters

private String swap(String shuffledWord, int index1, int index2)

{

//Converts the string word to array

char[] charWord = shuffledWord.toCharArray();

//Swapping process

char temp = charWord[index1];

charWord[index1] = charWord[index2];

charWord[index2] = temp;

return new String(charWord);

}//End of method

}//End of class

Sample Run:


Current puzzle: peapr
Current points for word: 10
Enter (g)uess, (n)ew word, (h)int, or (q)uit:
h
The letter at sopt 3 is p

Current puzzle: peapr
Current points for word: 5
Enter (g)uess, (n)ew word, (h)int, or (q)uit:
g
Enter your guess:
paper
You guessed it!
Score for Word: 5
Total Score: 5

Current puzzle: rttlee
Current points for word: 10
Enter (g)uess, (n)ew word, (h)int, or (q)uit:
g
Enter your guess:
liter
Nope, sorry

Current puzzle: rttlee
Current points for word: 9
Enter (g)uess, (n)ew word, (h)int, or (q)uit:
g
Enter your guess:
letter
You guessed it!
Score for Word: 9
Total Score: 14

Current puzzle: eeltrt
Current points for word: 10
Enter (g)uess, (n)ew word, (h)int, or (q)uit:
y
Invalid Option.

Current puzzle: eeltrt
Current points for word: 10
Enter (g)uess, (n)ew word, (h)int, or (q)uit:
q
Good Bye!
Final Socre: 14