first commit
This commit is contained in:
commit
98ac678602
|
@ -0,0 +1,13 @@
|
|||
# Hangman
|
||||
|
||||
## Description
|
||||
A CLI version of the popular children's game Hangman. Includes 20,000+ words!
|
||||
|
||||
## How To Use
|
||||
- Navigate inside tilde.town to /maddie/git/hangman/
|
||||
- python3 hangman.py
|
||||
- Have fun!
|
||||
|
||||
## Future Plans
|
||||
Fix some bugs and add replay feature; but this should be a working copy that works on Python3. It was originally written in 2.7.3 back in 2011.
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
|
||||
An implementation of the game Hangman.
|
||||
|
||||
"""
|
||||
|
||||
import random
|
||||
|
||||
word_file = open("words.txt")
|
||||
words = word_file.readlines()
|
||||
word = random.choice(words).strip()
|
||||
correct_guesses = []
|
||||
incorrect_guesses = []
|
||||
|
||||
print("""
|
||||
_ _
|
||||
| | | |
|
||||
| |__| | __ _ _ __ __ _ _ __ ___ __ _ _ __
|
||||
| __ |/ _` | '_ \ / _` | '_ ` _ \ / _` | '_ \
|
||||
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|
||||
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
|
||||
__/ |
|
||||
|___/
|
||||
|
||||
With over 20,000+ words!""")
|
||||
|
||||
|
||||
def print_string():
|
||||
string = ""
|
||||
for letter in word:
|
||||
if letter in correct_guesses:
|
||||
string += letter.upper() + " "
|
||||
else:
|
||||
string += "_ "
|
||||
print(string)
|
||||
|
||||
def main():
|
||||
print("")
|
||||
while len(incorrect_guesses) < 6:
|
||||
print("")
|
||||
print_string()
|
||||
guess = raw_input("Your guess: ").lower()
|
||||
if guess in correct_guesses or guess in incorrect_guesses:
|
||||
print("You've already guessed: " + guess)
|
||||
if guess in word:
|
||||
print("You guessed correctly! " +
|
||||
guess.upper() + " is in the word.")
|
||||
correct_guesses.append(guess)
|
||||
won = True
|
||||
for letter in word:
|
||||
if letter not in correct_guesses:
|
||||
won = False
|
||||
if won:
|
||||
print("Congratulations you discovered the mystery word and won!")
|
||||
break
|
||||
else:
|
||||
incorrect_guesses.append(guess)
|
||||
print("Incorrect! " + guess.upper() +
|
||||
" is not in the word! You have " +
|
||||
str(6 - len(incorrect_guesses)) +
|
||||
" guesses left.")
|
||||
|
||||
print("The mystery word was " + word.upper() + ".")
|
||||
print("Game Over!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Loading…
Reference in New Issue