From 8a9c443e810151e8623de16ef6110df8806575ca Mon Sep 17 00:00:00 2001 From: Faezeh Date: Tue, 11 Feb 2020 20:15:22 -0800 Subject: [PATCH 1/4] add wave-2 and adagrams --- lib/adagrams.rb | 33 +++++++++++++++++++++++++++++++++ wave-2-game.rb | 16 ++++++++-------- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..3a15a48 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,33 @@ +def draw_letters + # Letters A B C D E F G H I J K L M N O P Q R S T U V W X Y Z + letter_dist = [9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1] + hand_size = 10 + letter_a_offset = 65# 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 + + letter_pool = letter_dist.map.with_index { |dist, index| (index + letter_a_offset).chr * dist } + return letter_pool.join('').split('').sample(hand_size) + # ("A".."Z").each do |letter| + # dist = letter_dist[letter.ord - letter_a_offset] + # letter_pool += letter * dist + # end + # letter_pool_array = letter_pool.split"" + + # letter_pool_array = ["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"] + + # return letter_pool_array.sample(10) +end + +def uses_available_letters?(input, letter_in_hand) + hand_copy = letter_in_hand.clone + input.upcase.split("").each do |letter| + if hand_copy.include?(letter) + hand_copy.delete_at(hand_copy.index(letter)) + else + return false + end + end + return true + +end + + diff --git a/wave-2-game.rb b/wave-2-game.rb index 18745b7..d87d32e 100644 --- a/wave-2-game.rb +++ b/wave-2-game.rb @@ -37,28 +37,28 @@ def get_user_input def run_game display_welcome_message - + should_continue = true - + while should_continue puts "Let's draw 10 letters from the letter pool..." - + letter_bank = draw_letters display_drawn_letters(letter_bank) - + display_game_instructions - + user_input_word = get_user_input - + while ( !(uses_available_letters?(user_input_word, letter_bank)) ) display_needs_valid_input_message user_input_word = get_user_input end - + display_retry_instructions should_continue = get_user_input == "y" end - + display_goodbye_message end From 723ead8b2d87b5d176430463bd2b22a2dba30455 Mon Sep 17 00:00:00 2001 From: Faezeh A Date: Wed, 12 Feb 2020 09:47:48 -0800 Subject: [PATCH 2/4] Word scores can get calculated --- lib/adagrams.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 3a15a48..c9e899a 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -30,4 +30,31 @@ def uses_available_letters?(input, letter_in_hand) end +def score_word(word) + letter_values = word.upcase.split("").map do |letter| + case letter + when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" + 1 + when "D", "G" + 2 + when "B", "C", "M", "P" + 3 + when "F", "H", "V", "W", "Y" + 4 + when "K" + 5 + when "J", "X" + 8 + when "Q", "Z" + 10 + end + end + + if word.length >= 7 && word.length <= 10 + letter_values << 8 + end + + return letter_values.sum +end + From 4925b7a8a3e7b7f3c9d9d30809f72af5c4685acd Mon Sep 17 00:00:00 2001 From: faezeh-ashtiani Date: Wed, 12 Feb 2020 17:12:19 -0800 Subject: [PATCH 3/4] completed four waves and passes rake test --- lib/adagrams.rb | 80 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index c9e899a..453adda 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,22 +1,15 @@ +# A method to build a hand of 10 letters for the user. def draw_letters # Letters A B C D E F G H I J K L M N O P Q R S T U V W X Y Z letter_dist = [9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1] - hand_size = 10 - letter_a_offset = 65# 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 + hand_size = 10 + letter_a_offset = 65 letter_pool = letter_dist.map.with_index { |dist, index| (index + letter_a_offset).chr * dist } return letter_pool.join('').split('').sample(hand_size) - # ("A".."Z").each do |letter| - # dist = letter_dist[letter.ord - letter_a_offset] - # letter_pool += letter * dist - # end - # letter_pool_array = letter_pool.split"" - - # letter_pool_array = ["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"] - - # return letter_pool_array.sample(10) end +# A method to check if the word is an anagram of some or all of the given letters in the hand. def uses_available_letters?(input, letter_in_hand) hand_copy = letter_in_hand.clone input.upcase.split("").each do |letter| @@ -27,26 +20,26 @@ def uses_available_letters?(input, letter_in_hand) end end return true - end +# A method that returns the score of a given word as defined by the Adagrams game. def score_word(word) letter_values = word.upcase.split("").map do |letter| case letter - when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" - 1 - when "D", "G" - 2 - when "B", "C", "M", "P" - 3 - when "F", "H", "V", "W", "Y" - 4 - when "K" - 5 - when "J", "X" - 8 - when "Q", "Z" - 10 + when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" + 1 + when "D", "G" + 2 + when "B", "C", "M", "P" + 3 + when "F", "H", "V", "W", "Y" + 4 + when "K" + 5 + when "J", "X" + 8 + when "Q", "Z" + 10 end end @@ -57,4 +50,39 @@ def score_word(word) return letter_values.sum end +# A method looks at the array of words and calculates which of these words has the highest score. +def highest_score_from(words) + maximum_score = words.map { |word| score_word(word) }.max + highest = words.select { |word| score_word(word) == maximum_score } + if highest.length == 1 + winning_word = highest.first + else + highest_lengths = highest.map {|i| i.length} + if highest_lengths.any? { |x| x == 10} + index_of_length_10 = highest_lengths.find_index(10) + winning_word = highest[index_of_length_10] + else + winning_word = highest[highest_lengths.find_index(highest_lengths.min)] + end + end + + results = Hash.new + results[:score] = maximum_score + results[:word] = winning_word + + return results +end + +# def is_in_english_dict?(input) + + # if + # true + # else + # false + # end + + # returns + +# end + From 4253104a89df812810b945e15c18486ae4f49036 Mon Sep 17 00:00:00 2001 From: faezeh-ashtiani Date: Thu, 13 Feb 2020 15:42:13 -0800 Subject: [PATCH 4/4] All 5 waves completed --- lib/adagrams.rb | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 453adda..946f7d7 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,8 +1,11 @@ +require 'csv' + # A method to build a hand of 10 letters for the user. def draw_letters # Letters A B C D E F G H I J K L M N O P Q R S T U V W X Y Z letter_dist = [9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1] hand_size = 10 + # ASCII table number for a letter_a_offset = 65 letter_pool = letter_dist.map.with_index { |dist, index| (index + letter_a_offset).chr * dist } @@ -73,16 +76,10 @@ def highest_score_from(words) return results end -# def is_in_english_dict?(input) - - # if - # true - # else - # false - # end - - # returns - -# end +def is_in_english_dict?(input) + dict_path = File.join(File.dirname(__FILE__), "../assets/dictionary-english.csv") + dictionary = CSV.read(dict_path, headers: true).map { |row| row[0] } + return dictionary.include?(input) +end