|
|
@ -3,7 +3,7 @@ import random
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getGuess(number):
|
|
|
|
def get_guess(number):
|
|
|
|
validGuess = False
|
|
|
|
validGuess = False
|
|
|
|
guess = ""
|
|
|
|
guess = ""
|
|
|
|
while not validGuess:
|
|
|
|
while not validGuess:
|
|
|
@ -21,23 +21,21 @@ def resource_path(relative_path):
|
|
|
|
return os.path.join(base_path, relative_path)
|
|
|
|
return os.path.join(base_path, relative_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getWord():
|
|
|
|
# Gets a random word from a words.txt file present in the same directory as this script
|
|
|
|
|
|
|
|
def get_random_word():
|
|
|
|
lines = open(resource_path('words.txt')).read().splitlines()
|
|
|
|
lines = open(resource_path('words.txt')).read().splitlines()
|
|
|
|
return random.choice(lines)
|
|
|
|
return random.choice(lines)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def checkRight(wordToGuess, guessSoFar):
|
|
|
|
# Compares the users input with the word to be guessed. Returns true if the guess is correct, false otherwise
|
|
|
|
if str(wordToGuess).upper() == str(guessSoFar).upper():
|
|
|
|
def check_word_is_right(word_guessed, word_to_guess):
|
|
|
|
|
|
|
|
if str(word_guessed).upper() == str(word_to_guess).upper():
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def quit():
|
|
|
|
# Makes a cool header.
|
|
|
|
input("Thanks for playing! Press ENTER to exit :)")
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def header():
|
|
|
|
def header():
|
|
|
|
return r" ______ ______ _ _____" + "\n" + \
|
|
|
|
return r" ______ ______ _ _____" + "\n" + \
|
|
|
|
r"| _ \ \ / / _ \| | | ____|" + "\n" + \
|
|
|
|
r"| _ \ \ / / _ \| | | ____|" + "\n" + \
|
|
|
@ -46,35 +44,54 @@ def header():
|
|
|
|
r"|_| |_| |____/|_____|_____|"
|
|
|
|
r"|_| |_| |____/|_____|_____|"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Welcome to...")
|
|
|
|
# Prints welcome messages
|
|
|
|
print(header())
|
|
|
|
def intro():
|
|
|
|
print("A totally original guessing game!")
|
|
|
|
print("Welcome to...")
|
|
|
|
emptyWordGuess = ["_"] * 5
|
|
|
|
print(header())
|
|
|
|
wrongLetters, rightLettersWrongPlace = set()
|
|
|
|
print("A totally original guessing game!")
|
|
|
|
wordToGuess = list(getWord().upper())
|
|
|
|
|
|
|
|
# print(wordToGuess)
|
|
|
|
|
|
|
|
|
|
|
|
# Prints the results, a thank-you message and gracefully exits
|
|
|
|
counter = 1
|
|
|
|
def game_over(user_was_right, correct_word):
|
|
|
|
while counter < 6:
|
|
|
|
if user_was_right:
|
|
|
|
userGuess = getGuess(counter)
|
|
|
|
print("Congratulations! The word was", ''.join(correct_word).capitalize())
|
|
|
|
emptyWordGuess = ["_"] * 5
|
|
|
|
else:
|
|
|
|
for i in range(len(userGuess)):
|
|
|
|
print("Ran out of guesses! The word was: ", "".join(correct_word))
|
|
|
|
if wordToGuess[i] == userGuess[i]:
|
|
|
|
input("Thanks for playing! Press ENTER to exit :)")
|
|
|
|
emptyWordGuess[i] = userGuess[i]
|
|
|
|
sys.exit(0)
|
|
|
|
elif userGuess[i] in wordToGuess:
|
|
|
|
|
|
|
|
if userGuess[i] in emptyWordGuess and userGuess[i] not in wordToGuess[i:]:
|
|
|
|
|
|
|
|
|
|
|
|
# Main game flow.
|
|
|
|
|
|
|
|
def game_logic():
|
|
|
|
|
|
|
|
not_in_word, present_in_word = set(), set()
|
|
|
|
|
|
|
|
word_to_be_guessed = list(get_random_word().upper())
|
|
|
|
|
|
|
|
counter = 1
|
|
|
|
|
|
|
|
while counter < 7:
|
|
|
|
|
|
|
|
users_guess_input = get_guess(counter)
|
|
|
|
|
|
|
|
users_guess_results = ["_"] * 5
|
|
|
|
|
|
|
|
for i in range(len(users_guess_input)):
|
|
|
|
|
|
|
|
if word_to_be_guessed[i] == users_guess_input[i]:
|
|
|
|
|
|
|
|
users_guess_results[i] = users_guess_input[i]
|
|
|
|
|
|
|
|
elif users_guess_input[i] in word_to_be_guessed:
|
|
|
|
|
|
|
|
if users_guess_input[i] in users_guess_results and users_guess_input[i] not in word_to_be_guessed[i:]:
|
|
|
|
break
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
rightLettersWrongPlace.add(userGuess[i])
|
|
|
|
present_in_word.add(users_guess_input[i])
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
wrongLetters.add(userGuess[i])
|
|
|
|
not_in_word.add(users_guess_input[i])
|
|
|
|
if checkRight(wordToGuess, emptyWordGuess):
|
|
|
|
if check_word_is_right(word_to_be_guessed, users_guess_results):
|
|
|
|
print("Congratulations! The word was", ''.join(wordToGuess).capitalize())
|
|
|
|
game_over(True, word_to_be_guessed)
|
|
|
|
quit()
|
|
|
|
print("Incorrect letters: " + ', '.join(not_in_word))
|
|
|
|
print("Incorrect letters: " + ', '.join(wrongLetters))
|
|
|
|
print("Correct letters in the wrong place: " + ', '.join(present_in_word))
|
|
|
|
print("Correct letters in the wrong place: " + ', '.join(rightLettersWrongPlace))
|
|
|
|
print("Result: " + " ".join(users_guess_results))
|
|
|
|
print("Result: " + " ".join(emptyWordGuess))
|
|
|
|
|
|
|
|
counter += 1
|
|
|
|
counter += 1
|
|
|
|
|
|
|
|
game_over(False, word_to_be_guessed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
intro()
|
|
|
|
|
|
|
|
game_logic()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Ran out of guesses! The word was: ", "".join(wordToGuess))
|
|
|
|
if __name__ == '__main__':
|
|
|
|
quit()
|
|
|
|
main()
|
|
|
|