Updates some control flow issues.

- Updates `calc` to return if the user won.
     - We can then look at the status (win) and continue the loop
     instead of falling through to the "Try again?..." section.
 - Adds continuation after a caught error to prevent the secret from
 resetting
 - Empties the `secret` before regenerating it.
     - `generateSecret() appends to the secret so the second game had a
     len(secret) of 8
     - This caused the compare function to out of bounds on the `guess`
     array.
 - Changed the output formatting of the hit to help readability.

 - Some ideas
     - setting a variable to a length of 4 and referencing that in
     generateSecret, checkGuess, and compare would allow changing the
     difficulty, example:
        - Easy: length 5
        - Medium: length 6
        - Hard: length 8
     - Ask the user for a difficulty before the start of the game.
This commit is contained in:
Pilot 2020-09-15 11:14:50 -04:00
parent a6292cd291
commit 30f54f438f
1 changed files with 20 additions and 9 deletions

29
main.py
View File

@ -41,6 +41,10 @@ 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.
"""
@ -61,7 +65,6 @@ def checkGuess(guess):
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.")
@ -85,11 +88,7 @@ def compare(secret, guess, clue):
def calc(secret, guess, clue):
tmp = ''
if tmp.join(clue) == '****':
print('You won!')
else:
print('Not quite right.')
print('Your clue:', clue)
return tmp.join(clue) == '****'
generateSecret(secret)
@ -98,7 +97,7 @@ print(rules)
while game_continue:
guess = []
temp = input('Choose your colors. Input \'?\' for help: ')
temp = input('Choose your colors: ')
if debug:
print('Temp =', temp)
guess = list(temp)
@ -119,24 +118,36 @@ while game_continue:
# 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
calc(secret, guess, clue)
win = calc(secret, guess, clue)
# Calculates if you won or not and outputs it.
if win:
print('You won!')
else:
print('Not quite right.')
clue_string = ''.join(clue)
print('Your clue: ', clue_string)
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)
guess = []
elif cont == 'n':
game_continue = False
print('Bye!')