I have my project due of hangman and it is worrying me lately. I need to make a hangman game.
I have studied arrays, functions, loops, if else statements. That is about it. I haven't done 2d arrays. Will do it now, because someone told me it is not possible to make a hangman game of 40 words without 2d arrays.
My question to you is.
*how to store words for hangman? Say I saved them in arrays. But then how do i retrieve them?
*How to I randomly generate words for hangman?
*I know how to randomly generate numbers, but how to associate that random number with another word?
*How to compare that word with the user entered letters?
Some info regarding my game.
*I made seven categories. The user chooses one category. Every category has similar words but not having the same size. E.g I have a category for places. So the words may be like London, Zurich, Paris etc.
*Every category has no more than 7 words. Now how to randomly generate them?
*Also, it is bad to have different sized words in one simple category? I mean does it make the program more complex?
I really would appreciate your help. I have spent alot of hours figuring out things but i have failed.
Thank you for your time and cooperation.
You haven't failed. You just haven't gotten to the answer yet. I don't think the project is necessarily beyond what you've learned so far. If you have categories, then each category contains an array of words. If the number of words is up to 7, then each category must also keep track of how many words it actually has. If you know how to generate a random number, then you can use that as an index into an array to pick the corresponding word. I don't think it matters how long any one word is: from that point the program is the same, the user guesses whether a letter is present until all letters are accounted for. Don't forget that if a letter appears more than once in a word (for example the 'n' or 'o' in London), if the user guesses that letter, you need to show all occurrences at the same time.
Start: _ _ _ _ _ _
User guesses 'o': _ o _ _ o _
User guesses 'd': _ o _ d o _
And so on.
So, you need to keep track of (1) the original word, (2) which correct letters the user has tried, and what partial word to display, (3) which incorrect letters the user has tried, and how many (the user loses if too many wrong guesses), and (4) whether the word is finished (and the user has won).
Start simple and work up. Start by making your categories, and word lists for each, and printing them out. Then modify your program to prompt the user for a category, and print out only those words. Then modify it to generate a random number and print out only that word. Then you can start on the actual game-play. Also, don't be afraid to put lots of meaningful debug output in your program, telling you where it is in the code, and what it's doing. It's easy enough to remove all that once it works perfectly for you!
Thank you for that message. I have been able to successfully complete thr project.
For the readers, who are beginners, I would like to offer help in the following points:
I used 2d arrays for saving my words. And I used the randomn number generator to generate random numbers within the given range. Then I associated that number with the word in the 2d array and made that word the secret word which the user had to guess.
Commands like strcpy and strlen were very useful in copying the secret word as a string into the word to be guessed by the user and displayed on the screen. The length of the secret word was found out by the latter command.
Then, I used arrays for searching the input character by the user and to see if the character existed in the secret word. If it did, I would add that character to the secret word and display it on the screen for the user.
The BOOL operator has been very very useful in many many things. I was able to keep track of the characters which were NOT present in the secret word by simply doing bool=false and if the letter was found, I would do bool=true else it would remain false and go to the commands in the prgram which would then later increment on the chances I had given to the user. I also kept track of the incorrect letters guessed by the user. I simply added them to another array which couldn't hold more than 6 characters (which also happened to be the number of chances given to the user.)
So this was the general idea and working of my program: hangman man. If there are any queries, for the beginners, how to make this program, please let me know. I would be glad to help.
P.S. Thank you everyone who replied to my posts on Daniweb. I have been able to go forward with my project in a very smooth manner.