Hangman游戏,在获胜后无法获得显示/完成游戏的程序

时间:2013-12-05 03:28:33

标签: java

程序编译并运行。问题是,如果我输入一个正确的字母,当它在播放时,它会发现它是正确的,但不会“承认它”,可以这么说。它只是不断重复,除非你写了一个错误的字母,在这种情况下它正常工作并正确显示,以及计算剩余猜测并在第6次猜测后结束,返回主要。除此之外,它似乎对一个简单的初学者程序运行良好。我不知道如何让它像预期的那样工作,并且已经花了好几个小时试图解决它。任何帮助或建议将不胜感激。

我尽可能少地按照我的想法解决问题,再次感谢您的帮助和对不起的长度。

主要

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;

public class Hangmangame
{
   public static void main( String [] args)
   {    
      Scanner keyBd = new Scanner( System.in );
      char selection = ' ';
      do{
         System.out.printf("\nWelcome to Hangman!\n");
         System.out.println("1. Play Game");
         System.out.println("2. Rules");
         System.out.println("3. Exit");
         System.out.print  ("Selection: ");
         System.out.println();

         selection = keyBd.next().charAt(0);

            switch (selection)
            {
               case '1': Play(); break;//Play Game
               case '2': Rules(); break;//go to rules  
               case '3': break;//exit program
               default: 
                  System.out.println("\nInvalid selection!");
                  System.out.println("Press any key+<Enter> to continue...\n");
                  keyBd.next();
            }//end switch 
      }//end loop
      while(selection != '3');   
   }//end main

   public static void Play()
   {  
      Letter guesses = new Letter();
      Gallows printGallows = new Gallows();
      Word WordtoGuess = new Word(); 
      Hint YourHint = new Hint();     

      String[] Words = { "armor","dwarf","rogue","remix","shank","toxin","viper","wrath","wreck","zones"}; 


      ArrayList<Hint> Hints = new ArrayList<>();
         Hints.add(new Hint("often made out of metal"));
         Hints.add(new Hint("a short mythological blacksmith"));
         Hints.add(new Hint("a scoundrel or rascal"));
         Hints.add(new Hint("edited or changed"));
         Hints.add(new Hint("homemade sharpened object"));
         Hints.add(new Hint("a poisonous substance"));
         Hints.add(new Hint("poisonous reptile or vehicle"));
         Hints.add(new Hint("retributary punishment"));
         Hints.add(new Hint("scattered debris in an area"));
         Hints.add(new Hint("pre-selected designated areas"));      

      Random rnum = new Random();
      int n;
      System.out.println("start game");
      n = rnum.nextInt(10);
      WordtoGuess.setIndex(n);
      WordtoGuess.setWord(Words[n]);
      String word = Words[n];
      Hint hint = Hints.get(n);
      System.out.println("You have 6 chances to guess the word, good luck!!");
      System.out.println(" --\n   |\n   |\n   |\n_____\n");    //gallows
      do{
         while( printGallows.hasGuesses() /*&& WordtoGuess.Win()*/) //if this block is commented out the program will continue to play until all 6 attempts are used, ending the game.
         {                                                          //if not commented out the program will always tell you that you have won without ever asking for input guesses.

            System.out.println(hint);
            printGallows.printGallows();
            guesses.guessLetter();
            if ( WordtoGuess.Contains( guesses.getLetter() ) || WordtoGuess.NotContain( guesses.getLetter() ) ) 
            {
               System.out.println( "That letter has already been guessed... Please try another." );
            } 
            else 
            {
               if (WordtoGuess.ContainsGuess( guesses.getLetter() ) ) 
               {
                  WordtoGuess.CompareGuess( guesses.getLetter() , printGallows );
               } 
               else 
               {
                  printGallows.setGuesses();
               }
            }
         }
      } 
      while(WordtoGuess.RefreshGame(printGallows)); 
   }//end Play()

   public static void Rules()
   {      
      Scanner keyBd = new Scanner( System.in );
      System.out.println("Hangman Rules");
      System.out.println("Press any key+<Enter> to return to main menu");
      keyBd.next();      
   }//end Rules()   
}//end class hangman

import java.util.ArrayList;
import java.util.Scanner;

public class Word
{
   private String word;
   private char[] gword = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' }; //meant to be temporary in place of being sent from Letter
   private int index;
   private ArrayList<String> Words = new ArrayList<String>();
   private ArrayList<Character> goodGuess = new ArrayList<Character>();
   private ArrayList<Character> badGuess = new ArrayList<Character>();

   //constructor
   public Word(String word)
   {
      this.word = word;
   }

   public Word()
   {
      String word;
   } 
   //accesor methods
   public String getWord()
   {
      return word;
   }

   //mutator methods
   public void setWord(String word)
   {
     this.word = word;
   }

   public void setIndex(int index)
   {
      this.index = index;
   }

   public char[] setgword(char letter)
   {
      word = Words.get( index );
      gword = new char[ word.length() ];

      for( int i = 0; i < word.length(); i++) 
      {
         gword[i] = '_';
      }
      return gword;
   }

   public char[] CompareGuess( char letter , Gallows printGallows ) 
   {
      boolean isSame = true; 
      for ( int i = 0; i < 5; i++ ) 
      {
         if ( gword[ i ] == '_' ) 
         {
            if ( word.charAt( i ) == letter ) 
            {
               gword[ i ] = letter;
            }
         }
      }
      return gword;
   }

   public boolean Win()
   {
      for(int i=0; i<5;i++)
      {
         if (gword[i] == '_')
         {
            return true;
         }
      }
      return false;      
   }

   public boolean ContainsGuess( char letter ) 
   {
      for ( int i = 0; i < word.length(); i++ ) 
      {
         if ( word.charAt( i ) == letter ) 
         {
            goodGuess.add( letter );
            return true;
         }
      }
      badGuess.add( letter );
      return false;
   }

   public boolean Contains ( char letter ) 
   {
      for (int i = 0; i < goodGuess.size(); i++ ) 
      {
         if ( goodGuess.get( i ) == letter ) 
         {
            return true;
         }
      }
      return false;
   }

   public boolean NotContain ( char letter ) 
   {
      for (int i = 0; i < badGuess.size(); i++ ) 
      {
         if ( badGuess.get( i ) == letter ) 
         {
         return true;
         }
      }
      return false;
   }

   public boolean RefreshGame( Gallows printGallows ) 
   {
      if (!Win())
      {
         System.out.println("Good job you WIN!!!!");
      }
      else
      {
         printGallows.printGallows();
      }      
      printGallows.printGallows();
      goodGuess.clear();
      badGuess.clear();
      printGallows.setGuesses( 0 );
      System.out.printf("Your word was %s\n ",word); 
      return false;
   }

   //other Methods
   public String toString()
   {
      return String.format("your word is %s",word);
   }//end toString()              
}//end class Word

import java.util.Scanner;

public class Letter
{
   private char letter;
   Word gword = new Word();

   //constructor
   public Letter()
   {
      char letter;
   }

   //accesor methods   
   public char getLetter()
   {
      return letter;
   }

   //mutator methods      
   public char guessLetter() 
   {
      Scanner input = new Scanner( System.in );

      System.out.println( "\nGuess a letter: " );
      String letters = input.nextLine();
      this.letter = letters.charAt(0);
      //gword.setgword(letter); doesnt work properly with sending the input to gword in word file
      return letter;
   }   

   //other Methods
   public String toString()
   {
      return String.format("your letter is: %c",letter);
   }//end toString()         
}//end class Letter

盖洛斯

public class Gallows 
{
   private int badGuess;

   public Gallows() 
   {
      int badGuess = 0;
   }

   public int setGuesses() 
   {
      return badGuess++;
   }

   public int setGuesses( int badGuess) 
   {
      this.badGuess = badGuess;
      return badGuess;
   }

   public int getGuesses() 
   {
      return badGuess;
   }

   public boolean hasGuesses() 
   {
      if ( badGuess < 6 ) 
      {
         return true;
      }
      return false;
   }

   public void printGallows() 
   {
      switch (badGuess)
      {
         case 1:
            System.out.println(" --\n o |\n   |\n   |\n_____\n");    //head
            System.out.println("You only have 5 chances left!");
            break;
         case 2: 
            System.out.println(" --\n o |\n/  |\n   |\n_____\n");    //head + left arm
            System.out.println("You only have 4 chances left!");
            break;
         case 3: 
            System.out.println(" --\n o |\n/| |\n   |\n_____\n");    //head + left arm + body
            System.out.println("You only have 3 chances left!");
            break;
         case 4: 
            System.out.println(" --\n o |\n/|\\|\n   |\n_____\n");   //head + left arm + body + right arm
            System.out.println("You only have 2 chances left!");
            break;
         case 5: 
            System.out.println(" --\n o |\n/|\\|\n/  |\n_____\n");   //head + left arm + body + right arm + left leg
            System.out.println("You only have 1 chances left!");
            break;
         case 6:
            System.out.println(" --\n o |\n/|\\|\n/ \\|\n_____\n");  //head + left arm + body + right arm + left leg + right leg
            System.out.println("you lose! better luck next time!");
            break;  
      }//end switch
   }//end printGallows(); 
}//end class Gallows

1 个答案:

答案 0 :(得分:0)

我发现了你的错误,但我得说,你这里确实有一些冗余的代码。

您的主要问题是您没有跟踪进度。你可以在下面找到我改变的内容

在HangmanGame中

public static void Play()
{  
    Letter guesses = new Letter();
    Gallows printGallows = new Gallows();
    //WordToGuess is not initialized here 
    String YourHint = "";     
            //...
            //Not changed anything here                
            //...
    Random rnum = new Random();
    int n;
    System.out.println("start game");
    n = rnum.nextInt(10);


            //WordtoGuess is initialized here, after you have chosen a random number. 
            //Also, note how I didn't use the empty constructor, I am using the one that takes 
            //the word as its input - that constructor is changed in the Word class
    Word WordtoGuess = new Word(Words[n]);


    String hint = Hints.get(n);
    System.out.println("You have 6 chances to guess the word, good luck!!");
    System.out.println(" --\n   |\n   |\n   |\n_____\n");    //gallows
    do{
        while( printGallows.hasGuesses() && WordtoGuess.Win()) //I uncommented this part
        {                                                          
                //Nothing changed here
        }
    }
    while(WordtoGuess.RefreshGame(printGallows)); 
}//end Play()

在Word课程中:

public Word(String word)
{
    this.word = word;
    //The below lines of code is added. This initializes an array of characters 
    //that hold the lines to guess. Every character is initialized to an underscore
    //'_', since you are checking  this array for underscores. 
    gword = new char[word.length()];
    for (int i=0;i<gword.length;i++){
        gword[i] = '_';
    }
}

但是,正如我所说,这段代码可能更清晰。您可以使用单个方法打印绞架,保留2个char数组 - 一个用于猜测的单词,另一个用于跟踪,保留一个ArrayList以检查是否已经输入了一个字符,还有一个整数用于保存错误猜测的数量。