diff --git a/adagrams/game.py b/adagrams/game.py index 5fb37b11..5973f707 100644 --- a/adagrams/game.py +++ b/adagrams/game.py @@ -1,11 +1,129 @@ +import random +import english_dictionary +from english_dictionary.scripts.read_pickle import get_dict +# This dictionary package is not a comprehensive list of all English words. +# The dictionary does not recognize suffixes, e.g. "voted" is not recognized, but "vote" is. +english_dict = get_dict() + +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"] + +SCORE_CHART = { + "A": 1, + "E": 1, + "I": 1, + "O": 1, + "U": 1, + "L": 1, + "N": 1, + "R": 1, + "S": 1, + "T": 1, + "D": 2, + "G": 2, + "B": 3, + "C": 3, + "M": 3, + "P": 3, + "F": 4, + "H": 4, + "V": 4, + "W": 4, + "Y": 4, + "K": 5, + "J": 8, + "X": 8, + "Q": 10, + "Z": 10 +} + def draw_letters(): - pass + is_freq_correct = False + #Loop to check if letter frequency is correct in relation to LETTER_POOL + while is_freq_correct == False: + letter_bank = random.choices(LETTER_POOL, k=10) + # random.choices() documentation at https://docs.python.org/3/library/random.html + is_freq_correct = True + #Begin with assuming frequency is correct + #print(letter_bank) + for letter in letter_bank: + if letter_bank.count(letter) > LETTER_POOL.count(letter): + is_freq_correct = False + #Letter frequency is incorrect. Breaks the loop and creates new hand of letter. + break + #print(f"After freq check {letter_bank}") + return letter_bank + +#for testing: draw_letters() def uses_available_letters(word, letter_bank): - pass + word = word.upper() + #Return True if every letter in input word is available (in the right quantities) in letter_bank + for letter in word: + if letter in letter_bank and word.count(letter) <= letter_bank.count(letter): + continue + else: + #Will return False if input word uses letters not in letter_bank, + # and/or over the quantity of the letter available in letter_bank. + return False + return True def score_word(word): - pass + word = word.upper() + score = 0 + for letter in word: + score += SCORE_CHART[letter] + if len(word) >= 7: + score += 8 + return score def get_highest_word_score(word_list): - pass \ No newline at end of file + + highest_score = 0 + highest_words = [] + for word in word_list: + score = score_word(word) + if highest_score < score: + highest_score = score + highest_words = [word] + elif highest_score == score: + highest_words.append(word) + + if len(highest_words) == 1: # This means that there is no tie + return(highest_words[0], highest_score) + else: # This means there is a tie, applies tie-breaking logic + for word in highest_words: + if len(word) == 10: + return(word, highest_score) + return(min(highest_words, key=len), highest_score) + +def word_in_english_dictionary(word): + word = word.lower() + if word in english_dict.keys(): + return True + else: + return False diff --git a/adagrams/ui_helper.py b/adagrams/ui_helper.py index 950ed939..ea31b10e 100644 --- a/adagrams/ui_helper.py +++ b/adagrams/ui_helper.py @@ -12,6 +12,10 @@ def display_needs_valid_input_message(): print("You entered in a word that contains characters not in the letter bank") display_game_instructions() +def display_needs_valid_input_message_2(): + print("You entered in a word that contains characters not in the letter bank or is not in the English dictionary") + display_game_instructions() + def display_score(score): print(f"Your submitted anagram scored {score} points") diff --git a/main.py b/main.py index d583cfd9..46421b42 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ import sys from adagrams.ui_helper import * -from adagrams.game import draw_letters, uses_available_letters, score_word, get_highest_word_score +from adagrams.game import draw_letters, uses_available_letters, score_word, get_highest_word_score, word_in_english_dictionary def wave_1_run_game(): display_welcome_message() @@ -14,7 +14,7 @@ def wave_1_run_game(): display_retry_instructions() continue_input = input() game_continue = continue_input == "y" - + display_goodbye_message() def wave_2_run_game(): @@ -84,6 +84,31 @@ def wave_4_run_game(): display_highest_score(get_highest_word_score(played_words)) display_goodbye_message() +def wave_5_run_game(): + display_welcome_message() + game_continue = True + played_words = [] + while game_continue: + print("Let's draw 10 letters from the letter pool...") + letter_bank = draw_letters() + display_drawn_letters(letter_bank) + display_game_instructions() + user_input_word = input() + + while not (uses_available_letters(user_input_word, letter_bank) and word_in_english_dictionary(user_input_word)): + display_needs_valid_input_message_2() + user_input_word = input() + + score = score_word(user_input_word) + display_score(score) + played_words.append(user_input_word) + + display_retry_instructions() + continue_input = input() + game_continue = continue_input == "y" + display_highest_score(get_highest_word_score(played_words)) + display_goodbye_message() + def main(wave): if(wave == 1): wave_1_run_game() @@ -93,8 +118,10 @@ def main(wave): wave_3_run_game() elif(wave == 4): wave_4_run_game() + elif(wave == 5): + wave_5_run_game() else: - print("Please input a wave number. Valid wave numbers are 1, 2, 3, 4.") + print("Please input a wave number. Valid wave numbers are 1, 2, 3, 4, 5.") if __name__ == "__main__": args = sys.argv diff --git a/tests/test_wave_02.py b/tests/test_wave_02.py index 803450f2..db3d4694 100644 --- a/tests/test_wave_02.py +++ b/tests/test_wave_02.py @@ -47,4 +47,15 @@ def test_uses_available_letters_does_not_change_letter_bank(): # Assert assert is_valid == True assert letters == letters_copy - \ No newline at end of file + +# Student made test +def test_uses_available_letters_not_case_sensitive(): + # Arrange + letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"] + word = "acd" + + # Act + is_valid = uses_available_letters(word, letters) + + # Assert + assert is_valid == True \ No newline at end of file diff --git a/tests/test_wave_04.py b/tests/test_wave_04.py index 8fdedf66..7bece525 100644 --- a/tests/test_wave_04.py +++ b/tests/test_wave_04.py @@ -84,4 +84,15 @@ def test_get_highest_word_tie_same_length_prefers_first(): assert score_word(words[0]) == 18 assert score_word(words[1]) == 18 assert best_word[0] == words[0] - assert best_word[1] == 18 \ No newline at end of file + assert best_word[1] == 18 + +# Student made test, not used +# def test_get_highest_word_input_empty_list(): +# # Arrange +# words = [] + +# # Act +# best_word = get_highest_word_score(words) + +# # Assert +# assert best_word == (None, 0) \ No newline at end of file diff --git a/tests/test_wave_05.py b/tests/test_wave_05.py new file mode 100644 index 00000000..541f26d9 --- /dev/null +++ b/tests/test_wave_05.py @@ -0,0 +1,26 @@ +import pytest + +from adagrams.game import word_in_english_dictionary + + +def test_word_in_english_dictionary_valid_word_returns_true(): + # Arrange + word = "banana" + + # ACT + is_valid = word_in_english_dictionary(word) + + # Assert + assert is_valid == True + + + +def test_word_in_english_dictionary_invalid_word_returns_false(): + # Arrange + word = "xlkdfa;elkfjl" + + # Act + is_valid = word_in_english_dictionary(word) + + # Assert + assert is_valid == False \ No newline at end of file