import java.util.Scanner; public class WordGame { public static void main(String
ID: 3805907 • Letter: I
Question
import java.util.Scanner;
public class WordGame
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String playAgain = "YES";
while (playAgain.equalsIgnoreCase("YES"))
{
boolean won = playTheGame(keyboard);
if (won)
System.out.println("The word guesser won!");
else
System.out.println("The word guesser lost!");
System.out.println("Do you want to play again?");
playAgain = keyboard.nextLine();
}
}
public static boolean playTheGame(Scanner keyboard)
{
String word; // the word to be guessed
String guess=""; // the current guess, empty so the first
//iteration doesn't break
System.out.println("Enter a word for your opponent to guess");
word = keyboard.nextLine();
String shuffled = shuffle(word);
// Show the shuffled word one character at a time, allowing the
// user to guess
for(int index = 1; index<shuffled.length()+1
&& guess.equalsIgnoreCase(word)== false;++index)
{
// This shows one more character each time through
System.out.println("Some characters are: " +
shuffled.substring(0,index));
System.out.println("What is your guess?");
guess = keyboard.nextLine();
}
// If the guess is the word, the guesser won
return guess.equalsIgnoreCase(word);
}
public static String shuffle(String letters)
{
// StringBuilders are like Strings,except we can edit characters
StringBuilder result = new StringBuilder(letters);
// for each letter, swap it with a randomly chosen letter
for(int index = 0; index<result.length(); ++index)
{
// Find the random location--all parentheses are necessary
int rand = (int)(Math.random()*result.length());
// Find the letter at the next location
char c = result.charAt(index);
// Swap the letter at the random location with the next one
result.setCharAt(index, result.charAt(rand));
result.setCharAt(rand, c);
}
// Turn the StringBuidler back into a String object
return result.toString();
}
}
1) In the program above, list all of the identifiers that fall into the categories below for each method. Remember that a single identifier may have multiple roles. Do not consider arguments in System.out.println() methods.
Parameters
main:
playTheGame:
shuffle:
Arguments
main:
playTheGame:
shuffle:
Local variables
main:
playTheGame:
shuffle:
Explanation / Answer
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class Game {
private static final String[] WORDS_DATABASE = new String[] {
"Apple","Grapes","Mango","book","computer","tree"
};
public static void main(String[] args) {
Game jg = new Game();
jg.startGame();
}
private void startGame() {
int numberOfGuesses = 0;
String original = selectRandomWord();
String shuffled = getShuffledWord(original);
boolean gameOn = true;
while(gameOn) {
System.out.println("Shuffled word is: "+shuffled);
numberOfGuesses++;
String userGuess = getUserGuess();
if(original.equalsIgnoreCase(userGuess)) {
System.out.println("Congratulations! You found the word in "+numberOfGuesses+" guesses");
gameOn = false;
}else {
System.out.println("Sorry, Wrong answer");
}
}
}
public String getUserGuess() {
Scanner sn = new Scanner(System.in);
System.out.println("Please type in the original word: ");
return sn.nextLine();
}
public String selectRandomWord() {
int rPos = ThreadLocalRandom.current().nextInt(0, WORDS_DATABASE.length);
return WORDS_DATABASE[rPos];
}
public String getShuffledWord(String original) {
String shuffledWord = original;
int wordSize = original.length();
int shuffleCount = 20;
for(int i=0;i<shuffleCount;i++) {
//swap letters in two indexes
int position1 = ThreadLocalRandom.current().nextInt(0, wordSize);
int position2 = ThreadLocalRandom.current().nextInt(0, wordSize);
shuffledWord = swapCharacters(shuffledWord,position1,position2);
}
return shuffledWord;
}
private String swapCharacters(String shuffledWord, int position1, int position2) {
char[] charArray = shuffledWord.toCharArray();
// Replace with a "swap" function, if desired:
char temp = charArray[position1];
charArray[position1] = charArray[position2];
charArray[position2] = temp;
return new String(charArray);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.