From cd1b278c72f4afb797f6aa0addeb4ded7ffb3be1 Mon Sep 17 00:00:00 2001 From: laneia Date: Fri, 22 Feb 2019 13:50:11 -0800 Subject: [PATCH 1/3] committing waves 1, 2, & 3 (such small changes --- lib/adagrams.rb | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..5bcff7f 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,94 @@ +require "awesome_print" + +def draw_letters + avail_letters = %w[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] + avail_letters.sample(10) +end + +sample = draw_letters +# print sample + +def uses_available_letters?(input, letters_in_hand) + input = input.upcase + input.each_char.all? do |char| + if letters_in_hand.include?(char) + letters_in_hand.delete_at(letters_in_hand.index(char)) + true + else + false + end + end +end + +# user_input = gets.chomp.upcase +# p uses_available_letters?(user_input, sample) + +def score_word(word) + score = [] + score_chart = [%w[A E I O U L N R S T], %w[D G], %w[B C M P], %w[F H V W Y], %w[K]] + word = word.upcase + + score_chart.each_with_index do |letter_bank, letter_value| + word.each_char do |char| + if letter_bank.include?(char) + score << (letter_value + 1) + end + end + end + + word.each_char do |char| + if ["J", "X"].include?(char) + score << 8 + elsif ["Q", "Z"].include?(char) + score << 10 + end + end + + if word.length > 6 + score << 8 + end + + return score.sum +end + +# def highest_score_from(words) +# scored_words = [] + +# words.each do |played_word| +# played_word = played_word.upcase +# scored_words << +# { +# each_word: played_word, each_score: score_word(played_word), +# } +# end + +# high_score = scored_words.max_by { |point| point[:each_score] }[:each_score] +# highest_scored_words = scored_words.select { |semi_finalist| semi_finalist[:each_score] == high_score }.map { |semi_finalist| semi_finalist[:each_word] } + +# if highest_scored_words.length == 1 +# return ultimate_winner = {word: highest_scored_words[0], score: high_score} +# elsif highest_scored_words.length > 1 +# highest_scored_words.each do |ten_char_word| +# if ten_char_word.length == 10 +# ultimate_winner = {word: highest_scored_words.max_by { |highest| highest.length }, score: high_score} +# return ultimate_winner +# else +# ultimate_winner = {word: highest_scored_words.min_by { |highest| highest.length }, score: high_score} +# return ultimate_winner +# end +# end +# end +# end + +# input = ["BBBBBB", "AAAAAAAAAA"] +# p highest_score_from(input) + +# def is_in_english_dict?(input) +# require "csv" +# dictionary_array = [] +# CSV.foreach("assets/dictionary-english.csv") do |row| +# dictionary_array << row +# end +# dictionary_array.flatten! +# return dictionary_array.include?(input) ? true : false +# end From 6aeec8ca25a7edf972381df3843762659f90cc96 Mon Sep 17 00:00:00 2001 From: laneia Date: Fri, 22 Feb 2019 14:23:39 -0800 Subject: [PATCH 2/3] Added wave 4 --- lib/adagrams.rb | 52 ++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 5bcff7f..07c3ce2 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -51,37 +51,35 @@ def score_word(word) return score.sum end -# def highest_score_from(words) -# scored_words = [] +def highest_score_from(words) + scored_words = [] -# words.each do |played_word| -# played_word = played_word.upcase -# scored_words << -# { -# each_word: played_word, each_score: score_word(played_word), -# } -# end + words.each do |played_word| + played_word = played_word.upcase + scored_words << + { + each_word: played_word, each_score: score_word(played_word), + } + end -# high_score = scored_words.max_by { |point| point[:each_score] }[:each_score] -# highest_scored_words = scored_words.select { |semi_finalist| semi_finalist[:each_score] == high_score }.map { |semi_finalist| semi_finalist[:each_word] } + high_score = scored_words.max_by { |point| + point[:each_score] + }[:each_score] -# if highest_scored_words.length == 1 -# return ultimate_winner = {word: highest_scored_words[0], score: high_score} -# elsif highest_scored_words.length > 1 -# highest_scored_words.each do |ten_char_word| -# if ten_char_word.length == 10 -# ultimate_winner = {word: highest_scored_words.max_by { |highest| highest.length }, score: high_score} -# return ultimate_winner -# else -# ultimate_winner = {word: highest_scored_words.min_by { |highest| highest.length }, score: high_score} -# return ultimate_winner -# end -# end -# end -# end + highest_scored_words = scored_words.select { |semi_finalist| + semi_finalist[:each_score] == high_score + }.map { |semi_finalist| + semi_finalist[:each_word] + } -# input = ["BBBBBB", "AAAAAAAAAA"] -# p highest_score_from(input) + if highest_scored_words.length == 1 + return ultimate_winner = {word: highest_scored_words[0], score: high_score} + elsif highest_scored_words.length > 1 + winner = highest_scored_words.find { |ten| ten.length == 10 } + return {word: winner, score: high_score} if winner + end + return {word: highest_scored_words.min_by { |highest| highest.length }, score: high_score} +end # def is_in_english_dict?(input) # require "csv" From 2efbe21c3bb359d0236e614a2f66a72fb4a99d6f Mon Sep 17 00:00:00 2001 From: laneia Date: Fri, 22 Feb 2019 15:41:13 -0800 Subject: [PATCH 3/3] clone letters_in_hand in method uses_available_letters? to make wave 5 function --- lib/adagrams.rb | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 07c3ce2..d3c0e1c 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -9,6 +9,7 @@ def draw_letters # print sample def uses_available_letters?(input, letters_in_hand) + letters_in_hand = letters_in_hand.clone input = input.upcase input.each_char.all? do |char| if letters_in_hand.include?(char) @@ -20,9 +21,6 @@ def uses_available_letters?(input, letters_in_hand) end end -# user_input = gets.chomp.upcase -# p uses_available_letters?(user_input, sample) - def score_word(word) score = [] score_chart = [%w[A E I O U L N R S T], %w[D G], %w[B C M P], %w[F H V W Y], %w[K]] @@ -81,12 +79,12 @@ def highest_score_from(words) return {word: highest_scored_words.min_by { |highest| highest.length }, score: high_score} end -# def is_in_english_dict?(input) -# require "csv" -# dictionary_array = [] -# CSV.foreach("assets/dictionary-english.csv") do |row| -# dictionary_array << row -# end -# dictionary_array.flatten! -# return dictionary_array.include?(input) ? true : false -# end +def is_in_english_dict?(user_input_word) + require "csv" + dictionary_array = [] + CSV.foreach("assets/dictionary-english.csv") do |row| + dictionary_array << row + end + dictionary_array.flatten! + return dictionary_array.include?(user_input_word) ? true : false +end