Skip to content

Instantly share code, notes, and snippets.

@ahmedsakr
Created February 25, 2015 14:19
Show Gist options
  • Save ahmedsakr/00efc394dc663c5acc2c to your computer and use it in GitHub Desktop.
Save ahmedsakr/00efc394dc663c5acc2c to your computer and use it in GitHub Desktop.
stickman game!
import random
import sys
# parallel lists, potential_words & potential_words_type. The word and it's type respectively.
potential_words = ["League of legends", "Gone Girl", "Runescape"]
potential_words_type = ["Game", "Movie", "Game"]
letters_to_guess = {}
# revealed_indicies list is used to check whether the certain index of the word has been guessed
# if it guessed, it's value is 1. otherwise 0.
revealed_indicies = []
def print_dotted_line():
print ("------------------------------------------")
# revalidates all underscores and checks which to reveal based on guessing.
# Finally, prints it to the user.
def print_combination(word):
count = 0
combination = ""
while count in range(len(word)):
if word[count] == " ":
combination += " "
elif revealed_indicies[count] == 1:
combination += word[count] + " "
else:
combination += "_ "
count += 1
print (combination.upper())
# Handles the guess input from the user.
# The user has a total of attempts_remaining attempts to guess the correct word.
# If all are consumed without completion, the user has lost and is prompted to play again
# or quit. They are also informed what the riddle is.
#
# If the user decrypted the riddle, then they are also promoted to play another game.
# If the user entered a letter that has already been guessed, they are informed and no
# penalty is enforced.
def handle_guess(attempts_remaining, word, letter):
for stored_letter in letters_to_guess:
if letter == stored_letter:
if revealed_indicies[word.index(letter)] == 1:
print_dotted_line()
print ("You already guessed this letter!")
print ("")
print_combination(word)
next_guess = input("Enter a letter: ")
handle_guess(attempts_remaining, word, next_guess)
letter_locations = letters_to_guess[letter]
for x in letter_locations:
revealed_indicies[x] = 1
game_complete = False
for x in revealed_indicies:
if x == 0:
game_complete = False
break
else:
game_complete = True
if game_complete == True:
print_dotted_line()
print ("Congratulations! You have guessed it correctly!")
print ("")
decision = input("Play Again? (Y/N) ")
if decision.upper() == "Y":
count = 0
revealed_indicies.clear()
letters_to_guess.clear()
start()
else:
sys.exit()
print_dotted_line()
print ("Correct guess!")
print ("")
print_combination(word)
next_guess = input("Enter a letter: ")
handle_guess(attempts_remaining, word, next_guess)
print_dotted_line()
attempts_remaining -= 1
if attempts_remaining == 0:
print ("You have failed to figure out the riddle. It is %s." % (word))
decision = input("Play Again? (Y/N) ")
if decision.upper() == "Y":
count = 0
revealed_indicies.clear()
letters_to_guess.clear()
start()
else:
sys.exit()
print ("Wrong! You have %.0f attempt(s) remaining" % (attempts_remaining))
print ("")
print_combination(word)
next_guess = input("Enter a letter: ")
handle_guess(attempts_remaining, word, next_guess)
# start a new game of stickman.
def start():
print_dotted_line()
print ("")
print ("Welcome to the stickman game!")
print ("A random word has been chosen. Start!")
print ("")
word_index = random.randint(0, len(potential_words) - 1)
word = potential_words[word_index].lower()
index = 0
for letter in word:
letter_already_stored = False
for value in letters_to_guess:
if letter == value:
letter_already_stored = True
letters_to_guess[letter].append(index)
if letter_already_stored == False and letter != " ":
letters_to_guess[letter] = [index]
index += 1
count = 0
combination = ""
while count in range(len(word)):
if word[count] == " ":
combination += " "
revealed_indicies.append(1)
else:
combination += "_ "
revealed_indicies.append(0)
count += 1
print (combination + " Genre: " + potential_words_type[word_index])
letter_guess = input("Enter a letter: ")
handle_guess(10, word, letter_guess)
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment