473,543 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help making a hangman program

13 New Member
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!!!
Nov 26 '08 #1
25 3935
Dököll
2,364 Recognized Expert Top Contributor
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!
Nov 26 '08 #2
r035198x
13,262 MVP
For console applications, use the Scanner class to get the input from the user or the Console class if you are using Java 6.
Nov 26 '08 #3
Nepomuk
3,112 Recognized Expert Specialist
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
Nov 26 '08 #4
JosAH
11,448 Recognized Expert MVP
Also also, read about regular expression; in particular the String.replaceA ll() 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
Nov 26 '08 #5
yottabyte
13 New Member
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
Dec 1 '08 #6
Nepomuk
3,112 Recognized Expert Specialist
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.Rando m. 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
Expand|Select|Wrap|Line Numbers
  1. int lives = 5
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
Expand|Select|Wrap|Line Numbers
  1. String myString = "word";
  2. System.out.println(myString + " is " + myString.length() + " characters long.");
  3.  
That should get you going.

Greetings,
Nepomuk
Dec 1 '08 #7
tiktik
14 New Member
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,
tiktik
Dec 2 '08 #8
yottabyte
13 New Member
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.
Dec 3 '08 #9
JosAH
11,448 Recognized Expert MVP
@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:

Expand|Select|Wrap|Line Numbers
  1. if (used.indexOf(x) >= 0) // character already used
  2.  
if the above test fails 'x' represents a new guess, so:

Expand|Select|Wrap|Line Numbers
  1. used+= x; // now set x as used
  2. if (word.indexOf(x) < 0) // x is not in word
  3.  
If the above test fails then x is a character in the word, so:

Expand|Select|Wrap|Line Numbers
  1. correct+= x;
  2. String leftOver= word.replaceAll("[^"+correct+"]", "-");
  3.  
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:

Expand|Select|Wrap|Line Numbers
  1. boolean finished= leftOver.indexOf('-') < 0;
  2.  
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
Dec 3 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1657
by: rainbowii7 | last post by:
Calling all programmers for helllllllllllllllppppp!!! i am currently doing a uni degree and our lecturers have set us the task of making a game in JavaScript. i chose to do a hangman game and have completed the actual game. I have recently been in hospital as I am disabled and so have not been in the lessons where my lecturers have been...
5
15792
by: tigrfire | last post by:
So I'm trying to write a hangman game and the output is coming out a little strange. Here's my code thus far: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> int hangman()
4
2589
by: princessfrost | last post by:
Hi! I was wondering if someone could please help me with a hangman program that I have to do. I have some ideas, but really don't know what to do or where to start. My program needs to be: interactive with the user, menu based(like Menu: 1. Play hangman 2. Exit program), have a dictionaryand use a random method(create arrays of Strings or...
47
11429
by: araujo2nd | last post by:
Originally Posted by araujo2nd my name is andre, im from south africa, i was wondering if any1 could help me with a hangman application, im now in grade 11 and have a huge portfolio piece to do by the 15th which i have just started now, i have always struggled with java and would rele appreciate all the help that i can get. Pls could some1 help...
3
2941
by: kaka_hunter | last post by:
#include <iostream> #include <fstream> using namespace std; const int max_tries=7; int earnings=0; int wordnum; void getword () { ifstream fin;
5
4178
by: av3rage | last post by:
I have never done any programming in my life but I have decided to go into engineering and in doing so we have to take this intro to programming course and I am pretty clueless. I am starting to get the hang of how python works but to put my thoughts into the program to make it run is the confusing part for me. The task is to implement a...
8
3733
by: tidiz | last post by:
Hi, I'm trying to make a hangman game that should look like this: Welcome to Hangman ______ Your guess: c Success! __cc__ Your guess: b
3
9842
by: yottabyte | last post by:
Hey bytes, you may or may not remember but last time I was here a few months ago I got some help with making a hangman program which went well. Now I'm still doing okay in Java this year but I'm starting a new project for simple encryption and decryption. The way I set up the program to work is just to add 1 to each character to change a...
0
7729
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7677
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5258
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4884
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3385
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1809
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
626
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.