java - My Hangman Code Isn't Registering Incorrect Guesses Immediately, is Printing Double Sets of Underscores, and more -


i'm having 3 primary issues code replicate hangman game:

the "fill-in" portion randomly printed twice, correct guesses randomly logged incorrect
enter image description here

and incorrect guesses not counted
enter image description here code main class:

    public class main      {          public static void main(string[] args)          {             hangman manhang = new hangman();             }      }  hangman class:      import java.util.random;       public class hangman             {                 random r;                 getdata get;                 string[] bank = {"consider","minute","accord","evident","practice","intend","concern","commit","issue","approach","establish","utter","conduct","engage","obtain","scarce","policy","straight","stock","apparent","property","fancy","concept","court","appoint","ambiguous","arbitrary","alliteration","arrogant","benevolent","belligerent","boycott","cynical","connotation","cessation","contemporary","craving","grandiose","gratuitous","guile","harbinger","impetuous","incandescent","indigent","inexorable","injunction","insipid","insurgent","languish","magnate","abjure","abrogate","abstemious", "acumen", "antebellum","auspicious","belie","bellicose","bowdlerize","chicanery","chromosome","churlish","circumlocution","circumnavigate","deciduous","deleterious","diffident","enervate","enfranchise","epiphany","equinox","evanescent","expurgate","facetious", "fallacious"};                 string word;//stores random word used                 boolean finished = false;                 int incguesscount = 0;                 boolean[] lettersfound;//used mark letters have been found                 string guessedletter=" ";//used store guesses                 boolean playerhaswon = false;              public hangman()             {                 r = new random();                 = new getdata();                 word=bank[r.nextint(bank.length)]; //selects random word , assigns word value                 lettersfound = new boolean[word.length()]; //creates boolean array length of word                 int incguesscount = 0;                  while(incguesscount<5 && playerhaswon == false)                 {                     drawgallows(); //show gallows depending on how many incorrect guesses there                     displayword();                     getguess();                     checkguess();                     //checkvictory();                  }                  if (incguesscount>=5)                 {                     system.out.print('\u000c');//clears screen                     fivewrong();//displays full hangman                     system.out.println("you have lost! word "+word);                 }             }          public void getguess()         {             system.out.println("\u000c");             system.out.println(" ");             system.out.println("what letter guess?");             system.out.println("you have "+(5-incguesscount)+" guesses left.");             system.out.print("enter guess:");             guessedletter = get.aword();//uses scanners take in guesses         }           public boolean displayword()                     {             boolean goodguess = false;//assumes guess bad automatically             char letter = guessedletter.charat(0);              for(int = 0;i<word.length();i++)//goes through letters check guess's status                 if (lettersfound[i]==true)//checks if letter revealed @ position                 {                  system.out.print(word.charat(i)+" ");                 }                      else if (word.charat(i)==letter)//prints correctly guessed letter @ position                     {                         system.out.print(word.charat(i)+" ");                         lettersfound[i] = true;                         goodguess = true;                     }                         else//fills in non-applicable spaces underscore                             system.out.print("_ ");               return goodguess;                    }             public void checkguess()             {                  if(!displayword() && incguesscount==5)                      fivewrong();                  else if(!displayword() && incguesscount<5)                     {                      incguesscount++;                      drawgallows();                      getguess();                      displayword();                     }                   else                  {                      drawgallows();                      getguess();                      displayword();                  }             }             public void defaultman()         {             system.out.println('\u000c');             system.out.println(" ________");             system.out.println(" |      | ");             system.out.println(" |      | ");                                     system.out.println(" |      | ");                              system.out.println(" |       ");                                system.out.println(" |_________ ");             system.out.println(" ");         }          public void onewrong()         {             system.out.println('\u000c');             system.out.println(" ________");             system.out.println(" |      | ");             system.out.println(" |      | ");                                     system.out.println(" |      | ");                              system.out.println(" |       \\ ");                                system.out.println(" |_________ ");             system.out.println(" ");         }          public void twowrong()         {             system.out.println('\u000c');             system.out.println(" ________");             system.out.println(" |      | ");             system.out.println(" |      | ");                                     system.out.println(" |      | ");                              system.out.println(" |     / \\ ");                                system.out.println(" |_________ ");             system.out.println(" ");         }          public void threewrong()         {             system.out.println('\u000c');             system.out.println(" ________");             system.out.println(" |      | ");             system.out.println(" |      | ");                                     system.out.println(" |     /| ");                              system.out.println(" |     / \\ ");                                system.out.println(" |_________ ");              system.out.println(" ");         }          public void fourwrong()         {             system.out.println('\u000c');             system.out.println(" ________");             system.out.println(" |      | ");             system.out.println(" |      | ");                                     system.out.println(" |     /|\\ ");                              system.out.println(" |     / \\ ");                                system.out.println(" |_________ ");             system.out.println(" ");         }          public void fivewrong()         {             system.out.println('\u000c');             system.out.println(" ________");             system.out.println(" |      | ");             system.out.println(" |     ( ) ");                                     system.out.println(" |     /|\\ ");                              system.out.println(" |     / \\ ");                                system.out.println(" |_________ ");             system.out.println(" ");             system.out.println("you have lost! word "+word+".");             system.out.println("rerun program try again.");             finished=true;         }          public void drawgallows()         {             if(incguesscount==0)             {                 defaultman();                                }              if(incguesscount==1)             {                 onewrong();                                }              if(incguesscount==2)             {                 twowrong();                               }              if(incguesscount==3)             {                  threewrong();                              }              if(incguesscount==4)             {                 fourwrong();                                }              if(incguesscount==5)             {                 fivewrong();              }              }         }   , getdata class:      import java.util.scanner;      public class getdata     {        private scanner input;         public getdata()//produces scanner take in input       { input = new scanner(system.in); }         public string aword()//gets input guess/string       { return input.next(); }         public int anumber()//gets input number       { return input.nextint(); }      } 

apologies poor organization, i'm new coding. i've dug through code hours multiple people can't figure out logic fails, , in @ least several areas. appreciated, if it's pointing out more minor flawed logic or useless variables. thank you.

update: contributed, answers helped me fix bugs program. have 1 more problem win detection. have created variable gameover stores true when user has exceeded number of incorrect guesses , had increment whenever letter printed opposed underscore. created 2 if statements:

if(corletters == word.length())                     gameover = true;              if (incguesscount<5 && gameover)             {                 system.out.println("\u000c");                 system.out.println("congratulations!");                 system.out.println("you have won!");                 system.out.println("rerun program try again.");             } 

but game doesn't register win. again, , if have suggestions fix or improve win detection appreciated :)

this new hangman class after having issues resolved in thread:

import java.util.random;   public class hangman         {             random r;             getdata get;             string[] bank = {"consider","minute","accord","evident","practice","intend","concern","commit","issue","approach","establish","utter","conduct","engage","obtain","scarce","policy","straight","stock","apparent","property","fancy","concept","court","appoint","ambiguous","arbitrary","alliteration","arrogant","benevolent","belligerent","boycott","cynical","connotation","cessation","contemporary","craving","grandiose","gratuitous","guile","harbinger","impetuous","incandescent","indigent","inexorable","injunction","insipid","insurgent","languish","magnate","abjure","abrogate","abstemious", "acumen", "antebellum","auspicious","belie","bellicose","bowdlerize","chicanery","chromosome","churlish","circumlocution","circumnavigate","deciduous","deleterious","diffident","enervate","enfranchise","epiphany","equinox","evanescent","expurgate","facetious", "fallacious"};             string word;//stores random word used             boolean finished = false;             int incguesscount = 0;             int corletters = 0;             boolean[] lettersfound;//used mark letters have been found             string guessedletter=" ";//used store guesses             boolean gameover = false;               public hangman()             {                 r = new random();                 = new getdata();                 word=bank[r.nextint(bank.length)]; //selects random word , assigns word value                 lettersfound = new boolean[word.length()]; //creates boolean array length of word                                   {                     drawgallows(); //show gallows depending on how many incorrect guesses there                     displayword();                     getguess();                     checkguess();                  }                 while(incguesscount<5 && gameover == false);                  if (incguesscount>=5)                 {                      fivewrong();//displays full hangman                  }                  if(corletters == word.length())                     gameover = true;                  if (incguesscount<5 && gameover)                 {                     system.out.println("\u000c");                     system.out.println("congratulations!");                     system.out.println("you have won!");                     system.out.println("rerun program try again.");                 }               }          public void getguess()         {             system.out.println("\u000c");             system.out.println(" ");             system.out.println("what letter guess?");             system.out.println("you have "+(5-incguesscount)+" guesses left.");             system.out.print("enter guess:");             guessedletter = get.aword();//uses scanners take in guesses         }           public boolean displayword()                     {             boolean goodguess = false;//assumes guess bad automatically             char letter = guessedletter.charat(0);              for(int = 0;i<word.length();i++)//goes through letters check guess's status                 if (lettersfound[i]==true)//checks if letter revealed @ position                 {                  system.out.print(word.charat(i)+" ");                  corletters++;                 }                      else if (word.charat(i)==letter)//prints correctly guessed letter @ position                     {                         system.out.print(word.charat(i)+" ");                         lettersfound[i] = true;                         goodguess = true;                         corletters++;                     }                         else//fills in non-applicable spaces underscore                             system.out.print("_ ");               return goodguess;                    }             public void checkguess() {              boolean disw = displayword();               if (!disw && incguesscount == 5)               fivewrong();              else if (!disw && incguesscount < 5) {               incguesscount++;               }             }           public void defaultman()         {             system.out.println('\u000c');             system.out.println(" ________");             system.out.println(" |      | ");             system.out.println(" |      | ");                                     system.out.println(" |      | ");                              system.out.println(" |       ");                                system.out.println(" |_________ ");             system.out.println(" ");         }          public void onewrong()         {             system.out.println('\u000c');             system.out.println(" ________");             system.out.println(" |      | ");             system.out.println(" |      | ");                                     system.out.println(" |      | ");                              system.out.println(" |       \\ ");                                system.out.println(" |_________ ");             system.out.println(" ");         }          public void twowrong()         {             system.out.println('\u000c');             system.out.println(" ________");             system.out.println(" |      | ");             system.out.println(" |      | ");                                     system.out.println(" |      | ");                              system.out.println(" |     / \\ ");                                system.out.println(" |_________ ");             system.out.println(" ");         }          public void threewrong()         {             system.out.println('\u000c');             system.out.println(" ________");             system.out.println(" |      | ");             system.out.println(" |      | ");                                     system.out.println(" |     /| ");                              system.out.println(" |     / \\ ");                                system.out.println(" |_________ ");              system.out.println(" ");         }          public void fourwrong()         {             system.out.println('\u000c');             system.out.println(" ________");             system.out.println(" |      | ");             system.out.println(" |      | ");                                     system.out.println(" |     /|\\ ");                              system.out.println(" |     / \\ ");                                system.out.println(" |_________ ");             system.out.println(" ");         }          public void fivewrong()         {             system.out.println('\u000c');             system.out.println(" ________");             system.out.println(" |      | ");             system.out.println(" |     ( ) ");                                     system.out.println(" |     /|\\ ");                              system.out.println(" |     / \\ ");                                system.out.println(" |_________ ");             system.out.println(" ");             system.out.println("you have lost! word "+word+".");             system.out.println("rerun program try again.");             gameover=true;         }          public void drawgallows()         {             if(incguesscount==0)             {                 defaultman();                                }              if(incguesscount==1)             {                 onewrong();                                }              if(incguesscount==2)             {                 twowrong();                               }              if(incguesscount==3)             {                  threewrong();                              }              if(incguesscount==4)             {                 fourwrong();                                }              if(incguesscount==5)             {                 fivewrong();              }              }         }  

the reason "fill-in" portion can printed twice because using print word , determine if character correct. mean when have inside checkguess():

if(!displayword() && incguesscount==5)              fivewrong(); else if(!displayword() && incguesscount<5) 

you calling displayword() (which prints word , returns true if guess good) in decision part of if statement. causes word printed inside checkguess() method. fixed making checkword() such as:

public boolean checkword()             {     boolean goodguess = false;//assumes guess bad automatically     char letter = guessedletter.charat(0);     for(int = 0;i<word.length();i++){//goes through letters check guess's status         if (word.charat(i)==letter)         {             lettersfound[i] = true;             goodguess = true;         }     }      return goodguess;            }  

this used in decision part of if statement.

the second thing causing diagram not updated timely doing in checkguess(). asking them new guesses when should handled main while{}. edited version this:

public void checkguess() {     if(!checkword() && incguesscount==5)         fivewrong();     else if(!checkword() && incguesscount<5)     {         incguesscount++;     } }  

also, in constructor make counter makes loop fail end after player loses (because local variables of same name used before global variables) see following code:

 r = new random();  = new getdata();  word=bank[r.nextint(bank.length)];  lettersfound = new boolean[word.length()];  int incguesscount = 0; // <<< shouldn't here 

the thing left make win detection , think you're on way!

edit:

for win detection, way decided count number of characters user has guessed correctly. problems in displayword() add number of correct letters each time word printed, rather when add new correct character. change make:

if (lettersfound[i]==true)//checks if letter revealed @ position {     system.out.print(word.charat(i)+" ");     corletters++; } else if (word.charat(i)==letter)//prints correctly guessed letter @ position {     system.out.print(word.charat(i)+" ");     lettersfound[i] = true;     goodguess = true;     corletters++; } 

into this:

if (lettersfound[i]==true)//checks if letter revealed @ position {     system.out.print(word.charat(i)+" ");     //deleted line here } else if (word.charat(i)==letter)//prints correctly guessed letter @ position {     system.out.print(word.charat(i)+" ");     lettersfound[i] = true;     goodguess = true;     corletters++; } 

the other problem check if game on user win outside main do while. can fixed moving if statement checkguess(). such as:

public void checkguess() {     boolean disw = displayword();      if (!disw && incguesscount == 5)         fivewrong();     else if (!disw && incguesscount < 5) {         incguesscount++;      }      if (corletters == word.length()){         gameover = true;     } }  

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo