Skip to content

Instantly share code, notes, and snippets.

@chronus7
Last active August 29, 2015 14:09
Show Gist options
  • Save chronus7/022a900154ed75f515fb to your computer and use it in GitHub Desktop.
Save chronus7/022a900154ed75f515fb to your computer and use it in GitHub Desktop.
/r/dailyprogrammer #189 easy
# -*- coding: utf-8 -*-
# /r/dailyprogrammer #189 easy
# http://www.reddit.com/r/dailyprogrammer/comments/2mlfp/
# Title: [2014-11-17] Challenge #189 [Easy] Hangman!
""" Hangman
A small hangman-game.
_____
|/ | HANGMAN
| (.)
| \|/ GUESSES: ADEJKMSZ
| /^\ REMAINING: 0
/ \ _ _ _ _ O _
>> You are dead! Word: PYTHON
-- (2014) Dave J (https://github.com/DaveAtGit)
"""
# Imports
from argparse import ArgumentParser
import sys
import random
# Statics
HEIGHT = 8
WIDTH = 40
WORDLIST = "../wordlist.txt"
DIFFICULTIES = {
"easy": (3, 5),
"medium": (5, 7),
"hard": (7, 30) # just a guess. could be 100 at maximum as well..
} # would be possible to add initial hangman_iteration
# to vary number of steps possible
HANGMAN = """ _____
|/ |
| (.)
| \|/
| /^\\
/ \\"""
HANGMAN = [l for l in HANGMAN.split('\n')]
HANGMAN_I = [
((0, 5), (2, 5)), # stand
((1, 4), (1, 3), (1, 2), (1, 1)), # pole
((2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (2, 1)), # bar
((6, 1),), # gallows bird
((5, 2), (6, 2), (7, 2)), # head
((6, 3), (6, 4)), # torso
((5, 3), (7, 3)), # arms
((5, 4), (7, 4)) # legs
]
# Functions
def hang_man(matrix, iteration):
if iteration < 0:
return
coords = HANGMAN_I[iteration]
for x, y in coords:
matrix[y][x] = HANGMAN[y][x]
def write(matrix, x, y, text):
for i in range(len(text)):
matrix[y][x+i] = text[i]
def play(difficulty, wordlist):
d_l, d_h = DIFFICULTIES[difficulty]
r = range(d_l, d_h + 1)
with open(wordlist) as w:
words = list(filter(lambda x: len(x) in r,
(line.strip().replace("'", "").upper() for line in w)))
word = words[random.randint(0, len(words)-1)]
matrix = [[' ' for _ in range(WIDTH)] for _ in range(HEIGHT)]
for tpl in ((15, 1, "HANGMAN"),
(11, 3, "GUESSES:"),
(11, 4, "REMAINING:"),
(0, 7, ">>")):
write(matrix, *tpl)
guesses = ""
fails = -1
mjoin = lambda: '\n'.join(''.join(line).rstrip() for line in matrix)
wguesses = lambda: write(matrix, 20, 3, ''.join(sorted(i for i in guesses if i not in word)))
wremaining = lambda: write(matrix, 22, 4, str(len(HANGMAN_I) - fails - 1))
wword = lambda: write(matrix, 11, 5, ' '.join(('_', c)[c in guesses] for c in word))
update = lambda: [i() for i in (wword, wguesses, wremaining)]
last_words = " Congratulations. You got it."
while fails < len(HANGMAN_I) - 1:
update()
print(mjoin() + " Choose the next letter: ", end='')
c = sys.stdin.readline().strip()
if c is None or len(c) == 0: # no effective input
continue
c = c[0].upper()
if c in guesses: # already used. let's be fair
continue
guesses += c
if c not in word:
fails += 1
hang_man(matrix, fails)
else:
if all(i in guesses for i in word):
break
else:
last_words = " You are dead! Word: " + word
update()
print(mjoin() + last_words)
def main():
" reads the arguments "
ap = ArgumentParser()
ap.add_argument("difficulty", choices=DIFFICULTIES.keys(),
help="Choose the difficulty of the game.")
ap.add_argument("-w", "--wordlist", type=str, default=WORDLIST,
help="The alternative wordlist to use.")
args = ap.parse_args()
play(args.difficulty, args.wordlist)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment