From 6780a00b942f7a5cd2a748ce7e412a0660043dbd Mon Sep 17 00:00:00 2001 From: Aurea Date: Mon, 13 Aug 2018 16:04:03 -0700 Subject: [PATCH 01/16] create data structure for letters and draw_letters method --- lib/adagrams.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..64234d2 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,12 @@ + + +def draw_letters + + pool = ('A'*9 + 'B'*2 + 'C'*2 + 'D'*4 + 'E'*12 + 'F'*2 + 'G'*3 + 'H'*2 + 'I'*9 + 'J' + 'K' + 'L'*4 + 'M'*2 + 'N'*6 + 'O'*8 + 'P'*2 + 'Q' + 'R'*6 + 'S'*4 + 'T'*6 + 'U'*4 + 'V'*2 + 'W'*2 + 'X' + 'Y'*2 + 'Z').chars + + pool.shuffle! + + return pool.pop(10) +end + +puts "#{draw_letters}" From 0d56b7318d496add6ea8d3ccc26e954c0e175ac6 Mon Sep 17 00:00:00 2001 From: Aurea Date: Mon, 13 Aug 2018 16:37:55 -0700 Subject: [PATCH 02/16] Checked if user input was valid --- lib/adagrams.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 64234d2..76200d2 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -9,4 +9,21 @@ def draw_letters return pool.pop(10) end -puts "#{draw_letters}" + +def uses_available_letters?(input, letters_in_hand) + input.upcase.chars.each do |char| + if letters_in_hand.include? char + check_index = letters_in_hand.index(char) + letters_in_hand.delete_at(check_index) + + else + return false + end + end + return true +end + +input = "tza" +letters_in_hand = ["A", "M", "C", "C", "T", "Z"] + +puts uses_available_letters?(input, letters_in_hand) From 1d435c3b83b46a951a1913796667093983bea370 Mon Sep 17 00:00:00 2001 From: Aurea Date: Tue, 14 Aug 2018 14:30:45 -0700 Subject: [PATCH 03/16] Add scoring --- lib/adagrams.rb | 52 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 76200d2..6b8d0a0 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -13,6 +13,8 @@ def draw_letters def uses_available_letters?(input, letters_in_hand) input.upcase.chars.each do |char| if letters_in_hand.include? char + + check_index = letters_in_hand.index(char) letters_in_hand.delete_at(check_index) @@ -20,10 +22,56 @@ def uses_available_letters?(input, letters_in_hand) return false end end + return true end -input = "tza" -letters_in_hand = ["A", "M", "C", "C", "T", "Z"] +def score_word(word) + score = 0 + + score_chart = { + "A" => 1, + "B" => 3, + "C" => 3, + "D" => 2, + "E" => 1, + "F" => 4, + "G" => 2, + "H" => 4, + "I" => 1, + "J" => 8, + "K" => 5, + "L" => 1, + "M" => 3, + "N" => 1, + "O" => 1, + "P" => 3, + "Q" => 10, + "R" => 1, + "S" => 1, + "T" => 1, + "U" => 1, + "V" => 4, + "W" => 4, + "X" => 8, + "Y" => 4, + "Z" => 10 + } + + word.upcase.chars.each do |char| + score += score_chart[char] + end + if word.length >= 7 + score += 8 + end + + return score +end + +input = "cat" +letters_in_hand = ["A", "M", "C", "C", "T", "Z"] puts uses_available_letters?(input, letters_in_hand) + + +puts score_word("parameter") From a8f490f6536a25bbf30765441e97a2f20cd952de Mon Sep 17 00:00:00 2001 From: Aurea Date: Tue, 14 Aug 2018 15:03:37 -0700 Subject: [PATCH 04/16] complete all basic 4 waves --- lib/adagrams.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 6b8d0a0..8dd871b 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -69,9 +69,47 @@ def score_word(word) return score end + +def highest_score_from(words) + + highest_score = 0 + best_word = '' + words.each do |word| + + if score_word(word) > highest_score + + highest_score = score_word(word) + # best_word << word + best_word = word + + elsif score_word(word) == highest_score + + if word.length != best_word.length + if word.length == 10 + highest_score = score_word(word) + best_word = word + + end + if word.length < best_word.length && best_word.length != 10 + highest_score = score_word(word) + best_word = word + + end + end + end + end + + return {:word => best_word, + :score => highest_score} +end + input = "cat" letters_in_hand = ["A", "M", "C", "C", "T", "Z"] puts uses_available_letters?(input, letters_in_hand) puts score_word("parameter") + +sample_word = ["CAT", "QQQQQQQQQQ", "ZZZZZZZZZZ", "ACT"] + +puts highest_score_from(sample_word) From 60101309619826691e8fbbc8d27aa3d08766732a Mon Sep 17 00:00:00 2001 From: Aurea Date: Tue, 14 Aug 2018 15:31:20 -0700 Subject: [PATCH 05/16] check if user input word is found within modified English dictionary csv file --- lib/adagrams.rb | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 8dd871b..453e8d2 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,9 +1,13 @@ +# adds csv gem +require "csv" def draw_letters + # letter pool pool = ('A'*9 + 'B'*2 + 'C'*2 + 'D'*4 + 'E'*12 + 'F'*2 + 'G'*3 + 'H'*2 + 'I'*9 + 'J' + 'K' + 'L'*4 + 'M'*2 + 'N'*6 + 'O'*8 + 'P'*2 + 'Q' + 'R'*6 + 'S'*4 + 'T'*6 + 'U'*4 + 'V'*2 + 'W'*2 + 'X' + 'Y'*2 + 'Z').chars + # randomize letters pool.shuffle! return pool.pop(10) @@ -11,24 +15,29 @@ def draw_letters def uses_available_letters?(input, letters_in_hand) + # check through each character; is it available? input.upcase.chars.each do |char| if letters_in_hand.include? char - + # delete letter once user inputs it check_index = letters_in_hand.index(char) letters_in_hand.delete_at(check_index) else + # letter is not avaialable return false end end + # letter is avaialble return true end def score_word(word) + # initialize scoring value score = 0 + # create scoring chart score_chart = { "A" => 1, "B" => 3, @@ -58,10 +67,12 @@ def score_word(word) "Z" => 10 } + # add scores together word.upcase.chars.each do |char| score += score_chart[char] end + # add eight extra points for words 7+ in length if word.length >= 7 score += 8 end @@ -72,24 +83,34 @@ def score_word(word) def highest_score_from(words) + # initializing values highest_score = 0 best_word = '' + + # loop through each word in array of user input words.each do |word| + if score_word(word) > highest_score + # if we have found the highest scoring word, set variable equal to that score and that word highest_score = score_word(word) - # best_word << word best_word = word + # check for score ties elsif score_word(word) == highest_score + # check that the lengths are unequal, since that matters for winning if word.length != best_word.length + # check if highest scoring word length is 10; if so, that's the winning word if word.length == 10 highest_score = score_word(word) best_word = word end + + # check that word length is less than best word's length AND that it does not equal 10 + # if there is a tie between two high scoring words (and the current best word is not a length 10; otherwise, it wins), then choose the shortest length if word.length < best_word.length && best_word.length != 10 highest_score = score_word(word) best_word = word @@ -103,13 +124,19 @@ def highest_score_from(words) :score => highest_score} end -input = "cat" -letters_in_hand = ["A", "M", "C", "C", "T", "Z"] -puts uses_available_letters?(input, letters_in_hand) +def is_in_english_dict?(input) +# iterate through each row in csv file +# each row is an array, so check the 0th index + CSV.foreach("../assets/dictionary-english.csv") do |row| + # check if user input word is found within dictionary + if input.upcase == row[0].upcase + return true + end + + end -puts score_word("parameter") + return false -sample_word = ["CAT", "QQQQQQQQQQ", "ZZZZZZZZZZ", "ACT"] -puts highest_score_from(sample_word) +end From 66fb3e459bffe66d492094b31d5fd94b5d2da66f Mon Sep 17 00:00:00 2001 From: Aurea Date: Tue, 14 Aug 2018 16:52:40 -0700 Subject: [PATCH 06/16] fix pointer issue causing letters to be removed --- lib/adagrams.rb | 21 ++++++++++++--------- specs/adagrams_spec.rb | 31 +++++++++++++++++++++++++++++++ wave-5-game.rb | 4 ++++ 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 453e8d2..cbd707f 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -14,22 +14,21 @@ def draw_letters end -def uses_available_letters?(input, letters_in_hand) +def uses_available_letters?(input, letters) # check through each character; is it available? + letters_altered = letters.clone + input.upcase.chars.each do |char| - if letters_in_hand.include? char + if letters_altered.include? char # delete letter once user inputs it - check_index = letters_in_hand.index(char) - letters_in_hand.delete_at(check_index) + check_index = letters_altered.index(char) + letters_altered.delete_at(check_index) else - # letter is not avaialable return false end end - - # letter is avaialble return true end @@ -128,15 +127,19 @@ def is_in_english_dict?(input) # iterate through each row in csv file # each row is an array, so check the 0th index - CSV.foreach("../assets/dictionary-english.csv") do |row| + english_words = CSV.open("assets/dictionary-english.csv", "r") + english_words.each do |row| + # check if user input word is found within dictionary if input.upcase == row[0].upcase return true - end + end end return false end + +puts is_in_english_dict?('cat') diff --git a/specs/adagrams_spec.rb b/specs/adagrams_spec.rb index ae2ccd0..6c18423 100644 --- a/specs/adagrams_spec.rb +++ b/specs/adagrams_spec.rb @@ -168,4 +168,35 @@ expect(best_word[:score]).must_equal 18 end end + + + describe 'is_in_english_dict? method' do + + it 'returns true for valid english words from csv file' do + + word = 'bladder' + + result = is_in_english_dict?(word) + + expect( result ).must_equal true + + end + + + # it 'returns false for gibberish' + # + # end + + # it 'returns false if empty string' + # + # end + + # it 'raise ArgumentError if input is not string' + # + # end + + + + end + end diff --git a/wave-5-game.rb b/wave-5-game.rb index 344333e..4ce4bcb 100644 --- a/wave-5-game.rb +++ b/wave-5-game.rb @@ -64,11 +64,15 @@ def run_game while ( (!(uses_available_letters?(user_input_word, letter_bank)) ) || (!(is_in_english_dict?(user_input_word))) ) + # puts "Beginning of while loop, letter_bank = #{letter_bank}" if !(uses_available_letters?(user_input_word, letter_bank)) display_not_in_letter_bank_message elsif !(is_in_english_dict?(user_input_word)) display_not_in_dictionary_message end + + # puts "End of while loop, letter_bank = #{letter_bank}" + user_input_word = get_user_input end From 9cde3e9b9f198f4ba8675464ea51ded592c40b3f Mon Sep 17 00:00:00 2001 From: Aurea Date: Wed, 15 Aug 2018 14:51:53 -0700 Subject: [PATCH 07/16] starting tests on wave 5, fix pointer error --- lib/adagrams.rb | 22 +++++++++++++++++++--- specs/adagrams_spec.rb | 18 +++++++++--------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index cbd707f..f1e5149 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -10,14 +10,20 @@ def draw_letters # randomize letters pool.shuffle! + # return the first 10 letters from randomized pool return pool.pop(10) end def uses_available_letters?(input, letters) - # check through each character; is it available? + # input --> user's word (string) + # letters --> available letters in pool + + # made a clone(?) of letters; previously, we ran into pointer/duplication issues + # we don't want to alter letters since it needs to reset with all 10 pulled letters available letters_altered = letters.clone + # check through each character; is it available? input.upcase.chars.each do |char| if letters_altered.include? char @@ -26,13 +32,17 @@ def uses_available_letters?(input, letters) letters_altered.delete_at(check_index) else + # return false if user does NOT have available lettesr for input return false end end + # return true if user has available letters for input return true end def score_word(word) + # word --> string; user's input word + # initialize scoring value score = 0 @@ -76,11 +86,14 @@ def score_word(word) score += 8 end + # return score as int return score end def highest_score_from(words) + # word --> string; user's input word + # initializing values highest_score = 0 @@ -119,11 +132,14 @@ def highest_score_from(words) end end + # return hash containing highest scoring word AND the score for that word return {:word => best_word, :score => highest_score} end def is_in_english_dict?(input) + # input --> string; user's input word + # iterate through each row in csv file # each row is an array, so check the 0th index @@ -132,14 +148,14 @@ def is_in_english_dict?(input) # check if user input word is found within dictionary if input.upcase == row[0].upcase + # return true if word is in dictionary (csv file) return true end end + # return false if word is NOT dictionary (csv file) return false end - -puts is_in_english_dict?('cat') diff --git a/specs/adagrams_spec.rb b/specs/adagrams_spec.rb index 6c18423..1f61fe7 100644 --- a/specs/adagrams_spec.rb +++ b/specs/adagrams_spec.rb @@ -183,17 +183,17 @@ end - # it 'returns false for gibberish' - # - # end + it 'returns false for gibberish' - # it 'returns false if empty string' - # - # end + end + + it 'returns false if empty string' + + end + + it 'raise ArgumentError if input is not string' - # it 'raise ArgumentError if input is not string' - # - # end + end From 53fcd3f7cb54ef879277383cc920cd04555a11bc Mon Sep 17 00:00:00 2001 From: Michelle Date: Wed, 15 Aug 2018 15:02:54 -0700 Subject: [PATCH 08/16] added testing for wave 5 non-English words --- specs/adagrams_spec.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/specs/adagrams_spec.rb b/specs/adagrams_spec.rb index 1f61fe7..c83b23c 100644 --- a/specs/adagrams_spec.rb +++ b/specs/adagrams_spec.rb @@ -183,18 +183,24 @@ end - it 'returns false for gibberish' + it 'returns false for gibberish' do - end - - it 'returns false if empty string' + word = "jfdks" - end + result = is_in_english_dict?(word) - it 'raise ArgumentError if input is not string' + expect(result).must_equal false end + # it 'returns false if empty string' do + # + # end + # + # it 'raise ArgumentError if input is not string' do + # + # end + end From f3da14f2f133a05eec07f91f9f09f04486b1661b Mon Sep 17 00:00:00 2001 From: Aurea Date: Wed, 15 Aug 2018 15:12:47 -0700 Subject: [PATCH 09/16] add tests for valid and nonvalid words in dict --- specs/adagrams_spec.rb | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/specs/adagrams_spec.rb b/specs/adagrams_spec.rb index c83b23c..6cdeb02 100644 --- a/specs/adagrams_spec.rb +++ b/specs/adagrams_spec.rb @@ -175,9 +175,19 @@ it 'returns true for valid english words from csv file' do word = 'bladder' + result = is_in_english_dict?(word) + expect( result ).must_equal true + + word = 'pierce' + result = is_in_english_dict?(word) + expect( result ).must_equal true + word = 'CoCoNUTs' result = is_in_english_dict?(word) + expect( result ).must_equal true + word = 'XYLOPHONE' + result = is_in_english_dict?(word) expect( result ).must_equal true end @@ -185,11 +195,21 @@ it 'returns false for gibberish' do - word = "jfdks" + word = "jfdks" + result = is_in_english_dict?(word) + expect(result).must_equal false + + word = "SSSSSSSS" + result = is_in_english_dict?(word) + expect(result).must_equal false - result = is_in_english_dict?(word) + word = "iIiIi" + result = is_in_english_dict?(word) + expect(result).must_equal false - expect(result).must_equal false + word = "pupppy" + result = is_in_english_dict?(word) + expect(result).must_equal false end From 7a455c39b5e4c51e4ae724821820d2194016464d Mon Sep 17 00:00:00 2001 From: Michelle Date: Wed, 15 Aug 2018 15:18:39 -0700 Subject: [PATCH 10/16] finished last test for empty string entry --- specs/adagrams_spec.rb | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/specs/adagrams_spec.rb b/specs/adagrams_spec.rb index 6cdeb02..80ce9d5 100644 --- a/specs/adagrams_spec.rb +++ b/specs/adagrams_spec.rb @@ -213,16 +213,12 @@ end - # it 'returns false if empty string' do - # - # end - # - # it 'raise ArgumentError if input is not string' do - # - # end - + it 'returns false if empty string' do + word = "" + result = is_in_english_dict?(word) + expect(result).must_equal false + end end - end From 9769ad5c73c6df6715b5c09777886b9cce2f1693 Mon Sep 17 00:00:00 2001 From: Aurea Date: Wed, 15 Aug 2018 15:45:37 -0700 Subject: [PATCH 11/16] minor refactoring of methods --- lib/adagrams.rb | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index f1e5149..71a9c30 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -7,29 +7,26 @@ def draw_letters # letter pool pool = ('A'*9 + 'B'*2 + 'C'*2 + 'D'*4 + 'E'*12 + 'F'*2 + 'G'*3 + 'H'*2 + 'I'*9 + 'J' + 'K' + 'L'*4 + 'M'*2 + 'N'*6 + 'O'*8 + 'P'*2 + 'Q' + 'R'*6 + 'S'*4 + 'T'*6 + 'U'*4 + 'V'*2 + 'W'*2 + 'X' + 'Y'*2 + 'Z').chars - # randomize letters - pool.shuffle! - - # return the first 10 letters from randomized pool - return pool.pop(10) + # randomize and return the first 10 letters from randomized pool + return pool.shuffle.pop(10) end -def uses_available_letters?(input, letters) +def uses_available_letters?(input, letters_orig) # input --> user's word (string) # letters --> available letters in pool # made a clone(?) of letters; previously, we ran into pointer/duplication issues # we don't want to alter letters since it needs to reset with all 10 pulled letters available - letters_altered = letters.clone + letters = letters_orig.clone # check through each character; is it available? input.upcase.chars.each do |char| - if letters_altered.include? char + if letters.include? char # delete letter once user inputs it - check_index = letters_altered.index(char) - letters_altered.delete_at(check_index) + check_index = letters.index(char) + letters.delete_at(check_index) else # return false if user does NOT have available lettesr for input @@ -102,23 +99,19 @@ def highest_score_from(words) # loop through each word in array of user input words.each do |word| - if score_word(word) > highest_score # if we have found the highest scoring word, set variable equal to that score and that word highest_score = score_word(word) best_word = word - # check for score ties - elsif score_word(word) == highest_score + # check for score ties and that length is unequal + elsif score_word(word) == highest_score && word.length != best_word.length - # check that the lengths are unequal, since that matters for winning - if word.length != best_word.length # check if highest scoring word length is 10; if so, that's the winning word if word.length == 10 highest_score = score_word(word) best_word = word - end # check that word length is less than best word's length AND that it does not equal 10 @@ -126,21 +119,18 @@ def highest_score_from(words) if word.length < best_word.length && best_word.length != 10 highest_score = score_word(word) best_word = word - end - end end end # return hash containing highest scoring word AND the score for that word - return {:word => best_word, - :score => highest_score} + return {word: best_word, + score: highest_score} end def is_in_english_dict?(input) # input --> string; user's input word - # iterate through each row in csv file # each row is an array, so check the 0th index english_words = CSV.open("assets/dictionary-english.csv", "r") From d3c634b7625a58902c432fc2077a64521565e050 Mon Sep 17 00:00:00 2001 From: Michelle Date: Wed, 15 Aug 2018 16:27:31 -0700 Subject: [PATCH 12/16] try turning array of words into hash for greater efficiency --- lib/adagrams.rb | 73 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 22 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 71a9c30..9437d70 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,32 +1,38 @@ # adds csv gem require "csv" +require "pry" + +$words_hash = {} def draw_letters # letter pool pool = ('A'*9 + 'B'*2 + 'C'*2 + 'D'*4 + 'E'*12 + 'F'*2 + 'G'*3 + 'H'*2 + 'I'*9 + 'J' + 'K' + 'L'*4 + 'M'*2 + 'N'*6 + 'O'*8 + 'P'*2 + 'Q' + 'R'*6 + 'S'*4 + 'T'*6 + 'U'*4 + 'V'*2 + 'W'*2 + 'X' + 'Y'*2 + 'Z').chars - # randomize and return the first 10 letters from randomized pool - return pool.shuffle.pop(10) + # randomize letters + pool.shuffle! + + # return the first 10 letters from randomized pool + return pool.pop(10) end -def uses_available_letters?(input, letters_orig) +def uses_available_letters?(input, letters) # input --> user's word (string) # letters --> available letters in pool # made a clone(?) of letters; previously, we ran into pointer/duplication issues # we don't want to alter letters since it needs to reset with all 10 pulled letters available - letters = letters_orig.clone + letters_altered = letters.clone # check through each character; is it available? input.upcase.chars.each do |char| - if letters.include? char + if letters_altered.include? char # delete letter once user inputs it - check_index = letters.index(char) - letters.delete_at(check_index) + check_index = letters_altered.index(char) + letters_altered.delete_at(check_index) else # return false if user does NOT have available lettesr for input @@ -105,13 +111,16 @@ def highest_score_from(words) highest_score = score_word(word) best_word = word - # check for score ties and that length is unequal - elsif score_word(word) == highest_score && word.length != best_word.length + # check for score ties + elsif score_word(word) == highest_score + # check that the lengths are unequal, since that matters for winning + if word.length != best_word.length # check if highest scoring word length is 10; if so, that's the winning word if word.length == 10 highest_score = score_word(word) best_word = word + end # check that word length is less than best word's length AND that it does not equal 10 @@ -119,33 +128,53 @@ def highest_score_from(words) if word.length < best_word.length && best_word.length != 10 highest_score = score_word(word) best_word = word + end + end end end # return hash containing highest scoring word AND the score for that word - return {word: best_word, - score: highest_score} + return {:word => best_word, + :score => highest_score} end -def is_in_english_dict?(input) - # input --> string; user's input word +def create_hash() + -# iterate through each row in csv file -# each row is an array, so check the 0th index english_words = CSV.open("assets/dictionary-english.csv", "r") + english_words.each do |row| + $words_hash[row[0].upcase] = 0 + end + return $words_hash +end - # check if user input word is found within dictionary - if input.upcase == row[0].upcase - # return true if word is in dictionary (csv file) - return true +def is_in_english_dict?(input) + # input --> string; user's input word - end + +# iterate through each row in csv file +# each row is an array, so check the 0th index + puts $words_hash[input] + if $words_hash[input] + return true + else + return false end + # # check if user input word is found within dictionary + # if input.upcase == row[0].upcase + # # return true if word is in dictionary (csv file) + # return true + # + # end + # end + # return false if word is NOT dictionary (csv file) - return false + # return false +end -end +puts create_hash() +puts is_in_english_dict?("cat") From 3ac2b3f33d869e450bab33a50b0cc693905798f5 Mon Sep 17 00:00:00 2001 From: Aurea Date: Wed, 15 Aug 2018 16:32:15 -0700 Subject: [PATCH 13/16] implement csv file as hash for efficiency --- lib/adagrams.rb | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 9437d70..230847d 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -139,15 +139,14 @@ def highest_score_from(words) :score => highest_score} end -def create_hash() +def create_hash() english_words = CSV.open("assets/dictionary-english.csv", "r") english_words.each do |row| $words_hash[row[0].upcase] = 0 end - return $words_hash end def is_in_english_dict?(input) @@ -157,24 +156,13 @@ def is_in_english_dict?(input) # iterate through each row in csv file # each row is an array, so check the 0th index puts $words_hash[input] - if $words_hash[input] + if $words_hash[input.upcase] return true else return false end - # # check if user input word is found within dictionary - # if input.upcase == row[0].upcase - # # return true if word is in dictionary (csv file) - # return true - # - # end - # end - - # return false if word is NOT dictionary (csv file) - # return false end puts create_hash() -puts is_in_english_dict?("cat") From 0c33fb9b23d528147f5eb0ad0bbd72db1e077179 Mon Sep 17 00:00:00 2001 From: Michelle Date: Wed, 15 Aug 2018 16:43:36 -0700 Subject: [PATCH 14/16] updated comments to reflect changes related to csv file --- lib/adagrams.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 230847d..11230c8 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,8 +1,8 @@ # adds csv gem require "csv" -require "pry" +# initialize global variable $words_hash = {} def draw_letters @@ -97,7 +97,6 @@ def score_word(word) def highest_score_from(words) # word --> string; user's input word - # initializing values highest_score = 0 best_word = '' @@ -135,16 +134,20 @@ def highest_score_from(words) end # return hash containing highest scoring word AND the score for that word - return {:word => best_word, - :score => highest_score} + return {word: best_word, + score: highest_score} end def create_hash() + # read csv file english_words = CSV.open("assets/dictionary-english.csv", "r") + # iterate through each row in csv file english_words.each do |row| + # each row is an array, so check the 0th index + # {key, value} --> {"WORD", 0} $words_hash[row[0].upcase] = 0 end end @@ -152,17 +155,14 @@ def create_hash() def is_in_english_dict?(input) # input --> string; user's input word - -# iterate through each row in csv file -# each row is an array, so check the 0th index - puts $words_hash[input] + # will return a value --> truthy if $words_hash[input.upcase] return true + # if no truthy value, then false else return false end end - -puts create_hash() +create_hash() From 99faba220843019b70cc572fb67dc87328e8b936 Mon Sep 17 00:00:00 2001 From: Michelle Date: Thu, 16 Aug 2018 13:56:19 -0700 Subject: [PATCH 15/16] refactored the code --- lib/adagrams.rb | 59 ++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 11230c8..35ec83d 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -11,10 +11,8 @@ def draw_letters pool = ('A'*9 + 'B'*2 + 'C'*2 + 'D'*4 + 'E'*12 + 'F'*2 + 'G'*3 + 'H'*2 + 'I'*9 + 'J' + 'K' + 'L'*4 + 'M'*2 + 'N'*6 + 'O'*8 + 'P'*2 + 'Q' + 'R'*6 + 'S'*4 + 'T'*6 + 'U'*4 + 'V'*2 + 'W'*2 + 'X' + 'Y'*2 + 'Z').chars # randomize letters - pool.shuffle! - # return the first 10 letters from randomized pool - return pool.pop(10) + return pool.shuffle.pop(10) end @@ -24,15 +22,15 @@ def uses_available_letters?(input, letters) # made a clone(?) of letters; previously, we ran into pointer/duplication issues # we don't want to alter letters since it needs to reset with all 10 pulled letters available - letters_altered = letters.clone + letters_copy = letters.clone # check through each character; is it available? input.upcase.chars.each do |char| - if letters_altered.include? char + if letters_copy.include? char # delete letter once user inputs it - check_index = letters_altered.index(char) - letters_altered.delete_at(check_index) + check_index = letters_copy.index(char) + letters_copy.delete_at(check_index) else # return false if user does NOT have available lettesr for input @@ -46,8 +44,6 @@ def uses_available_letters?(input, letters) def score_word(word) # word --> string; user's input word - # initialize scoring value - score = 0 # create scoring chart score_chart = { @@ -80,8 +76,8 @@ def score_word(word) } # add scores together - word.upcase.chars.each do |char| - score += score_chart[char] + score = word.upcase.chars.sum do |char| + score_chart[char] end # add eight extra points for words 7+ in length @@ -111,24 +107,18 @@ def highest_score_from(words) best_word = word # check for score ties - elsif score_word(word) == highest_score + # check that the lengths are unequal, since that matters for winning - # check that the lengths are unequal, since that matters for winning - if word.length != best_word.length - # check if highest scoring word length is 10; if so, that's the winning word - if word.length == 10 - highest_score = score_word(word) - best_word = word + elsif score_word(word) == highest_score && word.length != best_word.length - end - # check that word length is less than best word's length AND that it does not equal 10 - # if there is a tie between two high scoring words (and the current best word is not a length 10; otherwise, it wins), then choose the shortest length - if word.length < best_word.length && best_word.length != 10 - highest_score = score_word(word) - best_word = word - end + # check if highest scoring word length is 10; if so, that's the winning word + # check that word length is less than best word's length AND that it does not equal 10 + # if there is a tie between two high scoring words (and the current best word is not a length 10; otherwise, it wins), then choose the shortest length + if (word.length == 10) || (word.length < best_word.length && best_word.length != 10) + highest_score = score_word(word) + best_word = word end end end @@ -155,13 +145,18 @@ def create_hash() def is_in_english_dict?(input) # input --> string; user's input word - # will return a value --> truthy - if $words_hash[input.upcase] - return true - # if no truthy value, then false - else - return false - end + # if it returns a value --> true + # otherwise, false + $words_hash.include?(input.upcase) ? true : false + + # NOTE: we did this first, but decided to create a hash for enhanced efficiency +# english_words = CSV.open("assets/dictionary-english.csv", "r") +# english_words.each do |row| +# # if input.upcase == row[0].upcase +# return true if word is in dictionary (csv file) +# return true +# end +# return false end From 699f50da896a18cabd61def4ac83f7a16b9bc211 Mon Sep 17 00:00:00 2001 From: Michelle Date: Thu, 16 Aug 2018 14:28:05 -0700 Subject: [PATCH 16/16] tidied up the comments --- lib/adagrams.rb | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 35ec83d..3e9b44c 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,11 +1,12 @@ - # adds csv gem require "csv" # initialize global variable $words_hash = {} +# wave 1 def draw_letters + # draw letters # letter pool pool = ('A'*9 + 'B'*2 + 'C'*2 + 'D'*4 + 'E'*12 + 'F'*2 + 'G'*3 + 'H'*2 + 'I'*9 + 'J' + 'K' + 'L'*4 + 'M'*2 + 'N'*6 + 'O'*8 + 'P'*2 + 'Q' + 'R'*6 + 'S'*4 + 'T'*6 + 'U'*4 + 'V'*2 + 'W'*2 + 'X' + 'Y'*2 + 'Z').chars @@ -15,20 +16,20 @@ def draw_letters return pool.shuffle.pop(10) end - +# wave 2 def uses_available_letters?(input, letters) # input --> user's word (string) # letters --> available letters in pool - # made a clone(?) of letters; previously, we ran into pointer/duplication issues - # we don't want to alter letters since it needs to reset with all 10 pulled letters available + # made a clone of letters; previously, we ran into memory location/duplication issues + # we don't want to alter letters since it needs to reset with all 10 pulled letters available upon each call letters_copy = letters.clone - # check through each character; is it available? + # check through each character; is it available from the letter pool? input.upcase.chars.each do |char| if letters_copy.include? char - # delete letter once user inputs it + # delete letter once user inputs it to avoid multiple checks on each tile check_index = letters_copy.index(char) letters_copy.delete_at(check_index) @@ -41,10 +42,10 @@ def uses_available_letters?(input, letters) return true end +# wave 3 def score_word(word) # word --> string; user's input word - # create scoring chart score_chart = { "A" => 1, @@ -76,9 +77,7 @@ def score_word(word) } # add scores together - score = word.upcase.chars.sum do |char| - score_chart[char] - end + score = word.upcase.chars.sum { |char| score_chart[char] } # add eight extra points for words 7+ in length if word.length >= 7 @@ -89,7 +88,7 @@ def score_word(word) return score end - +# wave 4 def highest_score_from(words) # word --> string; user's input word @@ -106,16 +105,12 @@ def highest_score_from(words) highest_score = score_word(word) best_word = word - # check for score ties - # check that the lengths are unequal, since that matters for winning + # check for score ties AND check if the word lengths are unequal + elsif score_word(word) == highest_score && word.length != best_word.length - elsif score_word(word) == highest_score && word.length != best_word.length - - - - # check if highest scoring word length is 10; if so, that's the winning word - # check that word length is less than best word's length AND that it does not equal 10 - # if there is a tie between two high scoring words (and the current best word is not a length 10; otherwise, it wins), then choose the shortest length + # check if word length is 10 -- OR... + # check that word length is less than best word's length AND that best word length does not equal 10 + # if there is a tie between two equally high scoring words (and the current best word is not a length 10), then choose the shorter length if (word.length == 10) || (word.length < best_word.length && best_word.length != 10) highest_score = score_word(word) best_word = word @@ -129,35 +124,38 @@ def highest_score_from(words) end - +# wave 5 addition def create_hash() + # we will create a hash for greater efficiency + # read csv file english_words = CSV.open("assets/dictionary-english.csv", "r") # iterate through each row in csv file english_words.each do |row| # each row is an array, so check the 0th index - # {key, value} --> {"WORD", 0} + # create hash key/value pairs: {key, value} --> {"WORD", 0} $words_hash[row[0].upcase] = 0 end end +# wave 5 def is_in_english_dict?(input) # input --> string; user's input word - # if it returns a value --> true + # if it returns a truthy value --> true # otherwise, false $words_hash.include?(input.upcase) ? true : false - # NOTE: we did this first, but decided to create a hash for enhanced efficiency + # NOTE: we did this first, but decided to create a hash for retrieval efficiency and only load it once also for additional enhanced efficiency... # english_words = CSV.open("assets/dictionary-english.csv", "r") # english_words.each do |row| # # if input.upcase == row[0].upcase -# return true if word is in dictionary (csv file) # return true # end # return false end +# invoke method to create hash of English words create_hash()