2011-12-05 6 views
1

だから、私はハングマンの非常に簡単で完全なバージョンを作りました。今、私はプログラムを洗練し、ゆっくりとフィーチャーを追加したい(最終的に私が望むグラフィック)。私が修正したい最初のことは、この時点で、ユーザーが単語の長さの何度も同じ文字(単語にあるもの)を入力することを決めた場合、文字を繰り返すことと関係しています。偽の "勝利"。これをやめさせる最善の方法は何ですか?ハングマンゲームのバグ

ps。私はプログラミングに慣れていないので、このプロジェクトは本当に難しかったです...答えが本当に明白な場合は、申し訳ありませんが、私はもう今夜考えることができません。

任意の助けをいただければ幸い、ここに私のコードだ:

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<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<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"); 
    } 

} 
+3

あなたはJavaを初めて使っているので、私はあなたにそれを伝えています。適切な命名規則に従っていません。あなたのクラス名 "hangman_test2"は大文字で始まり、 "HangmanTest2"のように_を入れる必要はなく、変数名は小文字 "letsPlay"で始める必要があります。 – gprathour

+0

まだコードは見ていませんが、複数の文字を入力すると、入力した文字が単語の場合にのみ勝利するように設定できませんでしたか? – mowwwalker

答えて

4

HashSetを見て、Setインタフェースを持っています。 Setとは、同じオブジェクトを2度追加しないようにすることです。

だから、ユーザーが手紙を追加するたびに、それがすでにセットに入っているかどうかを確認してください。そうでない場合は、推測を確認してセットに追加します。すでにセットに入っている場合は、その推測を無視します。

+2

これはまた、ユーザーが同じ間違った文字を複数回推測するのを防ぐという利点があります。 – yshavit

+0

Gnat - 良い提案のように聞こえますが、明日の朝は間違いなく試してみて、うまくいけば回答を受け入れてください。 – ThisBetterWork

関連する問題