Created
May 14, 2013 05:35
-
-
Save jacks205/5573924 to your computer and use it in GitHub Desktop.
Hangman Simulation. Takes words out of a words.txt file.
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; | |
# Repeatedly play the game of Hangman: | |
def play(): | |
maxAllowedAttempts = 8; | |
print rulesDisplay(maxAllowedAttempts); | |
listOfWords = inputListOfWords('words.txt'); | |
while (True): | |
playOneGame(maxAllowedAttempts, listOfWords); | |
ans = inputYesNo('\nPlay more? [Yes/No]: '); | |
if(ans.lower() == 'no'): break; | |
print 'So long...'; | |
# Return the rules for the Hangman game: | |
def rulesDisplay(maxAllowedAttempts): | |
return \ | |
'This program plays the game of Hangman.\n' + \ | |
'You are to guess a secret word by typing one or more adjacent letters.\n' + \ | |
'You can make at most ' + str(maxAllowedAttempts) + ' attempts.\n'; | |
# Input words from file and return a list of all words: | |
def inputListOfWords(fileName): | |
inFile = open(fileName, 'rU'); # read in "universal end-of-line" mode | |
listOfWords = []; | |
for line in inFile: | |
# Strip unnecessary '\n' from right end: | |
line = line.rstrip('\n'); | |
if (len(line) > 0 and line.isalpha()): | |
listOfWords.append(line); | |
inFile.close(); | |
return listOfWords; | |
# Input valid answer for a Yes/No question while ignoring case: | |
def inputYesNo(prompt): | |
response = raw_input(prompt); | |
while (response.lower() != 'yes' and response.lower() != 'no'): | |
print 'Invalid response: ', response; | |
response = raw_input(prompt); | |
return response; | |
# Update the letters guessed so far with the current guess: | |
def update(lettersGuessedSoFar, guess, secretWord): | |
position = secretWord.find(guess); | |
while (position >=0): | |
for char in guess: | |
lettersGuessedSoFar[position] = char; | |
position = position + 1; | |
position = secretWord.find(guess, position); | |
# Play a single game of Hangman: | |
def playOneGame(maxAllowedAttempts, listOfWords): | |
secretWord = chooseSecretWord(listOfWords); | |
attempt = 1; | |
unknownLetter = '_'; | |
lettersGuessedSoFar = len(secretWord) * [unknownLetter]; | |
for attempt in range(1, maxAllowedAttempts + 1): | |
display = gameDisplay(attempt, lettersGuessedSoFar); | |
guess = inputGuess(display); | |
update(lettersGuessedSoFar, guess, secretWord); | |
if (lettersGuessedSoFar.count(unknownLetter) == 0): | |
print gameDisplay(attempt, lettersGuessedSoFar) + \ | |
'\tYou won!'; | |
return; | |
print gameDisplay(attempt, lettersGuessedSoFar) + \ | |
'\tYou lost. It is ' + secretWord + '.'; | |
# Select a random word from a list of words: and return it in uppercase: | |
def chooseSecretWord(listOfWords): | |
secretWord = random.choice(listOfWords); | |
return secretWord.upper(); | |
# Input player's guess and return it in uppercase: | |
def inputGuess(display): | |
guess = raw_input(display + '\tYour guess: '); | |
return guess.upper(); | |
# Return, as a string, the current attempt number and letters guessed so far: | |
def gameDisplay(attempt, lettersGuessedSoFar): | |
space = ' '; | |
string = '('+str(attempt)+')' + \ | |
':\t' + space.join(lettersGuessedSoFar); | |
return string; | |
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
Xena | |
Johnny | |
Mike | |
Monique | |
Patrick | |
Peter | |
Ryan | |
William | |
Aaron | |
Andrew | |
Bryan | |
Chris | |
Emmanuel | |
Gina | |
Jennifer | |
Kyle | |
Martha | |
Nick | |
Nicholas | |
Ryan | |
Takahiro | |
Tommy | |
Joshua |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment