pystermind/main.py

169 lines
3.8 KiB
Python

# Pystermind 1.0
# By Wholesomedonut
# Read below for how-to-play, or input ? after running
import random
from witty_retort import MessageTable
remarks = MessageTable()
remarks.add_entry("Not quite", 30)
remarks.add_entry("Keep trying", 20)
remarks.add_entry("Intresting strategy", 15)
remarks.add_entry("Close", 10)
remarks.add_entry("Don't give up", 10)
remarks.add_entry("I though you had it", 10)
remarks.add_entry("You can do it!", 5)
""" Dummy class for Errors"""
class Error(Exception):
pass
""" Length of user input > or < 4 """
class LengthException(Error):
pass
""" Reports incorrect guess content """
class ColorException(Error):
pass
secret = []
guess = []
clue = []
game_continue = True
debug = False # Turn this flag on for easy debug prints, if that's your jam.
colors = ['r', 'b', 'g', 'y'] # Can easily be something else.
# Doesn't have to be 4 things, doesn't have to be colors.
rules = """
Input a string of 4 letters, representing colors.
r = red
b = blue
g = green
y = yellow
You will receive output from your guess that will tell you what you got right
and what you got wrong.
* = correct color in correct location
~ = correct color and wrong location
# = incorrect color in incorrect location
'?' for help.
'q' to quit.
"""
def format_message(message):
return message.rjust(25)
def generateSecret(secret):
for i in range(0, len(colors)):
secret.append(random.choice(colors))
if debug:
print(secret)
return secret
def checkGuess(guess):
if len(guess) > len(colors):
raise LengthException("Your guess is too long.")
elif len(guess) < len(colors):
raise LengthException("Your guess is too short.")
for i in guess:
if i not in colors:
raise ColorException("You are not guessing from r, g, b, or y.")
def compare(secret, guess, clue):
clue = []
for i in range(0, len(secret)):
if guess[i] == secret[i]:
clue.append('*')
elif guess[i] in secret:
clue.append('~')
else:
clue.append('#')
if debug:
out = ('Your guess: %s,\nthe secret: %s') % (guess, secret)
print(out)
return clue
def calc(secret, guess, clue):
tmp = ''
return tmp.join(clue) == '****'
generateSecret(secret)
print(rules)
while game_continue:
guess = []
temp = input(format_message('Choose your colors: '))
if debug:
print('Temp =', temp)
guess = list(temp)
if debug:
print('Guess =', guess)
if debug:
print('Secret = ', secret)
temp = ''
if '?' in guess and len(guess) == 1:
print(rules)
guess = ''
continue
elif 'q' in guess and len(guess) == 1:
print('Bye!')
game_continue = False
break
# Will obviously continue if you don't quit.
win = False
try:
if game_continue:
checkGuess(guess)
clue = compare(secret, guess, clue)
# Finds differences between guess and secret
win = calc(secret, guess, clue)
# Calculates if you won or not.
if win:
print('You won!')
else:
hint_text = ''.join(clue)
message = remarks.get_message()["remark"];
text = format_message(message + ": ")
print(text + hint_text);
print(' ');
continue
except LengthException as e:
print('Error:', e)
continue
except ColorException as e:
print('Error:', e)
continue
cont = input('Try again? y/n: ')
if cont == 'y': # Regenerates secret and clears variables.
secret = []
generateSecret(secret)
elif cont == 'n':
game_continue = False
print('Bye!')
break