简体   繁体   中英

Hangman Java Game Logic

I am learning java and I am writing a client-server hangman game where the client contacts the server and the server responds with a random word. The client then covers the word with asterisks. For example, if the word is java, the user will see ****. The user then guesses a letter and the part I am stuck on is how decrement the amount of guesses remaining when the user guesses a wrong letter. So for example:

The client sends the word "NEW" to the server and the server responds with the length of a random word and the program translates this into asterisks. Let's say the word is "java".

****

The user guesses the letter 'a'. The server then would respond with something like:
false
true
false
true
This is the value of theValue

The way I wrote my program it prints:
*a*a

This is what I want however, my program subtracts two guesses since 2 of the 4 boolean values returned by the server are false even though the user guessed a correct letter. Is there a way to look at all the boolean values and if none are true, then decrement a guess?

Here is the code:

public void evaluateGuess() {
    try {         
      for (int i = 0; i < wordAsItIs.length(); i++) {
            String word = in.readLine();     
            boolean theValue = Boolean.parseBoolean(word);
            ArrayList<Boolean> booleans = new ArrayList<Boolean>();
            booleans.add(theValue);
            System.out.println(booleans);

           if (booleans.contains(true) && wordAsItIs.charAt(i) == '*') {
               wordAsItIs.setCharAt(i, playersGuess);
           }

           if (!booleans.contains(true))
               guessesRemaining--;
      }
      displayGame();

    } catch (IOException e) {
      e.printStackTrace();
    }
  }

Dunno if you got the answer to your question.. but this MIGHT help. It's from something ive already done similar to what you are doing:

public static char[] checkMethod(char[] checkArray, char letter, char[] word)
{
    boolean flag = false;

    for(int i=0; i<checkArray.length; i++)
    {
        if(word[i] == letter)
        {
            checkArray[i] = letter;     //checks if the letter guessed exists in the board
            flag = true;
        }
    }

    if(flag == true)
        System.out.println("Good job!\n");  // if guess is right
    else 
    {
        counter--;
        if(counter > 0)     // counter for wrong guesses
            System.out.println("Wrong! You have " + counter + " guesses left");
    }
    return checkArray;
}

Where counter is declared outside in the class public static int counter = 5; //maximum number of wrongs public static int counter = 5; //maximum number of wrongs

Hope that helps.

do {

code for checking each guess, essentially your for loop goes in here

} while (booleans == false);

As soon as a true comes up, the loop will exit. Then, if it exits during the word, you can have the value not subtracted. If it doesn't exit, then you will need another conditional in there. I'd need to see more of your code (declarations of variables, constructors, etc.) to give you the specific code for your game.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM