-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
168 lines (135 loc) · 5.17 KB
/
hangman.py
File metadata and controls
168 lines (135 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Name: Laser Nite
# MIT Username: nite
# 6.S189/6.149 Project 1: Hangman
# hangman.py
# Import statements: DO NOT delete these! DO NOT write code above this!
from random import randrange
from string import *
from hangman_lib import *
# -----------------------------------
# Helper code
# (you don't need to understand this helper code)
# Import hangman words
WORDLIST_FILENAME = "words.txt"
def load_words():
"""
Returns a list of valid words. Words are strings of lowercase letters.
Depending on the size of the word list, this function may
take a while to finish.
"""
print "Loading word list from file..."
# inFile: file
inFile = open(WORDLIST_FILENAME, 'r', 0)
# line: string
line = inFile.readline()
# wordlist: list of strings
wordlist = split(line)
print " ", len(wordlist), "words loaded."
print 'Enter play_hangman() to play a game of hangman!'
return wordlist
# actually load the dictionary of words and point to it with
# the words_dict variable so that it can be accessed from anywhere
# in the program
words_dict = load_words()
# Run get_word() within your program to generate a random secret word
# by using a line like this within your program:
# secret_word = get_word()
def get_word():
"""
Returns a random word from the word list
"""
word=words_dict[randrange(0,len(words_dict))]
return word
# end of helper code
# -----------------------------------
# CONSTANTS
MAX_GUESSES = 6
# GLOBAL VARIABLES
secret_word = get_word()
letters_guessed = []
joined_guessed = []
def guesses():
'''
Prints a string that contains the word with a dash "-" in
place of letters not guessed yet.
'''
global secret_word
global letters_guessed
global joined_guessed
# Make list of guessed letters and dashes
guessed_list = []
for letter in secret_word:
if letter in letters_guessed:
guessed_list.append(secret_word[secret_word.index(letter)])
else:
guessed_list.append("-")
# Join the list entries together into a string of letters and dashes and print it
joined_guessed = join(guessed_list, "")
print joined_guessed
def play_hangman():
# Actually play the hangman game
global secret_word
global letters_guessed
global joined_guessed
# Put the mistakes_made variable here, since you'll only use it in this function
mistakes_made = 0
# Copyright Credit
print "Game developed by Laser Nite, Art created by sk"
# Print starting graphic
print_hangman_image(0)
# Show number of letters to player as dashes
print "\n"
guesses()
print "\n"
# When 6 mistakes have been made, the game is over
while mistakes_made < 6:
# Get guess from user and make lowercase
guess = raw_input("Enter a letter to guess: ").lower()
print ""
# Response to repeat guess of letter in secret word
if guess in letters_guessed and guess in joined_guessed:
print_hangman_image(mistakes_made)
print "Letter already guessed \n"
print "Letters guessed are: " + join(letters_guessed, " ") + "\n"
# Response to repeat guess of letter not in secret word
elif guess in letters_guessed:
# Reverse mistake count for incorrect letter already guessed
mistakes_made -= 1
print "Letter already guessed \n"
# Add the guess to list of guesses if passes repeat checks above
else:
letters_guessed.append(guess)
# Check if correct guess and render response
# Response to correct guess
if guess in secret_word:
# Print Graphic, Correct, Number of guesses left, and Dashes with known letters
print_hangman_image(mistakes_made)
print "Correct Guess! " + str(6 - mistakes_made) + " guesses left!\n"
guesses()
print ""
# If all letters are guessed, the player wins
if joined_guessed == secret_word:
print_hangman_image(0)
print "You Saved Him! You Win! \n"
# Copyright Credit
print "Game developed by Laser Nite, Art created by sk"
break
print "Letters guessed are: " + join(letters_guessed, " ") + "\n"
# Response to incorrect guess
else:
# Increase mistakes made
mistakes_made += 1
# Print Graphic, Incorrect, Number of guesses left, Letters guessed, and Dashes/known letters
print_hangman_image(mistakes_made)
print "Incorrect Guess!\n"
if (6 - mistakes_made) > 0:
print str(6 - mistakes_made) + " guesses left!\n"
print "Letters guessed are: " + join(letters_guessed, " ") + "\n"
guesses()
print ""
# Player loses if no guesses left
else:
print "You Lose! \n"
print "The word was " + secret_word + "\n"
# Copyright Credit
print "Game developed by Laser Nite, Art created by sk"