Hey, I just started programming this September and I have an assignment in which I am supposed to create a hangman game. This is my first post on this forum so forgive me for any "noob" mistakes Now I'm still learning Java so this will be a basic program running in the console window (no GUI) using strings loops, arrays, etc but hopefully nothing too complicated. I've developed somewhat of an algorithm for the program, but it's really simple so far.
Title screen
main menu (play, instructions, quit)
if they choose to select play there is a choice of difficulty (easy, med, hard)
depending on what difficulty they choose there will be different words
Now I've got all that simple stuff out of the way and I've gotten up to the part where it displays the blanks such as _ _ _ _ _ and now what I'm trying to do is figure out how to get input from the user and use it to fill in the blanks, and give them 5 "lives". What kind of process do I go through to make this happen? I'm not asking for any specific code but I might need some explanations on how I can piece everything together.
Thanks in advance!!!
25 3694
Greetings, yottabyte!
Welcome to bytes... Can you show us the code you referred to? Also you'll probably need to do a lot of the work as we help you figure it all out.
It's exciting though, hope you survive it, it's quite the language, good choice.
In a bit!
For console applications, use the Scanner class to get the input from the user or the Console class if you are using Java 6.
Also, I'd create a couter lives which counts (who would guess?) how many lives are left. Then you can put it all in a while loop... you should be able to figure out the rest.
Greetings,
Nepomuk
Also also, read about regular expression; in particular the String.replaceAll() method is a nice start. This game is particularly fitted to be solved by regular expressions. They take away all sorts of tedious loops and checks.
kind regards,
Jos
Yeah i haven't exactly gotten too far yet but I want to know if I'm starting this off right
I made an array that is like this:
String[] easydifficulty = new String[51];
easydifficulty[0] = "word";
easydifficulty[1] = "another word";
easydifficulty[2] = "another word";
easydifficulty[3] = "another word";
But I just did that to have all my strings in one place...how exactly do I make one random string appear as blanks _ _ _ _ _ and get the user input? I also don't fully understand what you meant by a counter to count lives
OK, now you can do it with an array and I guess as you're a beginner, that should do. To choose a random word from the array, have a look at java.util.Random. Read Reply #3 about how to get user input. And the lives counter? Well, say you may get it wrong 5 times, then you can have
somewhere and every time a wrong letter is entered, that variable is decremented (= reduced by one). Then it checks... etc.
Oh, and writing the blanks? Well, you can find the length of a string with - String myString = "word";
-
System.out.println(myString + " is " + myString.length() + " characters long.");
-
That should get you going.
Greetings,
Nepomuk
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_word", and then the second player would try to guess it. With regards to the blanks, an int variable "no_of_blanks" would read the length of the "Secret_word" 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_word"). 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,
tiktik
Can you explain the secret word part a little bit more? I don't fully understand it but I think it will be important to continuing on my project.
@yottabyte
Don't go that way: you'll end up with a highly procedural and low level solution: Pascal doesn't have regular expression handling so you'll end up with lots of nasty little loops and byte fiddling. Have a look at this:
Suppose we have three Strings: word, used and correct; the word String contains the secret word, the used String contains the characters that are used by the user (whether correct or not) and the correct String contains the correctly guessed characters.
Suppose 'x' is the current guess character. So we can do: -
if (used.indexOf(x) >= 0) // character already used
-
if the above test fails 'x' represents a new guess, so: -
used+= x; // now set x as used
-
if (word.indexOf(x) < 0) // x is not in word
-
If the above test fails then x is a character in the word, so: -
correct+= x;
-
String leftOver= word.replaceAll("[^"+correct+"]", "-");
-
The last line of this code snippet replaces all characters in the word that are not present in the String correct. They are replaced by a '-'.
If there are still '-'s left in that last String the word wasn't guessed yet: -
boolean finished= leftOver.indexOf('-') < 0;
-
The few lines of code above represent the core of the game logic. Note that there are no silly loops, no counting, just the handling of the game logic.
kind regards,
Jos
Okay well I got the hard (?) part of the project done I hope. I can get it to display a bunch of # symbols as the blanks, then when you put in a letter it makes it appear in the puzzle. Now can I get some help on how to make it a loop though? I want to use a do-while loop but i don't know what to put in the brackets after the while part. - String []easywords=new String [51];
-
easywords[0]="insertwordhere";
-
-
and so on until the last word...then
-
-
int which = (int)(Math.random()*51);
-
char[] guess= new char[easywords[which].length()];
-
for(int index=0; index<easywords[which].length(); index++) guess[index]='#';
-
-
****do{ goes here?****
-
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();
-
for(int index=0; index<easywords[which].length(); index++) if(easywords[which].charAt(index)==letter) guess[index]=letter;
-
for(int index=0; index<easywords[which].length(); index++) System.out.print(guess[index]);
-
System.out.println();
-
In.getChar();
- ****}while (???) goes here?****
Now I need to put a do and a while in the spots I marked with a star right? But what would I put in the while bracket?? I'm stumped
The place you put that loop if fine, it will do the job. Now, there are several possible ways to check if you're finished, the most simple one would be to have an int variable that holds the amount of characters you haven't guessed yet. When that's 0, you've finished.
Greetings,
Nepomuk
Wait so is that supposed to be a word bank or lives?
Well, you could have two counters: one for the remaining lives, one for the letters you've guessed correctly. But that's just one way to do it - there are other possibilities.
Greetings,
Nepomuk
*cough* reply #10 *cough*
kind regards,
Jos
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?
@yottabyte
Sure, you tell me what you don't understand (yet) w.r.t. reply #10
kind regards,
Jos
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?
The lives--; isn't working for me!
Actually, the lives--; part shouldn't be the problem. But you probably don't want - if(found==false)
-
lives--;
-
System.out.println("Lives remaining: "+lives+"");
but instead - 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.
Greetings,
Nepomuk
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 - System.out.println("Lives remaining: "+lives+"");
Now what it does is only display the lives remaining after I guess over 10 times.
@yottabyte
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,
Jos
@JosAH
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.
OK, I checked it out and found your flaw: - if(found==true) // that could be just "if(found)"
-
lives--;
-
System.out.println("Lives remaining: "+lives+"");
Greetings,
Nepomuk
Bah...I ended up figuring it out myself, although I really appreciate the help you guys gave.
Thanks a bunch! The program works perfectly now!
@yottabyte
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: -
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");
-
}
-
}
-
kind regards,
Jos
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. @tiktik Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
1 post
views
Thread by rainbowii7 |
last post: by
|
5 posts
views
Thread by tigrfire |
last post: by
| | |
3 posts
views
Thread by kaka_hunter |
last post: by
| | | | | | | | | | | | | |