Note that there are some explanatory texts on larger screens.

plurals
  1. PObug in hangman game
    primarykey
    data
    text
    <p>So, I've successfully made a very simple but complete version of hangman. Now I want to refine the program and slowly add features (eventually graphics I hope). The first thing I'd like to fix has to do with repeating letters, at this point, If the user decided to type the same letter (one that IS in the word) as many times as the length of the word, they would get a false "win". What's the best way to stop this from happening?</p> <p>ps. I'm new to programming, this project was really hard for me...so if the answer is really obvious, sorry for asking, I just can't think anymore tonight. </p> <p>any help would be greatly appreciated, here's my code: </p> <pre><code>import java.io.*; public class hangman_test2 { public static void main(String[] args) throws IOException { BufferedReader in; in = new BufferedReader (new InputStreamReader (System.in)); boolean Lets_play=true; String response; while (Lets_play) { printheader(); String the_word=getWord(); System.out.println(the_word); print_blanks(the_word); guesses(the_word); System.out.println("Want to play again?"); response=in.readLine(); if(response.charAt(0)=='n' || response.charAt(0)=='N') { Lets_play= false; System.out.println("Thanks for playing!"); } } }//end main public static void printheader() { System.out.println("Welcome, lets play hangman!"); System.out.println("enter letters to guess the word\n"); }//end print header public static String getWord() { String [] possible_word=new String [10]; possible_word[0]="green"; possible_word[1]="orange"; possible_word[2]="tree"; possible_word[3]="flowers"; possible_word[4]="ocean"; possible_word[5]="grudge"; possible_word[6]="scraple"; possible_word[7]="crab"; possible_word[8]="insect"; possible_word[9]="stripes"; String theWord= possible_word [(int)(Math.random()*possible_word.length)]; return theWord; }//end the word public static void print_blanks(String the_word) { for (int x=0; x&lt;the_word.length(); x++) { System.out.print("_ "); } }//print blanks public static void guesses(String the_word)throws IOException { BufferedReader in; in = new BufferedReader (new InputStreamReader (System.in)); boolean thisRound=true; int strike=0; int right_letter=0; while (thisRound) { int letters_not_in_word=0; char letter_guessed=in.readLine().charAt(0); for (int current_letter=0; current_letter&lt;the_word.length(); current_letter++) { if (the_word.charAt(current_letter)==letter_guessed) { System.out.println(letter_guessed + " fits in space number " + (current_letter+1)); right_letter++; if(right_letter == the_word.length()) { win(the_word); thisRound=false; } } else { letters_not_in_word++; if (letters_not_in_word==the_word.length()) { System.out.println(letter_guessed + " is not in the word"); strike ++; if(strike==5) { lose(the_word); thisRound=false; } }//if }//else }//for }//while }//end guesses public static void win( String word) { System.out.println("\ncongradulations, you won!"); System.out.println("the word is " + word + "\n"); } public static void lose( String word) { System.out.println("\nsorry, you lost"); System.out.println("the word is " + word + "\n"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload