Show HN: Hangman in 3 lines of Python
danverbraganza.com
danverbraganza.com
"""To write out all the code and make minor refactorings took around 30 minutes. It took me way longer than that to write this blog post detailing exactly how it works. If I wanted to draw a moral from an exercise I entered into purely for amusement, I would have to repeat the oft-quoted wisdom that code that is readable and understandable is going to save you much more time in the long run."""
See my 52 char fizz buzz in js: https://github.com/codeocelot/fizzbuzz_oneliner/
That's a good first stab at it, but it can definitely be smaller, esp. if you're willing to add more lines (which I'm not). Happy hacking!
It takes quite a few more characters, but it was fun.
Instead of minimizing it to least amount of lines/chars, you have to maximize the clarity of the code such that the rest of us laymen can understand it.
GO!!!!
license, chosen_word, guesses, scaffold, man, guesses_left = 'https://opensource.org/licenses/MIT', ''.join(filter(str.isalpha, __import__('random').choice(open('/usr/share/dict/words').readlines()).upper())), set(), '|======\n| |\n| {3} {0} {5}\n| {2}{1}{4}\n| {6} {7}\n| {8} {9}\n|', list('OT-\\-//\\||'), 10
while not all(letter in guesses for letter in chosen_word) and guesses_left: _, guesses_left = list(map(guesses.add, filter(str.isalpha, (__builtins__.raw_input if 'raw_input' in dir(__builtins__) else __builtins__.input)('%s(%s guesses left)\n%s\n%s:' % (','.join(sorted(guesses)), guesses_left, scaffold.format(*(man[:10-guesses_left] + [' '] * guesses_left)), ' '.join(letter if letter in guesses else '_' for letter in chosen_word))).upper()))), max((10 - len(guesses - set(chosen_word))), 0)
print('You', ['lose!\n' + scaffold.format(*man), 'win!'][bool(guesses_left)], '\nWord was', chosen_word) d: ("aardvark";"apple";"peanut";"etc"); / dictionary
w: d[*1?#d]; / pick a word
c: {|/w=/:x}; / correctly guessed
b: {{$[x 0;x 1;"_"]}'+(c[x];w)}; / word with blanks
p: {`0:" > ";*0:`}; / prompt for a letter
{~&/c[x]}{`0:b[x]; x,p[]}/"__"; `0:w,"\n";
https://github.com/JohnEarnest/ok/blob/gh-pages/examples/han...EDIT: Realized words from usr/.. word list, not internet.
w: *1?("aardvark";"apple";"peanut";"etc") / pick a word
c: {|/w=/:x} / correctly guessed
b: {("_",'w)@'c x} / word with blanks
p: {`0:" > ";*0:`} / prompt for a letter
{~&/c x}{`0:b x; x,p[]}/"__"; `0:w,"\n";Thanks for doing this write up.
The functional style of the code reminded me of Haskell.
Except for Arch Linux, apparently. It requires the words package, which then links to the file corresponding to your locale.
I do think the program itself is pretty neat and never would have thought of using the word list for hangman.
That said, I think this is a neat, small, working, fun implementation of Hangman, and using the system dictionary is a great idea.