Yeah I'm thinking that's pretty important to what I need to do, I still dont get it 100%....maybe could you elaborate on it some more please?
Help making a hangman program
Collapse
X
-
Comment
-
Ok let me post what I've got so far.
Now the problem is how can i lower the lives whenever they get a letter wrong?Code:public static void easypuzzle() { String []easywords=new String [51]; easywords[0]="cheat "; easywords[1]="photo"; easywords[2]="mathematics"; easywords[3]="official"; easywords[4]="service"; easywords[5]="favourite"; easywords[6]="chair"; easywords[7]="cheap"; easywords[8]="variable"; easywords[9]="trapping"; easywords[10]="west"; easywords[11]="Bind"; easywords[12]="teach"; easywords[13]="attitude"; easywords[14]="danger"; easywords[15]="hat"; easywords[16]="bright"; easywords[17]="die"; easywords[18]="engineer"; easywords[19]="interface"; easywords[20]="million"; easywords[21]="steal"; easywords[22]="pattern"; easywords[23]="local"; easywords[24]="sharing"; easywords[25]="dump"; easywords[26]="event"; easywords[27]="lower"; easywords[28]="man"; easywords[29]="hang"; easywords[30]="governor"; easywords[31]="chose"; easywords[32]="showing"; easywords[33]="assembler"; easywords[34]="informing"; easywords[35]="monitor"; easywords[36]="core"; easywords[37]="opportunity"; easywords[38]="process"; easywords[39]="unfortunate"; easywords[40]="wall"; easywords[41]="instruction"; easywords[42]="total"; easywords[43]="angle"; easywords[44]="outside"; easywords[45]="pipe"; easywords[46]="connection"; easywords[47]="ground"; easywords[48]="flash"; easywords[49]="movement"; easywords[50]="repeating"; int lives=10; char [] alphabet=new char[26]; alphabet[0]='a'; alphabet[1]='b'; alphabet[2]='c'; alphabet[3]='d'; alphabet[4]='e'; alphabet[5]='f'; alphabet[6]='g'; alphabet[7]='h'; alphabet[8]='i'; alphabet[9]='j'; alphabet[10]='k'; alphabet[11]='l'; alphabet[12]='m'; alphabet[13]='n'; alphabet[14]='o'; alphabet[15]='p'; alphabet[16]='q'; alphabet[17]='r'; alphabet[18]='s'; alphabet[19]='t'; alphabet[20]='u'; alphabet[21]='v'; alphabet[22]='w'; alphabet[23]='x'; alphabet[24]='y'; alphabet[25]='z'; System.out.println("You have chosen easy difficulty"); int which = (int)(Math.random()*51); char[] guess= new char[easywords[which].length()]; for(int index=0; index<easywords[which].length(); index++) guess[index]='-'; while(lives>0&&!check(guess,easywords[which])) { for(int index=0; index<easywords[which].length(); index++) System.out.print(guess[index]); System.out.println(); System.out.print("Guess a letter: "); char letter = In.getChar(); boolean found=false; for(int index=0; index<alphabet.length; index++) { if(letter == alphabet[index]) { found=true; alphabet[index]=' '; for(int i=0; i<easywords[which].length(); i++) if(easywords[which].charAt(i)==letter) guess[i]=letter; break; } } if(found==false) lives--; System.out.println("Lives remaining: "+lives+""); } }
The lives--; isn't working for me!Comment
-
Actually, the lives--; part shouldn't be the problem. But you probably don't wantbut insteadCode:if(found==false) lives--; System.out.println("Lives remaining: "+lives+"");That way, both of those commands are run if the if-clause is correct. Otherwise, only the lives-- is done.Code:if(found==false) { lives--; System.out.println("Lives remaining: "+lives+""); }
Greetings,
NepomukComment
-
I can't get it to display the lives remaining now, where should I move the line of code to?
The line of code i'm referring to
Now what it does is only display the lives remaining after I guess over 10 times.Code:System.out.println("Lives remaining: "+lives+"");Comment
-
I fail to see how you're using the methods I described in reply #10; you're heading towards the procedural, low level way of solving this: hundreds of lines of (buggy) code and a messy control flow.
Don't be afraid to throw away your old code and start afresh.
kind regards,
JosComment
-
Yeah I know, but it's just hard for me to let go of all the other stuff I've been doing, and I've got a deadline for tomorrow. Basically everything is working except for the lives. I just want to know how I can fix that part up since I really don't think I have the time to start all over again.I fail to see how you're using the methods I described in reply #10; you're heading towards the procedural, low level way of solving this: hundreds of lines of (buggy) code and a messy control flow.
Don't be afraid to throw away your old code and start afresh.
kind regards,
JosComment
-
Congrats; I think your deadline has passed by now and I want you to have a look at the following implementation: no loops, no char arrays and no convoluted control flow. Please study it a bit and see how it can be done in a more neat way:
kind regards,Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class HangMan { private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); private String correct, used, word; private int lives; private void setup(String w, int turns) { correct= "-"; used= ""; word= w; lives= turns; } private String word(String prompt) { String word= this.word.replaceAll("[^"+correct+"]", "-"); System.out.println(prompt+": "+word); return word; } private boolean wins(String word) { boolean wins= word.indexOf('-') < 0; if (wins) System.out.println("You guessed the word"); return wins; } private String guess(char x) { if (used.indexOf(x) >= 0) return word("Character '"+x+"' already used"); used+= x; if (word.indexOf(x) < 0) { lives--; return word("Character '"+x+"' is incorrect"); } correct+= x; return word("Character '"+x+"' is correct"); } private char input() { String s= ""; try { for (; (s= s.trim()).length() == 0; s= br.readLine()) System.out.print("Guess a character: "); } catch (IOException ioe) { System.exit(1); } if (s.equals("stop")) System.exit(0); return s.charAt(0); } public boolean play(String word) { for (setup(word, word.length());; System.out.println("lives: "+lives)) if (wins(guess(input()))) return true; else if (lives == 0) { System.out.println("You lose"); return false; } } public static void main(String[] args) { new HangMan().play("defenestration"); } }
JosComment
-
Hello. So I am trying to learn about Java on my own because I thought it would be interesting. I would be really interested in seeing how you did your program. I bought the Java book by C. Thomas Wu and there is a similar problem in the book that is giving me a hard time.
hi there...
Last year, I did the whole hangman game in Pascal; however it was a little bit different in structure than yours is going to be; in such a way that mine first involved the first player to enter the "Secret_wor d", and then the second player would try to guess it. With regards to the blanks, an int variable "no_of_blan ks" would read the length of the "Secret_wor d" entered by player 1, and then would dislpay the corresponding amount of blanks for player 2 to guess...
With regards to your question about how to get input from the user and use it to fill in the blanks, my approach to it was to read the character entered by the user, and then use an IF statement to check whether the character entered is correct (i.e. found in the "Secret_wor d"). If it turns out to be correct, then it should be dislpayed instead of the corresponding blank (in my case these were underscores _ ).
Hope this helps.
Kind regards,
tiktikComment
Comment