Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amethyst Megan G. #20

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 135 additions & 4 deletions adagrams/game.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,142 @@
import random
import copy

# Two Dimensional Array

LETTER_POOL = [
['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A'],
['B', 'B'],
['C', 'C'],
['D','D','D','D'],
['E','E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E'],
['F', 'F'],
['G', 'G', 'G'],
['H', 'H'],
['I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I'],
['J'],
['K'],
['L', 'L', 'L', 'L'],
['M', 'M'],
['N', 'N', 'N', 'N', 'N', 'N'],
['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],
['P', 'P'],
['Q'],
['R', 'R', 'R', 'R', 'R', 'R'],
['S', 'S', 'S', 'S'],
['T', 'T', 'T', 'T', 'T', 'T'],
['U', 'U', 'U', 'U'],
['V', 'V'],
['W', 'W'],
['X'],
['Y', 'Y'],
['Z']
]

# helper function to create one big list of strings

def get_one_list_of_letters():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yessss! I love how you are making use of helper functions here! It allows how us make more concise and readable code! Also great job on leaving a comment about what this function does.


total_letters_list = []
for elem in LETTER_POOL:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like how you created a nested data structure and then you used a nested loop to build a big list for you to pull a random letter from. You definitely are showing proficiency when it comes to this topic! ⭐️

for char in elem:
total_letters_list.append(char)
return total_letters_list

def draw_letters():
pass

total_letters_list = get_one_list_of_letters()

# start with an empty hand and initialize counter at 0
hand = []
i = 0
while i < 10:
# randomize letter choice
random_letter = total_letters_list[random.randint(0, 97)]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small comment, one thing that we want to try to avoid as developers is hard coding values inside for code base. So, instead using 97, we could maybe do len(total_letters_list) that way if anyone wants to change the weights of the letter then they won't have change the 97 as well! Again, just a small change to make your code more scalable.

# ensure that random letter choice in hand is never greater than the max amount in the letter pool
# if less than the max amount then append to empty list which is our hand of letters
if hand.count(random_letter) < total_letters_list.count(random_letter):
hand.append(random_letter)
else:
# use continue to end this iteration and immediately start next iteration
continue
i += 1
Comment on lines +57 to +62

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count is a great function to use in a situation like this! That way you don't have worry about manipulating any data and removing things! An alternative refactor we could do to make the code a bit more concise is this:

while len(hand) < 10:
     random_letter = total_letters_list[random.randint(0, 97)]

     if hand.count(random_letter) < total_letters_list.count(random_letter):
            hand.append(random_letter)

This code will run the same as yours, this code just doesn't keep track of a counter since the times we append to hand is equal to len(hand)


return hand

def uses_available_letters(word, letter_bank):
pass

# made copy of list as to not modify the original copy
letter_bank_copy = copy.deepcopy(letter_bank)

# iterate through each letter in the word and make letters all uppercase
for letter in word.upper():

if letter in letter_bank_copy:
# remove letter from letter bank so it can't be reused
letter_bank_copy.remove(letter)
else:
return False

return True
Comment on lines 66 to +80

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of the deepcopy function here, it lets you get a copy of the letter_bank without changing the object itself. This shows me that you have a good understanding of variables and memory. An alternative approach that doesn't mutate the original letter_bank is this here:

def uses_available_letters(word, letter_bank):
    cap_word = word.upper()
    for letter in cap_word:
       if letter_bank.count(letter) == 0: 
          return False

       if cap_word.count(letter) > letter_bank.count(letter):
          return False
    
    return True

It is basically the same thing as what you had in wave 1, using the count function to count the instances of a letter in both the word and letter_bank.


def score_word(word):
pass
# create dictionary to store key value pairs of letters and their corresponding value
letter_values = {
('A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'): 1,
('D', 'G'): 2,
('B', 'C', 'M', 'P'): 3,
('F', 'H', 'V', 'W', 'Y'): 4,
('K'): 5,
('J', 'X'): 8,
('Q', 'Z'): 10
}
# initialize score at zero
score = 0
# iterate through each letter of the word(uppercase)
for letter in word.upper():
# iterate through keys in dictionary to gain access to their values
for key in letter_values:
if letter in key:
Comment on lines +98 to +99

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, Megan! You used tuples as the keys for your dictionary and then used the if letter in key to add up the score! ⭐️

# add score as we iterate through
score += letter_values[key]
# if a word is between 7 and 10 letters, add 8 bonus points to the total
if len(word) > 6:
score += 8

return score

def get_highest_word_score(word_list):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great function! I know we talked about this logic in co-working and you implemented very well! 🤩

pass
# initialize a score keeper and empty list for the winning word(s)
highest_score = 0
winning_list = []
# iterate through each word in the word list
for word in word_list:
# pull in score value from score_word function
score = score_word(word)
# reassign highest_score if the current score is greater than it
if score > highest_score:
highest_score = score
# reassign winning_list when it's corresponding score is the highest
winning_list = [word]
# score is a tie to current high score, add word to winning_list
elif score == highest_score:
winning_list.append(word)

# initialize letter_count at 10 letters and winning_word as empty string
letter_count = 10
winning_word = ''
# if only one word in winning list, return the word and highest score
if len(winning_list) == 1:
return winning_list[0], highest_score
else:
for word in winning_list:
# if the word has 10 letters exactly, return word and highest score
if len(word) == 10:
return word, highest_score
else:
# if the word length is less than the letter count, we want to reassign letter count with word length and winning word with the actual winning word
if len(word) < letter_count:
letter_count = len(word)
winning_word = word

return winning_word, highest_score

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Megan, you completed your first official Ada project! Congrats! Your code was a pleasure to look through! Very easy to follow and quite concise! Please feel free to reach out if you need any clarifications on the comments I left! You did great! Can't wait to review more of your code! 😝✨