2011-12-05 122 views
1

所以,我已经成功地制作了一个非常简单但完整的hang子手版本。现在我想改进程序并慢慢添加功能(最终是我希望的图形)。我想解决的第一件事就是重复字母,在这一点上,如果用户决定键入单词长度的同一个字母(单词中的一个),他们会得到虚假的“胜利”。阻止这种情况发生的最好方法是什么?hang子手游戏中的bug

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接口。集合做什么是防止两次添加相同的对象。

因此,每次用户添加一个字母,检查它是否已经在集合中。如果不是,则检查他们的猜测并将其添加到集合中。如果它已经在集合中,那么忽略他们的猜测。

+2

这也有防止用户多次猜测相同的错误字母的好处。 – yshavit

+0

Gnat - 这听起来像一个很好的建议,我一定会在明天早上尝试一下,并确保在出现问题时接受您的答案。 – ThisBetterWork