From dbc2cbd3767b6d440c5f0b5ca4c2b3c02722b0a1 Mon Sep 17 00:00:00 2001 From: Luke Renton Date: Thu, 1 Dec 2022 00:41:00 +0200 Subject: [PATCH] Added comments to code --- player.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/player.py b/player.py index c837bb2..206f868 100644 --- a/player.py +++ b/player.py @@ -1,5 +1,6 @@ import os +# runs the actuall running of the game def play_game(dictionary, previous_guesses, previous_feedback): guess = make_new_guess(dictionary, previous_guesses, previous_feedback) print("I'll make the guess: ", guess) @@ -8,6 +9,7 @@ def play_game(dictionary, previous_guesses, previous_feedback): previous_feedback.append(result) return result +# count all the letters in the guesses we have left to get their frequencies def count_letters(word): letters = {} seen = set() @@ -17,6 +19,7 @@ def count_letters(word): seen.add(word[i]) return (letters) +# find all the possible guesses we can make given the results of all the previous guesses def possible(test, dictionary, wrong): possible = [] for i in range(len(dictionary)): @@ -34,6 +37,8 @@ def possible(test, dictionary, wrong): possible.append(word) return (possible) +# find the best guess by calculating the value of each word (the more frequently occuring letters in a word the higher value it holds) +# uses count_letters and value_of_letters to get the value of each word def best_guess(words, values): word_value = {} for i in range(len(words)): @@ -41,26 +46,30 @@ def best_guess(words, values): for x in range(len(words[i])): if words[i][x] in values: value += values[words[i][x]] - else: value += 0.0000000000000000000000000000000000000000000001 + else: value += 0.0000000000000000000000000000000000000000000001 #just add some arbitraly large number word_value[words[i]] = value - word_value = {k:v for k,v in sorted(word_value.items(),key=lambda item: item[1], reverse=True)} + word_value = {k:v for k,v in sorted(word_value.items(),key=lambda item: item[1], reverse=True)} #find our most valuable word return(list(word_value.keys())[0]) +# using the frequencies lets find the value of the letters def value_of_letters(answers): wordscont = ''.join(answers) lcount = count_letters(wordscont) c = {k:v/len(wordscont) for k,v in sorted(lcount.items(),key=lambda item: item[1], reverse=True)} return(c) +# the actuall running of the program def make_new_guess(words, previous_guesses, previous_feedback): letters = value_of_letters(words) dictionary = words + # start the process if len(previous_guesses) == 0: wrong = set() - firstbest = 'RATED' + firstbest = 'RATED' #just the best guess, no need to calculate anything else wrong.add(firstbest) return firstbest else: + # uses the functions we made to solve for the word count = len(previous_guesses) - 1 wrong = set(previous_guesses) for i in range(len(previous_guesses)): @@ -89,13 +98,14 @@ def make_new_guess(words, previous_guesses, previous_feedback): previous_guesses = [] previous_feedback = [] +# get the possible guesses we can make location = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) lines = open(os.path.join(location,'words.txt'), 'r') dictionary = lines.read().upper().replace('"','').split(',') lines.close() result = play_game(dictionary, previous_guesses,previous_feedback) -while (result != result.upper()): +while (result != result.upper() or "." in result): result = play_game(dictionary, previous_guesses,previous_feedback) print("Congrats (You) won!")