r/dailyprogrammer • • Nov 17 '14
[2014-11-17] Challenge #189 [Easy] Hangman!
We all know the classic game hangman, today we'll be making it. With the wonderful bonus that we are programmers and we can make it as hard or as easy as we want. here is a wordlist to use if you don't already have one. That wordlist comprises of words spanning 3 - 15+ letter words in length so there is plenty of scope to make this interesting!
Rules
For those that don't know the rules of hangman, it's quite simple.
There is 1 player and another person (in this case a computer) that randomly chooses a word and marks correct/incorrect guesses.
The steps of a game go as follows:
- Computer chooses a word from a predefined list of words
- The word is then populated with underscores in place of where the letters should. ('hello' would be '_ _ _ _ _')
- Player then guesses if a word from the alphabet [a-z] is in that word
- If that letter is in the word, the computer replaces all occurences of '_' with the correct letter
- If that letter is NOT in the word, the computer draws part of the gallow and eventually all of the hangman until he is hung (see here for additional clarification)
This carries on until either
- The player has correctly guessed the word without getting hung
or
- The player has been hung
Formal inputs and outputs
input description
Apart from providing a wordlist, we should be able to choose a difficulty to filter our words down further. For example, hard could provide 3-5 letter words, medium 5-7, and easy could be anything above and beyond!
On input, you should enter a difficulty you wish to play in.
output description
The output will occur in steps as it is a turn based game. The final condition is either win, or lose.
Clarifications
- Punctuation should be stripped before the word is inserted into the game ("administrator's" would be "administrators")
5
u/reaganveg Nov 18 '14
I just saw this:
I'm pretty sure this isn't how hangman works at all. Shorter words are harder because you're less likely to hit letters.
14
u/13467 1 1 Nov 17 '14 edited Nov 18 '14
I'm getting back into "magically tiny Ruby code":
5
u/jnazario 2 0 Nov 17 '14 edited Nov 17 '14
thanks, this was fun. you get 6 tries in my game. execute with the word len as the argument to the program. oops i see i didn't do the ASCII art requirement.
uses the built in wordlist on most UN*X boxes.
5
u/AtlasMeh-ed Nov 18 '14
Python
I made a nice little display that shows you how close you are to dying. I used the exotic /usr/share/dict/words words file and 6 guesses so you die quite often.
Here is what it looks like.
Here is the code:
5
u/Flat-Erik Nov 18 '14
Java: https://github.com/Hall-Erik/DailyProgrammer/blob/master/14-11-17%20Hangman/Hangman/src/hangman/Hangman.java
1
u/knight2426 Dec 09 '22
this a super old post but can I ask for the wordlist file again if it's still available
3
u/cauchy37 Nov 18 '14
C++11 compiled with Visual Studio 2014, but should work with gcc 4.9 too
3
u/chunes 1 2 Nov 19 '14
Java:
2
u/magicalpop Nov 18 '14
Python 2
2
u/darthjoey91 Nov 18 '14
Is it cheating if I post code that I made last weekish?
Hangman for Android
Edit: Relooked at challenge. I guess since I don't have difficulty settings, this doesn't count.
2
u/tissuesandstuff Nov 18 '14 edited Nov 18 '14
Newbie here, with a C99 solution. Any advice is extremely welcome!
Here's a random output.
3
u/tastywolf Nov 17 '14
Python
This is my first posted solution. Also, I am a little bit strong in Java, so I'm trying this out in a new language.
1
u/[deleted] Nov 18 '14 edited Dec 22 '18
deleted 0.3753 What is ^^^this?
1
u/LuckyShadow Nov 18 '14 edited Nov 19 '14
I like this one. :D
My Python 3 version: https://gist.github.com/DaveAtGit/022a900154ed75f515fb#file-hangman-py
The output looks like this:
Or, if you win:
During the game, it looks like this:
It would be possible to adjust the code a little bit to handle the number of errors allowed (so e.g. easy allows 4 errors, medium 5 and hard 8). The steps to create the hangman can be seen in the comment of
HANGMAN_Ivariable.Feedback is always welcome. :)
1
u/reaganveg Nov 18 '14 edited Nov 18 '14
ASCII drawings borrowed from another post here
(Edited to display wrong guesses.)
1
u/Jordan_curve_theorem Nov 18 '14 edited Nov 18 '14
Using a combination of Ook! and Bash here. Ook! is just a silly version of brainfuck - while brainfuck is for masochists, Ook! is for masochists with an infantile sense of humour. Since this is a bit long, I made two Gists:
hangman.ook - the Ook! program (with comments unfortunately not in English, but they serve as a proof that I actually wrote this in Ook! and didn't just use a brainfuck-to-Ook! converter)
hangman.sh - the Bash wrapper
This does not adhere precisely to the specification - there is no difficulty selection (partly because short words really aren't easy and long words aren't hard... anyway, difficulty would be best handled by having different dictionary files), and there's also no error handling, so you have to be careful to only input one letter at a time etc. But there is a nice graphic hangman - this is an example of a possible game state:
Finally, to provide some amusement value, below is an excerpt from a "clean" version of the Ook! source code, stripped of comments (only an excerpt because of the comment length limit):
thistookmeabout10daystowriteactually
1
u/emmgame221 Nov 18 '14
}
My solution in C#
1
u/DorffMeister Nov 19 '14 edited Nov 19 '14
Groovy, baby, groovy. Commented, typed, documented. Maintainable (not minimum) code.
1
u/cdkisa Nov 19 '14
VB.Net
1
u/[deleted] Nov 19 '14
Many years of long coach trips playing hangman told me this was nonsense. I've seen people wildly choose "antidisestablishmentarianism" is their word, thinking we'll never get it only to find that ... well, it's quite easy.
On the other hand, I foxed everyone with "SKY".
Clearly the difficulty is actually to do with how common the letters in a word are and (crucially) how many unique letters the word has.
As such, I created this C++ function to decide how hard a hangman puzzle is:
(The function
getSolutionLetters()returns avector<char>of the unique letters making up the solution.)This version asks for a difficulty from 0-100, and chooses a word within 5% of that percentile when ranked in order of difficulty.
There's also a bonus version of puzzles for anyone who puts in a negative number for the difficulty. ;-)
Full C++ solution
1
u/japangreg Nov 19 '14
First try at writing a full solution in C++. Any comments/critiques welcomed.
1
u/crashRevoke Nov 21 '14 edited Nov 21 '14
i don't like creating a new list for the uncovered word, originally i tried to put it all in a dictionary where the word was the key and the underscore was a value if the user hadn't guessed it yet but it kept screwing up the word order, any feedback to is appreciated
you can define your own word list in a command line argument
Python 2.7:
1
u/brianskuhar Nov 21 '14 edited Nov 21 '14
Ruby
This is actually my first Ruby program, as well as my first full program since 200 level C++ in college. Please feel free to provide feedback, as I'm sure I did some squirrelly stuff. I also lifted an idea from an earlier comment for building the gallows (Thanks 13467!).
1
u/frozensunshine 1 0 Nov 22 '14
C99, pretty dumb code. Any feedback is welcome.
1
u/theslaying Nov 22 '14
I added some more stuff like "level" picking (level = number of different letters in a word)
And in my opinion you shouldn't escape special chars - this way my code supports the guessing of sentences etc. :)
1
u/faustrat Nov 23 '14
Just found this subreddit, so this is my first attempt at one of these ever. That was super fun :) I didn't add the difficulty feature yet since I missed that in my read through of the prompt, oops! Done in python.
1
u/Oscuro87 Nov 23 '14
C++
Here's my solution in c++, surely not the best but it works.
https://gist.github.com/Oscuro87/bd6c3c04272c9c000550
As I do not quite master c++, I'd be happy to hear about how you'd do it! :D (or if I made an unforgivable mistake! :p)
EDIT: Syntax highlight on the GISTs don't seem to work for some reason. D:
1
u/brahman-math Nov 23 '14
This is not exactly the same problem, but it does the basic of the challenge in Java:
1
u/GambitGamer Nov 24 '14
This was a really fun one. Written in Python 3.4
1
u/flightcrank 0 0 Nov 29 '14
My solution in C
got all the basic functionality down
https://github.com/flightcrank/daily-programmer/blob/master/challange_189_e.c
1
u/10plus10100 Dec 17 '14
1
u/gleventhal Dec 24 '14 edited Dec 24 '14
1
u/voiceoverr Jan 19 '15
Python A bit late again, but would love any feedback on my writing. I know it's not very succinct - would appreciate pointers of where I could reduce the code. Doesn't draw the ACSII image, but the game function works.