Merge pull request 'Fixes and Changes' (#1) from timemachine/pystermind:master into master

Reviewed-on: #1
This commit is contained in:
wholesomedonut 2020-09-15 19:30:22 +00:00
commit cf07672ad7
3 changed files with 65 additions and 10 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__/

45
main.py
View File

@ -4,6 +4,16 @@
# 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):
@ -41,8 +51,14 @@ 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)):
@ -61,7 +77,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 +100,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 +109,7 @@ print(rules)
while game_continue:
guess = []
temp = input('Choose your colors. Input \'?\' for help: ')
temp = input(format_message('Choose your colors: '))
if debug:
print('Temp =', temp)
guess = list(temp)
@ -119,24 +130,38 @@ 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)
# Calculates if you won or not and outputs it.
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)
guess = []
elif cont == 'n':
game_continue = False
print('Bye!')

29
witty_retort.py Normal file
View File

@ -0,0 +1,29 @@
import random
import functools
class MessageTable:
contents = []
def add_entry(self, string, probability):
entry = {
"remark": string,
"probability": probability,
"table": self
}
self.contents.append(entry)
def get_message(self):
probability_sum = functools.reduce(
lambda total, entry:
entry["probability"] + total,
self.contents, 0
);
hit_value = random.randrange(probability_sum)
running_value = 0
for i in self.contents:
running_value += i["probability"]
if hit_value < running_value:
return i