mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-29 06:05:36 -08:00
264
44 Hangman/python/hangman.py
Normal file → Executable file
264
44 Hangman/python/hangman.py
Normal file → Executable file
@@ -1,13 +1,122 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# HANGMAN
|
||||
#
|
||||
# Converted from BASIC to Python by Trevor Hobson
|
||||
# Converted from BASIC to Python by Trevor Hobson and Daniel Piron
|
||||
|
||||
import random
|
||||
|
||||
print(" " * 32 + "HANGMAN")
|
||||
print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n")
|
||||
|
||||
class Canvas:
|
||||
""" For drawing text-based figures """
|
||||
|
||||
def __init__(self, width=12, height=12, fill=" "):
|
||||
self._buffer = []
|
||||
for _ in range(height):
|
||||
line = []
|
||||
for _ in range(width):
|
||||
line.append("")
|
||||
self._buffer.append(line)
|
||||
|
||||
self.clear()
|
||||
|
||||
def clear(self, fill=" "):
|
||||
for row in self._buffer:
|
||||
for x in range(len(row)):
|
||||
row[x] = fill
|
||||
|
||||
def render(self):
|
||||
lines = []
|
||||
for line in self._buffer:
|
||||
# Joining by the empty string ("") smooshes all of the
|
||||
# individual characters together as one line.
|
||||
lines.append("".join(line))
|
||||
return "\n".join(lines)
|
||||
|
||||
def put(self, s, x, y):
|
||||
# In an effort to avoid distorting the drawn image, only write the
|
||||
# first character of the given string to the buffer.
|
||||
self._buffer[y][x] = s[0]
|
||||
|
||||
|
||||
def init_gallows(canvas):
|
||||
for i in range(12):
|
||||
canvas.put("X", 0, i)
|
||||
for i in range(7):
|
||||
canvas.put("X", i, 0)
|
||||
canvas.put("X", 6, 1)
|
||||
|
||||
|
||||
def draw_head(canvas):
|
||||
canvas.put("-", 5, 2)
|
||||
canvas.put("-", 6, 2)
|
||||
canvas.put("-", 7, 2)
|
||||
canvas.put("(", 4, 3)
|
||||
canvas.put(".", 5, 3)
|
||||
canvas.put(".", 7, 3)
|
||||
canvas.put(")", 8, 3)
|
||||
canvas.put("-", 5, 4)
|
||||
canvas.put("-", 6, 4)
|
||||
canvas.put("-", 7, 4)
|
||||
|
||||
|
||||
def draw_body(canvas):
|
||||
for i in range(5, 9, 1):
|
||||
canvas.put("X", 6, i)
|
||||
|
||||
|
||||
def draw_right_arm(canvas):
|
||||
for i in range(3, 7):
|
||||
canvas.put("\\", i - 1, i)
|
||||
|
||||
|
||||
def draw_left_arm(canvas):
|
||||
canvas.put("/", 10, 3)
|
||||
canvas.put("/", 9, 4)
|
||||
canvas.put("/", 8, 5)
|
||||
canvas.put("/", 7, 6)
|
||||
|
||||
|
||||
def draw_right_leg(canvas):
|
||||
canvas.put("/", 5, 9)
|
||||
canvas.put("/", 4, 10)
|
||||
|
||||
|
||||
def draw_left_leg(canvas):
|
||||
canvas.put("\\", 7, 9)
|
||||
canvas.put("\\", 8, 10)
|
||||
|
||||
|
||||
def draw_left_hand(canvas):
|
||||
canvas.put("\\", 10, 2)
|
||||
|
||||
|
||||
def draw_right_hand(canvas):
|
||||
canvas.put("/", 2, 2)
|
||||
|
||||
|
||||
def draw_left_foot(canvas):
|
||||
canvas.put("\\", 9, 11)
|
||||
canvas.put("-", 10, 11)
|
||||
|
||||
|
||||
def draw_right_foot(canvas):
|
||||
canvas.put("-", 2, 11)
|
||||
canvas.put("/", 3, 11)
|
||||
|
||||
|
||||
PHASES = (
|
||||
("First, we draw a head", draw_head),
|
||||
("Now we draw a body.", draw_body),
|
||||
("Next we draw an arm.", draw_right_arm),
|
||||
("this time it's the other arm.", draw_left_arm),
|
||||
("Now, let's draw the right leg.", draw_right_leg),
|
||||
("This time we draw the left leg.", draw_left_leg),
|
||||
("Now we put up a hand.", draw_left_hand),
|
||||
("Next the other hand.", draw_right_hand),
|
||||
("Now we draw one foot", draw_left_foot),
|
||||
("Here's the other foot -- you're hung!!", draw_right_foot)
|
||||
)
|
||||
|
||||
|
||||
words = ["GUM", "SIN", "FOR", "CRY", "LUG", "BYE", "FLY",
|
||||
"UGLY", "EACH", "FROM", "WORK", "TALK", "WITH", "SELF",
|
||||
@@ -20,124 +129,89 @@ words = ["GUM", "SIN", "FOR", "CRY", "LUG", "BYE", "FLY",
|
||||
"MATRIMONIAL", "PARASYMPATHOMIMETIC", "THIGMOTROPISM"]
|
||||
|
||||
|
||||
def play_game(guessTarget):
|
||||
"""Play the game"""
|
||||
guessWrong = 0
|
||||
guessProgress = ["-"] * len(guessTarget)
|
||||
guessList = []
|
||||
gallows = [([" "] * 12) for i in range(12)]
|
||||
for i in range(12):
|
||||
gallows[i][0] = "X"
|
||||
for i in range(7):
|
||||
gallows[0][i] = "X"
|
||||
gallows[1][6] = "X"
|
||||
guessCount = 0
|
||||
def play_game(guess_target):
|
||||
"""Play one round of the game"""
|
||||
wrong_guesses = 0
|
||||
guess_progress = ["-"] * len(guess_target)
|
||||
guess_list = []
|
||||
|
||||
gallows = Canvas()
|
||||
init_gallows(gallows)
|
||||
|
||||
guess_count = 0
|
||||
while True:
|
||||
print("Here are the letters you used:")
|
||||
print(",".join(guessList) + "\n")
|
||||
print("".join(guessProgress) + "\n")
|
||||
guessLetter = ""
|
||||
guessWord = ""
|
||||
while guessLetter == "":
|
||||
guessLetter = input("What is your guess? ").upper()[0]
|
||||
if not guessLetter.isalpha():
|
||||
guessLetter = ""
|
||||
print(",".join(guess_list) + "\n")
|
||||
print("".join(guess_progress) + "\n")
|
||||
guess_letter = ""
|
||||
guess_word = ""
|
||||
while guess_letter == "":
|
||||
|
||||
guess_letter = input("What is your guess? ").upper()[0]
|
||||
if not guess_letter.isalpha():
|
||||
guess_letter = ""
|
||||
print("Only letters are allowed!")
|
||||
elif guessLetter in guessList:
|
||||
guessLetter = ""
|
||||
elif guess_letter in guess_list:
|
||||
guess_letter = ""
|
||||
print("You guessed that letter before!")
|
||||
guessList.append(guessLetter)
|
||||
guessCount = guessCount + 1
|
||||
if guessLetter in guessTarget:
|
||||
indices = [i for i, letter in enumerate(guessTarget) if letter == guessLetter]
|
||||
|
||||
guess_list.append(guess_letter)
|
||||
guess_count += 1
|
||||
if guess_letter in guess_target:
|
||||
indices = [i for i, letter in enumerate(guess_target) if letter == guess_letter]
|
||||
for i in indices:
|
||||
guessProgress[i] = guessLetter
|
||||
if guessProgress == guessTarget:
|
||||
guess_progress[i] = guess_letter
|
||||
if guess_progress == guess_target:
|
||||
print("You found the word!")
|
||||
break
|
||||
else:
|
||||
print("\n" + "".join(guessProgress) + "\n")
|
||||
while guessWord == "":
|
||||
guessWord = input("What is your guess for the word? ").upper()
|
||||
if not guessWord.isalpha():
|
||||
guessWord = ""
|
||||
print("\n" + "".join(guess_progress) + "\n")
|
||||
while guess_word == "":
|
||||
guess_word = input("What is your guess for the word? ").upper()
|
||||
if not guess_word.isalpha():
|
||||
guess_word = ""
|
||||
print("Only words are allowed!")
|
||||
if guessWord == guessTarget:
|
||||
print("Right!! It took you", guessCount, "guesses!")
|
||||
if guess_word == guess_target:
|
||||
print("Right!! It took you", guess_count, "guesses!")
|
||||
break
|
||||
else:
|
||||
guessWrong = guessWrong + 1
|
||||
comment, draw_bodypart = PHASES[wrong_guesses]
|
||||
|
||||
print(comment)
|
||||
draw_bodypart(gallows)
|
||||
print(gallows.render())
|
||||
|
||||
wrong_guesses += 1
|
||||
print("Sorry, that letter isn't in the word.")
|
||||
if guessWrong == 1:
|
||||
print("First, we draw the head.")
|
||||
for i in range(5, 8):
|
||||
gallows[2][i] = "-"
|
||||
gallows[4][i] = "-"
|
||||
gallows[3][4] = "("
|
||||
gallows[3][5] = "."
|
||||
gallows[3][7] = "."
|
||||
gallows[3][8] = ")"
|
||||
elif guessWrong == 2:
|
||||
print("Now we draw a body.")
|
||||
for i in range(5, 9):
|
||||
gallows[i][6] = "X"
|
||||
elif guessWrong == 3:
|
||||
print("Next we draw an arm.")
|
||||
for i in range(3, 7):
|
||||
gallows[i][i-1] = "\\"
|
||||
elif guessWrong == 4:
|
||||
print("This time it's the other arm.")
|
||||
for i in range(3, 7):
|
||||
gallows[i][13-i] = "/"
|
||||
elif guessWrong == 5:
|
||||
print("Now, let's draw the right leg.")
|
||||
gallows[9][5] = "/"
|
||||
gallows[10][4] = "/"
|
||||
elif guessWrong == 6:
|
||||
print("This time we draw the left leg.")
|
||||
gallows[9][7] = "\\"
|
||||
gallows[10][8] = "\\"
|
||||
elif guessWrong == 7:
|
||||
print("Now we put up a hand.")
|
||||
gallows[2][10] = "\\"
|
||||
elif guessWrong == 8:
|
||||
print("Next the other hand.")
|
||||
gallows[2][2] = "/"
|
||||
elif guessWrong == 9:
|
||||
print("Now we draw one foot.")
|
||||
gallows[11][9] = "\\"
|
||||
gallows[11][10] = "-"
|
||||
elif guessWrong == 10:
|
||||
print("Here's the other foot -- You're hung!!.")
|
||||
gallows[11][2] = "-"
|
||||
gallows[11][3] = "/"
|
||||
for i in range(12):
|
||||
print("".join(gallows[i]))
|
||||
print("\n")
|
||||
if guessWrong == 10:
|
||||
print("Sorry, you lose. The word was " + guessTarget)
|
||||
|
||||
if wrong_guesses == 10:
|
||||
print("Sorry, you lose. The word was " + guess_target)
|
||||
break
|
||||
|
||||
|
||||
def main():
|
||||
"""Main"""
|
||||
print(" " * 32 + "HANGMAN")
|
||||
print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n")
|
||||
|
||||
random.shuffle(words)
|
||||
wordCurrent = 0
|
||||
wordCount = 49
|
||||
current_word = 0
|
||||
word_count = len(words)
|
||||
|
||||
keep_playing = True
|
||||
while keep_playing:
|
||||
play_game(words[wordCurrent])
|
||||
wordCurrent = wordCurrent + 1
|
||||
if wordCurrent >= wordCount:
|
||||
|
||||
play_game(words[current_word])
|
||||
current_word += 1
|
||||
|
||||
if current_word == word_count:
|
||||
print("You did all the words!!")
|
||||
keep_playing = False
|
||||
else:
|
||||
keep_playing = input("Want another word? (yes or no) ").lower().startswith("y")
|
||||
|
||||
print("It's been fun! Bye for now.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user