From 30f54f438f99d5281f44e22bfcf88f5f7c741b6d Mon Sep 17 00:00:00 2001 From: Pilot Date: Tue, 15 Sep 2020 11:14:50 -0400 Subject: [PATCH 1/3] 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. --- main.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index cece5ce..bcd0484 100644 --- a/main.py +++ b/main.py @@ -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!') -- 2.34.1 From 80e36e4aa58dc9f67a1fa23353a045ffb5ab8bb3 Mon Sep 17 00:00:00 2001 From: Pilot Date: Tue, 15 Sep 2020 14:39:17 -0400 Subject: [PATCH 2/3] Silly responses for failed attempts - This is over engineered, I was just trying out an idea - MessageTable implements a Random Distribution System - Changed the alignment of the output messages to lineup. --- .gitignore | 1 + witty_retort.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 .gitignore create mode 100644 witty_retort.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/witty_retort.py b/witty_retort.py new file mode 100644 index 0000000..b1c7645 --- /dev/null +++ b/witty_retort.py @@ -0,0 +1,41 @@ +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 + + + + + +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) -- 2.34.1 From 894dd6df8412dd7d6e262274629882aaaefbb500 Mon Sep 17 00:00:00 2001 From: Pilot Date: Tue, 15 Sep 2020 14:44:38 -0400 Subject: [PATCH 3/3] Changed the alignment of the output messages. --- main.py | 24 +++++++++++++++++++----- witty_retort.py | 12 ------------ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index bcd0484..d2465ac 100644 --- a/main.py +++ b/main.py @@ -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): @@ -47,6 +57,8 @@ and what you got wrong. """ +def format_message(message): + return message.rjust(25) def generateSecret(secret): for i in range(0, len(colors)): @@ -97,7 +109,7 @@ print(rules) while game_continue: guess = [] - temp = input('Choose your colors: ') + temp = input(format_message('Choose your colors: ')) if debug: print('Temp =', temp) guess = list(temp) @@ -126,14 +138,16 @@ while game_continue: clue = compare(secret, guess, clue) # Finds differences between guess and secret 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: - print('Not quite right.') - clue_string = ''.join(clue) - print('Your clue: ', clue_string) + hint_text = ''.join(clue) + message = remarks.get_message()["remark"]; + text = format_message(message + ": ") + print(text + hint_text); + print(' '); continue except LengthException as e: diff --git a/witty_retort.py b/witty_retort.py index b1c7645..eb4ecec 100644 --- a/witty_retort.py +++ b/witty_retort.py @@ -27,15 +27,3 @@ class MessageTable: if hit_value < running_value: return i - - - - -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) -- 2.34.1