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

Pine C16 Eunice & Lilly #30

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
90 changes: 86 additions & 4 deletions adagrams/game.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,93 @@
import random
def draw_letters():
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍
Nice work!

pass
#creating a dictionary of letter pool
letter_bank = {'A':9, 'B':2, 'C':2, 'D': 4, 'E': 12, 'F': 2, 'G':3, 'H': 2, 'I': 9, 'J': 1, 'K':1, 'L':4, 'M': 2, 'N':6, 'O': 8, 'P':2, 'Q':1, 'R': 6, 'S': 4, 'T':6, 'U':4, 'V': 2, 'W': 2, 'X': 1, 'Y': 2, 'Z':1}

#get all of all possible letters
tile_bag = []
for letter, freq in letter_bank.items():
for each_letter in range(freq):
tile_bag.append(letter)

#creates ten tiles from string from the list of letters
tile_list = []
for ten_letters in range(10):
letter = random.choice(tile_bag)
tile_list.append(letter)
tile_bag.remove(letter)

return tile_list


def uses_available_letters(word, letter_bank):
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 Nice work, this is a clever way to solve this.

pass
#verifies if word is in letterbank and if there are enough tiles
letter_list = [letter for letter in letter_bank if letter in word]
if len(letter_list) == len(word):
return True
return False


def score_word(word):
pass
#creates empty variable for total/makes input all upper case
total=0
modified_word=word.upper()
#Creates tile scores
value1=["A","E","I","O","U","L","N", "R", "S", "T"]
value_2=["D", "G"]
value_3=["B", "C", "M", "P"]
value_4=["F", "H", "V", "W", "Y"]
value_5=["K"]
value_8=["J", "X"]
value_10=["Q", "Z"]
Comment on lines +35 to +41
Copy link
Collaborator

Choose a reason for hiding this comment

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

This works, but It involves a lot of hardcoded if statements. This could also be solved with a dictionary, using either a set of letters as values and scores as values, or individual letters as keys and points as values.

For example

    letter_scores = {
        {"A","E","I","O","U","L","N", "R", "S", "T"}:  1,
        {"D", "G"}: 2,
        ...

Or:

    letter_scores = {
        'A': 1,
        'B': 3,
        'C': 3,
        ...

Then you can solve the problem with a more compact loop.


#Assigns score for each letter in word, and gets total updating it
for letter in modified_word:
if letter in value1:
total+=1
elif letter in value_2:
total+=2
elif letter in value_3:
total+=3
elif letter in value_4:
total+=4
elif letter in value_5:
total+=5
elif letter in value_8:
total+=8
elif letter in value_10:
total+=10
if len(modified_word)>=7:
total+=8
return(total)

def get_highest_word_score(word_list):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Clever use of max and min to determine the winner.

pass
#Get word dictionary with words and point value
word_dict = {}
for word in word_list:
word_dict[word] = score_word(word)

#gets the max value in dictionary
max_value = max(word_dict.values())
#get duplicates if present
max_values = [k for k, v in word_dict.items() if v == max_value]
#gets min word length
min_word_length = min(len(k) for k, v in word_dict.items())
#gets name of the max key
max_key = max(word_dict, key=word_dict.get)

if len(max_values) > 1:
#loopping over dictionary to find tie breaking winner
tie_breaker = []
for word , points in word_dict.items():
#if ever see word with 10 instant winner
if len(word) == 10:
tie_breaker = (word, points)
return tie_breaker
#if if there is no word with 10 it will print the word with the fewest letters
elif len(word) == min_word_length:
tie_breaker = (word, points)
return tie_breaker
# if no duplicates print single winner
else:
return max_key, word_dict[max_key]