Skip to content

Commit

Permalink
Added comments to code
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeRenton committed Nov 30, 2022
1 parent 355c76e commit dbc2cbd
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions player.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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()
Expand All @@ -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)):
Expand All @@ -34,33 +37,39 @@ 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)):
value = 0
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)):
Expand Down Expand Up @@ -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!")
Expand Down

0 comments on commit dbc2cbd

Please sign in to comment.