Merge pull request 'Fixes and Changes' (#1) from timemachine/pystermind:master into master
Reviewed-on: #1
This commit is contained in:
commit
cf07672ad7
|
@ -0,0 +1 @@
|
||||||
|
__pycache__/
|
45
main.py
45
main.py
|
@ -4,6 +4,16 @@
|
||||||
# Read below for how-to-play, or input ? after running
|
# Read below for how-to-play, or input ? after running
|
||||||
|
|
||||||
import random
|
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"""
|
""" Dummy class for Errors"""
|
||||||
class Error(Exception):
|
class Error(Exception):
|
||||||
|
@ -41,8 +51,14 @@ and what you got wrong.
|
||||||
* = correct color in correct location
|
* = correct color in correct location
|
||||||
~ = correct color and wrong location
|
~ = correct color and wrong location
|
||||||
# = incorrect color in incorrect location
|
# = incorrect color in incorrect location
|
||||||
|
|
||||||
|
'?' for help.
|
||||||
|
'q' to quit.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def format_message(message):
|
||||||
|
return message.rjust(25)
|
||||||
|
|
||||||
def generateSecret(secret):
|
def generateSecret(secret):
|
||||||
for i in range(0, len(colors)):
|
for i in range(0, len(colors)):
|
||||||
|
@ -61,7 +77,6 @@ def checkGuess(guess):
|
||||||
raise LengthException("Your guess is too short.")
|
raise LengthException("Your guess is too short.")
|
||||||
|
|
||||||
for i in guess:
|
for i in guess:
|
||||||
print('i:', i)
|
|
||||||
if i not in colors:
|
if i not in colors:
|
||||||
raise ColorException("You are not guessing from r, g, b, or y.")
|
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):
|
def calc(secret, guess, clue):
|
||||||
tmp = ''
|
tmp = ''
|
||||||
if tmp.join(clue) == '****':
|
return tmp.join(clue) == '****'
|
||||||
print('You won!')
|
|
||||||
else:
|
|
||||||
print('Not quite right.')
|
|
||||||
print('Your clue:', clue)
|
|
||||||
|
|
||||||
|
|
||||||
generateSecret(secret)
|
generateSecret(secret)
|
||||||
|
@ -98,7 +109,7 @@ print(rules)
|
||||||
while game_continue:
|
while game_continue:
|
||||||
guess = []
|
guess = []
|
||||||
|
|
||||||
temp = input('Choose your colors. Input \'?\' for help: ')
|
temp = input(format_message('Choose your colors: '))
|
||||||
if debug:
|
if debug:
|
||||||
print('Temp =', temp)
|
print('Temp =', temp)
|
||||||
guess = list(temp)
|
guess = list(temp)
|
||||||
|
@ -119,24 +130,38 @@ while game_continue:
|
||||||
|
|
||||||
# Will obviously continue if you don't quit.
|
# Will obviously continue if you don't quit.
|
||||||
|
|
||||||
|
win = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if game_continue:
|
if game_continue:
|
||||||
checkGuess(guess)
|
checkGuess(guess)
|
||||||
clue = compare(secret, guess, clue)
|
clue = compare(secret, guess, clue)
|
||||||
# Finds differences between guess and secret
|
# Finds differences between guess and secret
|
||||||
calc(secret, guess, clue)
|
win = calc(secret, guess, clue)
|
||||||
# Calculates if you won or not and outputs it.
|
# 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:
|
except LengthException as e:
|
||||||
print('Error:', e)
|
print('Error:', e)
|
||||||
|
continue
|
||||||
|
|
||||||
except ColorException as e:
|
except ColorException as e:
|
||||||
print('Error:', e)
|
print('Error:', e)
|
||||||
|
continue
|
||||||
|
|
||||||
cont = input('Try again? y/n: ')
|
cont = input('Try again? y/n: ')
|
||||||
if cont == 'y': # Regenerates secret and clears variables.
|
if cont == 'y': # Regenerates secret and clears variables.
|
||||||
|
secret = []
|
||||||
generateSecret(secret)
|
generateSecret(secret)
|
||||||
guess = []
|
|
||||||
elif cont == 'n':
|
elif cont == 'n':
|
||||||
game_continue = False
|
game_continue = False
|
||||||
print('Bye!')
|
print('Bye!')
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue