Question

I am trying to make a word guessing game on java, where there are four letters...

I am trying to make a word guessing game on java, where there are four letters and you have 10 guesses to try and guess the letter combination. When I try to play, it says every combination of letters I enter is correct.

For example:

"You have 10 guesses left.
Enter your guess:
wxyz
There are 4 correct letter placements.
Congrats!"

I emailed my professor and she said one of my methods is wrong because I'm only checking for specific instances of w x y and z instead of any combination of letters. I'm not sure how to fix it so that it checks for every combination.

Here is my code for the method:

public int play(String str){
int w = 0;
int x = 0;
int y = 0;
int z = 0;
  
char lett;
int total;
  
remainingGuesses = remainingGuesses - 1;
  
for(int wx = 0; wx < str.length(); wx++) {
lett = str.charAt(wx);
  
if(lett == 'w' || lett == 'W') {w = 1;}
if(lett == 'x'|| lett == 'X') {x = 1;}
if(lett == 'y'|| lett == 'Y') {y = 1;}
if(lett == 'z'|| lett == 'Z') {z = 1;}
  
}
total = w + x + y + z;
wordFound = true;

return total;
}

Could someone give me the correct code, or at least a hint?

Reposting because the first expert who answered misunderstood my question. I'm looking for code that checks for every combination of the four letters, NOT for specific instances.

To the expert who commented: the word is randomly generated, so I can't provide a specific instance of a case that's correct or incorrect. Do you want the entire code?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class WordGuess {
  
private static final String FILE = "words.txt";
  
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
  
// read all words from file
ArrayList<String> words = readWords(FILE);
  
// get the length of the list
int nWords = words.size();
  
// generate a random number between 0 and nWords to pick a random word
Random rand = new Random();
int pick = 0 + rand.nextInt(nWords);
while(pick < 0 || pick > nWords)
{
pick = 0 + rand.nextInt(nWords);
}
  
// get the word of the game
String quesWord = words.get(pick).toLowerCase();
ArrayList<String> userWord = new ArrayList<>();
for(int i = 0; i < 4; i++)
{
userWord.add("-");
}
  
// prompt user until all the 4 blanks are covered
int guesses = 0;
do
{
System.out.println("Word status: " + getWord(userWord));
System.out.print("Enter a character (a-z): ");
char inp = Character.toLowerCase(sc.nextLine().charAt(0));
  
// loop to constantly prompt user if any other character is encountered
while(inp < 'a' || inp > 'z')
{
System.out.print("Invalid input!\n\nEnter a character (a-z): ");
inp = Character.toLowerCase(sc.nextLine().charAt(0));
}
  
// if the input guess is wrong
if(!quesWord.contains(inp + ""))
{
guesses++;
System.out.println("Wrong guess!\nGuesses: " + guesses + " (Max - 10 guesses)" + "\n");
}
else
{
// correct guess
guesses++;
System.out.println("You made a correct guess!\nGuesses: " + guesses + " (Max - 10 guesses)" + "\n");
  
// get the position of the character in the original word
int actualPosition = quesWord.indexOf(inp);
  
// now, set the character at the above index of the userWord list
userWord.set(actualPosition, inp + "");
}
  
// if the user word is complete or user excdeeded the number of guesses limit (10 guesses)
if(!userWord.contains("-") || guesses == 10)
{
if(!userWord.contains("-"))
System.out.println("You completed the game in " + guesses + " guesses!\n"
+ "Thank you for playing the game\n"
+ "Goodbye!\n");
else
System.out.println("You failed to complete the game in " + guesses + " guesses\n"
+ "Better luck next time.\nGoodbye!\n");
System.exit(0);
}
}while(!userWord.contains("-") || guesses <= 10);
}
  
// method to read input file and load all the words in the list
private static ArrayList<String> readWords(String file)
{
ArrayList<String> words = new ArrayList<>();
Scanner fileReader;
  
try
{
fileReader = new Scanner(new File(file));
while(fileReader.hasNextLine())
{
words.add(fileReader.nextLine().trim());
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println("Couldn't find " + file);
}
return words;
}
  
// retrun the contents of the list as a string
private static String getWord(ArrayList<String> list)
{
String res = "";
for(String s : list)
{
res += s;
}
return res;
}
}

**************************************************************** SCREENSHOT ***********************************************************

INPUT FILE

CONSOLE OUTPUT

Add a comment
Know the answer?
Add Answer to:
I am trying to make a word guessing game on java, where there are four letters...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C programming REQUIREMENTS: This program is a letter guessing game where a simple Al opponent...

    Using C programming REQUIREMENTS: This program is a letter guessing game where a simple Al opponent generates a secret letter for the user to guess. The user must be able to take turns guessing the secret letter, with the Al responding to the user's guesses. After successfully guessing, the user must be allowed to play again. The Al must count how many turns the user has taken. Here is an example of a game in progress where the human user...

  • Word Guessing Game Assignment Help Needed. This C++ (I'm using Visual Studio 2015) assignment is actually...

    Word Guessing Game Assignment Help Needed. This C++ (I'm using Visual Studio 2015) assignment is actually kind of confusing to me. I keep getting lost in all of the words even though these are supposed to instruct me as to how to do this. Can I please get some help as to where to start? Word guessing game: Overview: Create a game in which the user has a set number of tries to correctly guess a word. I highly recommend...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The...

    Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The game requires the user to guess a secret word. The game does not end until the user guesses the word. After they win the game, they are prompted to choose to play again. The secret word that user must guess is "valentine". It is a case-sensitive analysis With each guess... If the guess does not have an equal number of characters, tell the user...

  • Python Problem Hello i am trying to finish this code it does not save guesses or...

    Python Problem Hello i am trying to finish this code it does not save guesses or count wrong guesses. When printing hman it needs to only show _ and letters together. For example if the word is amazing and guess=a it must print a_a_ _ _ _ not a_ _ _ _ and _a_ _ _ separately or with spaces. The guess has a maximum of 8 wrong guesses so for every wrong guess the game must count it if...

  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. The phrase is hidden-- all letters are replaced with asterisks. Spaces and punctuation are left unhidden. So if the phrase is "Joe Programmer", the initial partially hidden phrase is: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...

  • FIX CODE-- import random number=random.randint ('1', 'another number') print("Hello, CIS 101") print("Let's play a guessing Game!called...

    FIX CODE-- import random number=random.randint ('1', 'another number') print("Hello, CIS 101") print("Let's play a guessing Game!called guess my number.") print("The rules are: ") print("I think of a number and you'll have to guess it.") guess = random.randint(1, 5) print("You will have " + str(guess) + " guesses.") YourNumber = ??? unusedguesses=int(guess) while unusedguesses < guess:     if int(YourNumber) == number:         print("YAY, You have guessed my number")     else:         unusedguesses=int(guess)-1         if unusedguesses == 0:              break         else:             print("Try again!") print("The number was ", str(number))

  • on python i need to code a guessing game. After the player has guessed the random...

    on python i need to code a guessing game. After the player has guessed the random number correctly, prompt the user to enter their name. Record the names in a list along with how many tries it took them to reach the unknown number. Record the score for each name in another list. When the game is quit, show who won with the least amount of tries. this is what i have so far: #Guess The Number HW assignment import...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT