Created
February 13, 2018 06:20
-
-
Save Sparrow1029/ffc9f565ab5c614271e359d59e08be7a to your computer and use it in GitHub Desktop.
PyBites Code Challenge 10 - hangman game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hang_graphics(): | |
"""graphs from https://gist.github.com/devdarren/4199441""" | |
yield """ | |
________ | |
| | | |
| | |
| | |
| | |
|""" | |
yield """ | |
________ | |
| | | |
| 0 | |
| | |
| | |
|""" | |
yield """ | |
________ | |
| | | |
| 0 | |
| / | |
| | |
|""" | |
yield """ | |
________ | |
| | | |
| 0 | |
| /| | |
| | |
|""" | |
yield """ | |
________ | |
| | | |
| 0 | |
| /|\ | |
| | |
|""" | |
yield """ | |
________ | |
| | | |
| 0 | |
| /|\ | |
| / | |
|""" | |
yield """ | |
________ | |
| | | |
| 0 | |
| /|\ | |
| / \ | |
|""" | |
if __name__ == '__main__': | |
graphics = list(hang_graphics()) | |
print(len(graphics)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from string import ascii_lowercase | |
import sys, os | |
from movies import get_movie as get_word # keep interface generic | |
from graphics import hang_graphics | |
ASCII = list(ascii_lowercase) | |
HANG_GRAPHICS = list(hang_graphics()) | |
ALLOWED_GUESSES = len(HANG_GRAPHICS) | |
# PLACEHOLDER = '_' | |
class Hangman(object): | |
"""Main game class""" | |
def __init__(self, word): | |
"""Storing correct guess values as dict of 0 - 1 switches. | |
self.hidden displays current part of word player has guessed.""" | |
self.word = word | |
self.guessed = [] | |
self.wrong = 0 | |
self.right = {x.lower(): 0 for x in set(filter(lambda x: x.lower() in | |
ascii_lowercase, word))} | |
self.hidden = [c if c.lower() not in ascii_lowercase else '_' for c in word] | |
def guess(self): | |
"""Handles player input and returns their guess.""" | |
while True: | |
letter = input('Guess: ') | |
try: | |
int(letter) | |
print("Letters only please.") | |
continue | |
except ValueError: | |
pass | |
if len(letter) > 1: | |
print('One at a time, hoss.') | |
continue | |
elif letter.lower() not in ascii_lowercase: | |
print("Letters only please.") | |
continue | |
if letter.lower() in self.guessed: | |
print("Already guessed that one.") | |
continue | |
break | |
self.guessed.append(letter.lower()) | |
return letter.lower() | |
def display(self): | |
"""Function to handle displaying current game state.""" | |
print(HANG_GRAPHICS[self.wrong]) | |
print('\nPrevious guesses: {}\n'.format( | |
' '.join([s.upper() for s in self.guessed]))) | |
print(''.join(self.hidden)) | |
def update_hidden(self, letter): | |
"""Helper function to update what part of word is displayed.""" | |
word = self.word | |
letter = letter.lower() | |
for i in range(len(word)): | |
if word[i].lower() == letter: | |
self.hidden[i] = word[i] | |
def chk(self, letter): | |
"""Check correctness of guess.""" | |
c = letter.lower() | |
if c in self.right.keys(): | |
self.right[c] = 1 | |
self.update_hidden(letter) | |
else: | |
self.wrong += 1 | |
def win(self): | |
"""Test for win state.""" | |
if all(x == 1 for x in self.right.values()): | |
return True | |
return False | |
def lose(self): | |
"""Test for losing state.""" | |
if self.wrong >= ALLOWED_GUESSES-1: | |
return True | |
return False | |
if __name__ == '__main__': | |
if len(sys.argv) > 1: | |
word = sys.argv[1] | |
else: | |
word = get_word() | |
game = Hangman(word) # Instantiate game class | |
# init main game loop | |
while True: | |
os.system('clear') | |
# print(word) | |
# print(game.right) | |
game.display() | |
letter = game.guess() | |
game.chk(letter) | |
if game.win(): | |
os.system('clear') | |
print(word, end="\n\n") | |
sys.exit("Nice job! You win!") | |
elif game.lose(): | |
os.system('clear') | |
game.display() | |
sys.exit("ACK! You Died. Better luck next time, pardner.") | |
else: | |
continue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import re | |
MOVIE_TITLE = re.compile(r'\d+\.\s+(.*)\s\(.*').sub | |
def get_movie(): | |
"""Source 100 movies: | |
http://www.infoplease.com/ipea/A0760906.html""" | |
with open('movies.txt') as f: | |
rand_line = random.choice(f.readlines()) | |
return MOVIE_TITLE(r'\1', rand_line.rstrip()) | |
if __name__ == '__main__': | |
movie = get_movie() | |
print(movie) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Citizen Kane (1941) | |
2. The Godfather (1972) | |
3. Casablanca (1942) | |
4. Raging Bull (1980) | |
5. Singin' in the Rain (1952) | |
6. Gone with the Wind (1939) | |
7. Lawrence of Arabia (1962) | |
8. Schindler's List (1993) | |
9. Vertigo (1958) | |
10. The Wizard of Oz (1939) | |
11. City Lights (1931) | |
12. The Searchers (1956) | |
13. Star Wars (1977) | |
14. Psycho (1960) | |
15. 2001: A Space Odyssey (1968) | |
16. Sunset Blvd. (1950) | |
17. The Graduate (1967) | |
18. The General (1927) | |
19. On the Waterfront (1954) | |
20. It's a Wonderful Life (1946) | |
21. Chinatown (1974) | |
22. Some Like It Hot (1959) | |
23. The Grapes of Wrath (1940) | |
24. E.T. The Extra-Terrestrial (1982) | |
25. To Kill A Mockingbird (1962) | |
26. Mr. Smith Goes to Washington (1939) | |
27. High Noon (1952) | |
28. All About Eve (1950) | |
29. Double Indemnity (1944) | |
30. Apocalypse Now (1979) | |
31. The Maltese Falcon (1941) | |
32. The Godfather Part II (1974) | |
33. One Flew Over the Cuckoo's Nest (1975) | |
34. Snow White and the Seven Dwarfs (1937) | |
35. Annie Hall (1977) | |
36. The Bridge on the River Kwai (1957) | |
37. The Best Years of Our Lives (1946) | |
38. The Treasure of the Sierra Madre (1948) | |
39. Dr. Strangelove (1964) | |
40. The Sound of Music (1965) | |
41. King Kong (1933) | |
42. Bonnie and Clyde (1967) | |
43. Midnight Cowboy (1969) | |
44. The Philadelphia Story (1940) | |
45. Shane (1953) | |
46. It Happened One Night (1934) | |
47. A Streetcar Named Desire (1951) | |
48. Rear Window (1954) | |
49. Intolerance (1916) | |
50. The Lord of the Rings: The Fellowship of the Ring (2001) | |
51. West Side Story (1961) | |
52. Taxi Driver (1976) | |
53. The Deer Hunter (1978) | |
54. M*a*s*h (1970) | |
55. North By Northwest (1959) | |
56. Jaws (1977) | |
57. Rocky (1976) | |
58. The Gold Rush (1925) | |
59. Nashville (1975) | |
60. Duck Soup (1933) | |
61. Sullivan's Travels (1958) | |
62. American Graffiti (1973) | |
63. Cabaret (1972) | |
64. Network (1976) | |
65. The African Queen (1951) | |
66. Raiders of the Lost Ark (1981) | |
67. Who's Afraid of Virginia Woolf? (1966) | |
68. Unforgiven (1992) | |
69. Tootsie (1982) | |
70. A Clockwork Orange (1971) | |
71. Saving Private Ryan (1998) | |
72. The Shawshank Redemption (1994) | |
73. Butch Cassidy and the Sundance Kid (1969) | |
74. The Silence of the Lambs (1991) | |
75. In the Heat of the Night (1967) | |
76. Forrest Gump (1994) | |
77. All the President's Men (1976) | |
78. Modern Times (1936) | |
79. The Wild Bunch (1969) | |
80. The Apartment (1960) | |
81. Spartacus (1960) | |
82. Sunrise (1927) | |
83. Titanic (1997) | |
84. Easy Rider (1969) | |
85. A Night at the Opera (1935) | |
86. Platoon (1986) | |
87. 12 Angry Men (1957) | |
88. Bringing Up Baby (1938) | |
89. The Sixth Sense (1999) | |
90. Swing Time (1936) | |
91. Sophie's Choice (1982) | |
92. Goodfellas (1990) | |
93. The French Connection (1971) | |
94. Pulp Fiction (1994) | |
95. The Last Picture Show (1971) | |
96. Do the Right Thing (1989) | |
97. Blade Runner (1982) | |
98. Yankee Doodle Dandy (1942) | |
99. Toy Story (1995) | |
100. Ben-Hur (1959) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment