Code the Hangman Game in Java
HTML-код
- Опубликовано: 17 июн 2020
- In this video, I will teach you how to make a hangman game in java.
Thanks for watching the video and please leave a like and subscribe!
*****************************************
The link to the dictionary text file:
www.gwicks.net/dictionaries.htm
Download the text version for English
unzip it and the text file is inside the folder
*****************************************
The Function Code:
public static void drawHangman(int l) {
if(l == 6) {
System.out.println("|----------");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 5) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 4) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("| |");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 3) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("| -|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 2) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("| -|-");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 1) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("| -|-");
System.out.println("| /");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else{
System.out.println("|----------");
System.out.println("| O");
System.out.println("| -|-");
System.out.println("| /|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
}
hey great video, just wanted to know if there is a way I can let users get the whole word and still have the commands you wrote for checking valid input. thanks !
What you can do is add code at the end of the while loop that asks the user for input and then uses an if statement to check If the input is the same as the hidden_text. If they are the same then set finish = true
this is really helpful for beginners like me ! thank you for sharing !!!
Exactly what I was looking for! But in my case I just want to code the program so that I can choose the word myself.
However you got my subscription.
Thank you so much! I learned a lot from your video. I subscribed to your channel to get updated for your new videos :)
great video man but i am having trouble pasting in my dictionary into the SRC.... nothing happens when I right click the src and press paste
it may be because i am using IntelliJ and not eclipse. Any thoughts or advice?
great video bro, learned alot!!
thank you, helped a lot!
Ty man, u helped me a lot
Ty bro, u helped me for my application hehe
omg your the best very nice video ithelped me understand at last how this work , now i have a request can you do one with fxml and methods accepting parameters?
Amazing video learned a lot. Quick Question what would be the best method to test short programs like hangman Junit or TDD?
I would just test myself if the program is short enough.
Thank you so much bro
Hey, could you suggest how I could add a warning counter so that if the user types a number or more than 1 letter they get a warning and 3 warnings would result in a life lost
so check their input every time they enter it. For the life lost I think you can create a variable to count how many times u warn them and when it reaches 3 you subtract their life.
@@CodingWithTim ty, would this be inside the same while loop that runs when input Is a number or >1 letter, and if so how would I get it to exit the while loop when lives are 0 as the loop runs while input is number and > 1 letter
@@steve.d1614 www.youtube.com/@kushpatelj86/featured
hello! great video but if i wanted to know the whole word at the end how would i do that?
hey I know you did this video over a year ago but while(letter.length() ! = 1 || Character.isDigit(letter.charAt(0))) { this line of code is saying int cannot be converted to Boolean, wondering if you can help me out thanks
I really don’t understand what I’ve done wrong
it should be letter.length() != 1 instead of ! =
There shouldn't be a space between ! and =.
@@CodingWithTim thank you so much!
@@olliebb1735 www.youtube.com/@kushpatelj86/featured
@@olliebb1735 www.youtube.com/@kushpatelj86/featured
String letter = input.next();
//checks for valid input
while(letter.length() != 1 || Character.isDigit(letter.charAt(0))); {
System.out.println("Error Input - Try Again");
letter = input.next();
this while loop doesnt work properly for me, it always says an error if i put in 1 character and if i put 2+ the code would just break.
Did you put a closing bracket for the while loop?
@@CodingWithTim ye i did
After ' charAt(0))) ' get rid of your semicolon. It doesn't belong there.
@@jonispatented www.youtube.com/@kushpatelj86/featured
@@AlawiDenGhosty www.youtube.com/@kushpatelj86/featured
Thanks brooooooooooo.
How could i do this if i want a string of self typed words?
So instead of adding words from a text file, you can add it yourself. To do that, simply create an empty ArrayList of String and manually add String into the list. You can make your list that way instead.
***example code****
ArrayList my_list = new ArrayList();
my_list.add("hello");
my_list.add("world");
my_list.add("apple");
***example code****
I manually added three words into my_list. You can follow that format for as many words as you want.
@@CodingWithTim could i then simply delete the “file dictionary” lines and input the array list or would I also have to delete the “word arraylist” ?
@@lassemadsen7382 sure delete the word arraylist along with the file dictionary.
@@CodingWithTim this might be too much to ask but could I possibly dm you somewhere and show you my code because something has gone completely wrong ..
@@lassemadsen7382Sure, I can definitely do what I can to help. Here is my email address: timtanthetitan@gmail.com
If you have trouble with the errors, it would be helpful for me if you could send me the code and error messages through email.
something wrong with the dictionary link it just opens and closes nothing downloads
I updated the description, use the new instruction to download the text file
god bless u
There are bunch of things to improve in your code, Tim. From the code-quality standpoint
ahhhhh you look so cute