From b27c26296bed9586339c8bd0f0dbbdab4e92804d Mon Sep 17 00:00:00 2001 From: Katie Date: Wed, 12 Feb 2020 14:50:36 -0800 Subject: [PATCH 1/5] modified adagrams.rb and created a pretty_game.rb ruby file to play the game --- .DS_Store | Bin 0 -> 8196 bytes lib/adagrams.rb | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ pretty_game.rb | 43 +++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 .DS_Store create mode 100644 pretty_game.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..97fc26715d6850e9e354cd7f63353ae871d255cb GIT binary patch literal 8196 zcmeHM&1(}u6o1pkx@(|B6s+K3p@JS_X-sPmUbZn61Vw3zJ^0b?X1B@G&2HG;G(rgk zLGa{R^y&xx72?Mq;a?$wcn}Zz-po*T^I`BJ7Uz|jH#_tD&A$0%vXi$309NTZC4e~q z(6Ep%$nE|55>ZJ{VZYo6`4%=Z_AocnW|%huz$;jd=j)7}A^4_l4(FoKs{EOj4Ol zF_@s^xIu9Aru2Q`1s#~61Cw`VGDBhf?#MGZ9GEG*s+9stfnfzGcb@?ZV*GOl@^|_w zUB+_$CpS*cVC`<)mbv~nyX|{%vbgw(O-<*H%pBG9V|hJ4o7eBvnqjw=SV z-Sh${>{h}~;KUp+C+F>3R?@_Iw0U#e`OJai;`*Teh^>quPF(kGjGhRY0l!hI)x%bs zQ|I4)9vp9l)v8odJNw|rlHLOvG{J*_cFQO{J2ilx1|RWuc2@Ri0Hwh?#;}Xg>>%0) zcL4u&0Iwr{4OXEFH_(rYoA&P0)ZWcu#4g61WM}5XqMwALj81t2%QhA=!u5sc6s$5o1n~Oc dABNaA5K0_V`o8ekgQi{ta2Zsk6!@zO`~qW0Fc<&; literal 0 HcmV?d00001 diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..cf87a6a 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,120 @@ +require 'csv' + +def draw_letters + letter_quantities = { + '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 + } + pool_of_letters = [] + letter_quantities.each do |letter, quantities| + quantities.times do + pool_of_letters.push(letter.to_s) + end + end + chosen_letters = [] + + random_indexes_array = (0...pool_of_letters.length).to_a.sample(10) + + random_indexes_array.each do |index| + chosen_letters.push(pool_of_letters[index]) + end + + # 10.times do + # rand_index = rand(pool_of_letters.length) + # chosen_letters.push(pool_of_letters[rand_index]) + # pool_of_letters.delete_at(rand_index) + # end + return chosen_letters +end + +def uses_available_letters?(input, chosen_letters) + chosen_letters_array_to_manipulate = chosen_letters.clone + input_split_letters = input.upcase.split("") + input_split_letters.each do |letter| + if chosen_letters_array_to_manipulate.include?(letter) + chosen_letters_array_to_manipulate.delete_at(chosen_letters_array_to_manipulate.index(letter)) + # puts "The letter testing is #{letter}" + # print chosen_letters_array_to_manipulate + # puts "\n" + else + return false + end + end + return true +end + +def score_word(word) + 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 + } + letters_in_word = word.upcase.split("") + total = 0 + letters_in_word.each do |letter| + letter_values.each do |letter_array, value| + if letter_array.include?(letter) + total += value + end + end + end + if word.length > 6 + total += 8 + end + return total +end + +def highest_score_from(words) + word_with_highest_score = "" + highest_score = 0 + words.each do |word| + score_of_word = score_word(word) + if score_of_word > highest_score + word_with_highest_score = word + highest_score = score_of_word + elsif score_of_word == highest_score + if word.length == 10 && word_with_highest_score.length != 10 + word_with_highest_score = word + highest_score = score_of_word + elsif word.length < word_with_highest_score.length && word_with_highest_score.length != 10 + word_with_highest_score = word + highest_score = score_of_word + end + end + end + + return {:word => word_with_highest_score, :score => highest_score} +end + +def is_in_english_dict?(input) + csv_data = CSV.parse(File.read("assets/dictionary-english.csv"), headers: true).by_col[0] + return csv_data.include?(input.downcase) +end diff --git a/pretty_game.rb b/pretty_game.rb new file mode 100644 index 0000000..bc10817 --- /dev/null +++ b/pretty_game.rb @@ -0,0 +1,43 @@ +require_relative 'lib/adagrams' + +puts "Welcome to Adagrams! How many hands would you like to play?" +num_rounds_to_play = gets.chomp + +until num_rounds_to_play == num_rounds_to_play.to_i.to_s + puts "Sorry that's not an integer. Please try again" + num_rounds_to_play = gets.chomp +end + +user_words = [] + +num_rounds_to_play.to_i.times do + letters_in_hand = draw_letters + puts "Here are 10 letters for you from a random draw:" + print letters_in_hand + puts "\n" + + puts "Please use the letters above and give me a word." + user_input = gets.chomp.upcase + + used_available_letters = uses_available_letters?(user_input, letters_in_hand) + dictionary_word = is_in_english_dict?(user_input) + + until used_available_letters == true && dictionary_word == true + puts "Sorry, this is not a valid word. Please try again." + user_input = gets.chomp.upcase + used_available_letters = uses_available_letters?(user_input, letters_in_hand) + dictionary_word = is_in_english_dict?(user_input) + end + + + user_words.push(user_input) + + + + puts "The score of #{user_input} is: #{score_word(user_input)}" + puts ".........................." +end + +puts "\n\n" + +puts "Congrats! The winning word was #{highest_score_from(user_words)[:word]} with a score of #{highest_score_from(user_words)[:score]}" \ No newline at end of file From a699b4426b7590cd5dabea0e5e934912a5ea39ba Mon Sep 17 00:00:00 2001 From: Cathy O Date: Wed, 12 Feb 2020 15:59:07 -0800 Subject: [PATCH 2/5] added and deleted comments for clarity --- lib/adagrams.rb | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index cf87a6a..a44ce90 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,5 +1,6 @@ require 'csv' +# method to generate 10 random letters def draw_letters letter_quantities = { 'A': 9, @@ -29,44 +30,45 @@ def draw_letters 'Y': 2, 'Z': 1 } - pool_of_letters = [] + pool_of_letters = [] + # loop over letter_quantities hash to add the right quantity of each letter to the pool letter_quantities.each do |letter, quantities| - quantities.times do + quantities.times do pool_of_letters.push(letter.to_s) end end - chosen_letters = [] + chosen_letters = [] + # create an array of 10 random numbers to be used as indexes in the pool array random_indexes_array = (0...pool_of_letters.length).to_a.sample(10) + # loop through the array of indexes and add the indexed element to chosen_letters random_indexes_array.each do |index| chosen_letters.push(pool_of_letters[index]) end - # 10.times do - # rand_index = rand(pool_of_letters.length) - # chosen_letters.push(pool_of_letters[rand_index]) - # pool_of_letters.delete_at(rand_index) - # end return chosen_letters end +# method to determine if the letters in the user input are in the array of chosen letters def uses_available_letters?(input, chosen_letters) - chosen_letters_array_to_manipulate = chosen_letters.clone + # create a clone of the chosen_letters array so the original does not get altered + chosen_letters_array_to_manipulate = chosen_letters.clone input_split_letters = input.upcase.split("") + + # loop through each letter of user input and check if it's in the chosen_letters array input_split_letters.each do |letter| if chosen_letters_array_to_manipulate.include?(letter) chosen_letters_array_to_manipulate.delete_at(chosen_letters_array_to_manipulate.index(letter)) - # puts "The letter testing is #{letter}" - # print chosen_letters_array_to_manipulate - # puts "\n" else return false end end + return true end +# method to calculate the score of a word def score_word(word) letter_values = { ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"] => 1, @@ -79,6 +81,8 @@ def score_word(word) } letters_in_word = word.upcase.split("") total = 0 + + # for each letter, loop through the values hash and add the value of each letter to the total letters_in_word.each do |letter| letter_values.each do |letter_array, value| if letter_array.include?(letter) @@ -86,15 +90,19 @@ def score_word(word) end end end + if word.length > 6 total += 8 end + return total end +# method to determine which word has the highest score def highest_score_from(words) word_with_highest_score = "" highest_score = 0 + words.each do |word| score_of_word = score_word(word) if score_of_word > highest_score @@ -114,6 +122,7 @@ def highest_score_from(words) return {:word => word_with_highest_score, :score => highest_score} end +# method to determine if word is in the english dictionary def is_in_english_dict?(input) csv_data = CSV.parse(File.read("assets/dictionary-english.csv"), headers: true).by_col[0] return csv_data.include?(input.downcase) From 2e335ab4b009f8497370bd841d88bf32c571e3e2 Mon Sep 17 00:00:00 2001 From: Katie Date: Thu, 13 Feb 2020 15:23:33 -0800 Subject: [PATCH 3/5] changed letter quantities into a constant to move outside of the method --- lib/adagrams.rb | 61 +++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index a44ce90..40968cf 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,38 +1,39 @@ require 'csv' +LETTER_QUANTITIES = { + '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 +} + # method to generate 10 random letters def draw_letters - letter_quantities = { - '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 - } pool_of_letters = [] - # loop over letter_quantities hash to add the right quantity of each letter to the pool - letter_quantities.each do |letter, quantities| + # loop over LETTER_QUANTITIES hash to add the right quantity of each letter to the pool + LETTER_QUANTITIES.each do |letter, quantities| quantities.times do pool_of_letters.push(letter.to_s) end From 789e1b349e64e6043d44016eba8bfb99a10630d7 Mon Sep 17 00:00:00 2001 From: Katie Date: Fri, 14 Feb 2020 14:20:08 -0800 Subject: [PATCH 4/5] changed variable name in uses_available_letters? method to match instructions --- lib/adagrams.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 40968cf..cfdc236 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -52,15 +52,15 @@ def draw_letters end # method to determine if the letters in the user input are in the array of chosen letters -def uses_available_letters?(input, chosen_letters) - # create a clone of the chosen_letters array so the original does not get altered - chosen_letters_array_to_manipulate = chosen_letters.clone +def uses_available_letters?(input, letters_in_hand) + # create a clone of the letters_in_hand array so the original does not get altered + letters_in_hand_array_to_manipulate = letters_in_hand.clone input_split_letters = input.upcase.split("") - # loop through each letter of user input and check if it's in the chosen_letters array + # loop through each letter of user input and check if it's in the letters_in_hand array input_split_letters.each do |letter| - if chosen_letters_array_to_manipulate.include?(letter) - chosen_letters_array_to_manipulate.delete_at(chosen_letters_array_to_manipulate.index(letter)) + if letters_in_hand_array_to_manipulate.include?(letter) + letters_in_hand_array_to_manipulate.delete_at(letters_in_hand_array_to_manipulate.index(letter)) else return false end From 1b8d1f81f974a74f82493b3dfec1c7be6da1b012 Mon Sep 17 00:00:00 2001 From: Cathy O Date: Fri, 14 Feb 2020 14:29:48 -0800 Subject: [PATCH 5/5] revised syntax in is_in_english_dict? method --- .DS_Store | Bin 8196 -> 8196 bytes lib/adagrams.rb | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.DS_Store b/.DS_Store index 97fc26715d6850e9e354cd7f63353ae871d255cb..52b2d304d2a149dae01d21d47a01ff780e1e3390 100644 GIT binary patch delta 245 zcmZp1XmOa}&nUJrU^hRb*k&GqPNvEELhLq4<;4X_Ir&Kp3=Aid3UV@wOAHLIGcqx= zu(GjpaBy*O@p8omXXKX$mn4>y7CR*tMT2+&i6t3HiIZmvc-C`paB^_Q3y4=&o0yvE zD41E+>L^s3n;YmTm>8SY)^c))s~XyRCgfIDRoB$k%>dd11dNOjnt>lm!>E}+1~Wqr zLncE~S#VKaPJUiGPz0o!kD-8}h#{4s1PCh`;u+Ez5*czCQYV`UOE58VZ0-^|z__ur UmuWM*#5b17XN7AwmUb}#0P)^CasU7T delta 38 ucmZp1XmOa}&nUbxU^hRb@Ma!?PNvOAg|{+p_Tt&gyqR6%8_UKDc4h$k)eOD> diff --git a/lib/adagrams.rb b/lib/adagrams.rb index cfdc236..0d90f86 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -125,6 +125,6 @@ def highest_score_from(words) # method to determine if word is in the english dictionary def is_in_english_dict?(input) - csv_data = CSV.parse(File.read("assets/dictionary-english.csv"), headers: true).by_col[0] + csv_data = CSV.read("assets/dictionary-english.csv", headers: true).by_col[0] return csv_data.include?(input.downcase) end