version 1.0

Totally functional in baseline. Just needs... more stuff.
This commit is contained in:
Wholesomedonut 2020-09-13 19:02:02 -06:00
parent 99ab4accd3
commit 134192c307
1 changed files with 143 additions and 0 deletions

143
main.py Normal file
View File

@ -0,0 +1,143 @@
# Pystermind 1.0
# By Wholesomedonut
# Read below for how-to-play, or input ? after running
import random
""" 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
"""
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:
print('i:', i)
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 = ''
if tmp.join(clue) == '****':
print('You won!')
else:
print('Not quite right.')
print('Your clue:', clue)
generateSecret(secret)
print(rules)
while game_continue:
guess = []
temp = input('Choose your colors. Input \'?\' for help: ')
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.
try:
if game_continue:
checkGuess(guess)
clue = compare(secret, guess, clue)
# Finds differences between guess and secret
calc(secret, guess, clue)
# Calculates if you won or not and outputs it.
except LengthException as e:
print('Error:', e)
except ColorException as e:
print('Error:', e)
cont = input('Try again? y/n: ')
if cont == 'y': # Regenerates secret and clears variables.
generateSecret(secret)
guess = []
elif cont == 'n':
game_continue = False
print('Bye!')
break