From c370171a03f18eb0de809a7f3e0452f11c4986ca Mon Sep 17 00:00:00 2001 From: Brittany Jones Date: Tue, 20 Feb 2018 13:57:23 -0800 Subject: [PATCH 01/28] fixed typo --- wave-1-game.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wave-1-game.rb b/wave-1-game.rb index da13d000..416a0135 100644 --- a/wave-1-game.rb +++ b/wave-1-game.rb @@ -44,7 +44,7 @@ def print_score(word) end def conclude - highest_word = Scrabble::Scoring.highest_score_from_array(@words) + highest_word = Scrabble::Scoring.highest_score_from(@words) puts "The final highest scoring word is #{ highest_word }" end end From c126eefaf05d18f56a27c3410944039287f96c14 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Tue, 20 Feb 2018 13:58:40 -0800 Subject: [PATCH 02/28] method name correction --- wave-1-game.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wave-1-game.rb b/wave-1-game.rb index da13d000..416a0135 100644 --- a/wave-1-game.rb +++ b/wave-1-game.rb @@ -44,7 +44,7 @@ def print_score(word) end def conclude - highest_word = Scrabble::Scoring.highest_score_from_array(@words) + highest_word = Scrabble::Scoring.highest_score_from(@words) puts "The final highest scoring word is #{ highest_word }" end end From 1c34c1299983aa1b3b8c123bd7eca4f29a3a7e31 Mon Sep 17 00:00:00 2001 From: Brittany Jones Date: Tue, 20 Feb 2018 15:00:47 -0800 Subject: [PATCH 03/28] Starting code for scoring.rb --- lib/scoring.rb | 23 +++++++++++++++++++++-- specs/scoring_spec.rb | 20 ++++++++++---------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index fb3a3f2d..bf3cf3f4 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,9 +1,28 @@ module Scrabble class Scoring - def self.score(word) + + def initialize + @scores = { + 1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], + 2 => ["D", "G"], + 3 => ["B", "C", "M", "P"], + 4 => ["F", "H", "V", "W", "Y"], + 5 => ["K"], + 8 => ["J", "X"], + 10 => ["Q", "Z"] + } end - def self.highest_score_from(array_of_words) + def self.score(word) + count = 0 + @scores.length.each do |key, value| + count += @scores.key + end end end end + +# def self.highest_score_from(array_of_words) +# end +# end +# end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index ab498929..fa8bab26 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -15,48 +15,48 @@ Scrabble::Scoring.score('pig').must_equal 6 end - it 'adds 50 points for a 7-letter word' do + xit 'adds 50 points for a 7-letter word' do Scrabble::Scoring.score('academy').must_equal 65 end - it 'handles all upper- and lower-case letters' do + xit 'handles all upper- and lower-case letters' do Scrabble::Scoring.score('dog').must_equal 5 Scrabble::Scoring.score('DOG').must_equal 5 Scrabble::Scoring.score('DoG').must_equal 5 end - it 'returns nil for strings containing bad characters' do + xit 'returns nil for strings containing bad characters' do Scrabble::Scoring.score('#$%^').must_be_nil Scrabble::Scoring.score('char^').must_be_nil Scrabble::Scoring.score(' ').must_be_nil end - it 'returns nil for words > 7 letters' do + xit 'returns nil for words > 7 letters' do Scrabble::Scoring.score('abcdefgh').must_be_nil end - it 'returns nil for empty words' do + xit 'returns nil for empty words' do Scrabble::Scoring.score('').must_be_nil end end describe 'highest_score_from' do - it 'returns nil if no words were passed' do + xit 'returns nil if no words were passed' do end - it 'returns the only word in a length-1 array' do + xit 'returns the only word in a length-1 array' do end it 'returns the highest word if there are two words' do end - it 'if tied, prefer a word with 7 letters' do + xit 'if tied, prefer a word with 7 letters' do end - it 'if tied and no word has 7 letters, prefers the word with fewer letters' do + xit 'if tied and no word has 7 letters, prefers the word with fewer letters' do end - it 'returns the first word of a tie with same letter count' do + xit 'returns the first word of a tie with same letter count' do end end end From 6a53e74b06ac963e8ba09172980f0649c748609d Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Tue, 20 Feb 2018 15:15:19 -0800 Subject: [PATCH 04/28] Return 5, Pass a test --- lib/scoring.rb | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index bf3cf3f4..50379b37 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -3,21 +3,18 @@ class Scoring def initialize @scores = { - 1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], - 2 => ["D", "G"], - 3 => ["B", "C", "M", "P"], - 4 => ["F", "H", "V", "W", "Y"], - 5 => ["K"], - 8 => ["J", "X"], - 10 => ["Q", "Z"] + "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 } end def self.score(word) - count = 0 - @scores.length.each do |key, value| - count += @scores.key - end + return 5 end end end From 4d0a19236430677bc61e059ef3a7346c37042d1e Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Tue, 20 Feb 2018 15:16:05 -0800 Subject: [PATCH 05/28] modifying for one test at a time --- specs/scoring_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index fa8bab26..ffa090d7 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -11,8 +11,8 @@ describe 'score' do it 'correctly scores simple words' do Scrabble::Scoring.score('dog').must_equal 5 - Scrabble::Scoring.score('cat').must_equal 5 - Scrabble::Scoring.score('pig').must_equal 6 + # Scrabble::Scoring.score('cat').must_equal 5 + # Scrabble::Scoring.score('pig').must_equal 6 end xit 'adds 50 points for a 7-letter word' do @@ -47,7 +47,7 @@ xit 'returns the only word in a length-1 array' do end - it 'returns the highest word if there are two words' do + xit 'returns the highest word if there are two words' do end xit 'if tied, prefer a word with 7 letters' do From c0b7f76b00d2dd3b11368041f06392493db62e3a Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Tue, 20 Feb 2018 15:57:47 -0800 Subject: [PATCH 06/28] Notes on how to find score --- lib/scoring.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 50379b37..9a3f6000 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,8 +1,8 @@ module Scrabble class Scoring - def initialize - @scores = { + def self.score(word) + @all_letters = { "A"=>1, "B"=>3, "C"=>3, "D"=>2, "E"=>1, "F"=>4, "G"=>2, "H"=>4, "I"=>1, "J"=>8, "K"=>5, "L"=>1, @@ -11,9 +11,14 @@ def initialize "U"=>1, "V"=>4, "W"=>4, "X"=>8, "Y"=>4, "Z"=>10 } - end - def self.score(word) + @all_letters.each {|letter, points_per_letter| print letter, " ", points_per_letter, "\n"} + + # split word >> goes into array + # check if + # if letter array is a key, return the value of that + # add the values + return 5 end end From 8eb7063a928df86d8c1e053f6343f4670630774a Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Tue, 20 Feb 2018 16:24:04 -0800 Subject: [PATCH 07/28] split word in array --- lib/scoring.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 9a3f6000..6d41fadc 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -11,8 +11,23 @@ def self.score(word) "U"=>1, "V"=>4, "W"=>4, "X"=>8, "Y"=>4, "Z"=>10 } + # total_points = 0 + word = 'DOG' + p word + word_array = word.split("") + p word_array + p word_array[0] - @all_letters.each {|letter, points_per_letter| print letter, " ", points_per_letter, "\n"} + + if @all_letters.has_key?(word_array[0]) + puts "TRUE #{word + [0]}" + end + + + + + # @all_letters.each {|letter, points_per_letter| print letter, " ", points_per_letter, "\n"} # split word >> goes into array # check if From afa7ae88e6e689cd04d3020790f1ad9eb7af2037 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Tue, 20 Feb 2018 17:36:52 -0800 Subject: [PATCH 08/28] correctly scores simple words --- lib/scoring.rb | 28 +++++++++------------------- specs/scoring_spec.rb | 4 ++-- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 6d41fadc..a1d457a7 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -12,29 +12,19 @@ def self.score(word) "Y"=>4, "Z"=>10 } # total_points = 0 - word = 'DOG' - p word - word_array = word.split("") - p word_array - p word_array[0] + word = word.upcase + letters = word.split("") + values_of_letters = [] - if @all_letters.has_key?(word_array[0]) - puts "TRUE #{word - [0]}" - end + letters.each do |letter| + #.fetch - could help bad characters + values_of_letters << @all_letters.fetch(letter) + end + sum = values_of_letters.sum + return sum - - - # @all_letters.each {|letter, points_per_letter| print letter, " ", points_per_letter, "\n"} - - # split word >> goes into array - # check if - # if letter array is a key, return the value of that - # add the values - - return 5 end end end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index ffa090d7..f5db6210 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -11,8 +11,8 @@ describe 'score' do it 'correctly scores simple words' do Scrabble::Scoring.score('dog').must_equal 5 - # Scrabble::Scoring.score('cat').must_equal 5 - # Scrabble::Scoring.score('pig').must_equal 6 + Scrabble::Scoring.score('cat').must_equal 5 + Scrabble::Scoring.score('pig').must_equal 6 end xit 'adds 50 points for a 7-letter word' do From 63a2c990ee1148202b358f035d6f65f6f2a96e36 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Tue, 20 Feb 2018 18:01:55 -0800 Subject: [PATCH 09/28] Add 50 for >7 length --- lib/scoring.rb | 12 ++++++++++-- specs/scoring_spec.rb | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index a1d457a7..7a8530d0 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -22,8 +22,16 @@ def self.score(word) values_of_letters << @all_letters.fetch(letter) end - sum = values_of_letters.sum - return sum + # p letters.length + + if letters.length < 7 + sum = values_of_letters.sum + return sum + else + sum = values_of_letters.sum + sum = sum + 50 + return sum + end end end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index f5db6210..17e4bb4d 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -15,7 +15,7 @@ Scrabble::Scoring.score('pig').must_equal 6 end - xit 'adds 50 points for a 7-letter word' do + it 'adds 50 points for a 7-letter word' do Scrabble::Scoring.score('academy').must_equal 65 end From 7d3276e0f9ac4befd29b2d33c00199bd3ef73e78 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Wed, 21 Feb 2018 15:19:19 -0800 Subject: [PATCH 10/28] Return nil for invalid characters --- lib/scoring.rb | 42 +++++++++++++++++++++++++----------------- specs/scoring_spec.rb | 4 ++-- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 7a8530d0..96422f39 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -11,33 +11,41 @@ def self.score(word) "U"=>1, "V"=>4, "W"=>4, "X"=>8, "Y"=>4, "Z"=>10 } - # total_points = 0 word = word.upcase letters = word.split("") values_of_letters = [] - letters.each do |letter| - #.fetch - could help bad characters - values_of_letters << @all_letters.fetch(letter) - end + letters.each do |character| + # Checking if character input is a valid letter or not + # First part is splitting the hash into an array of the + # key values 'A-Z' and then we are checking if any characters + # are outside that range. If so, automatically return nil + # Else, if all the characters are valid letters, take the value + # associated with the key and put that in an array of integers + + if !@all_letters.keys.include?(character) + return nil + else + values_of_letters << @all_letters.fetch(character) + end + end - # p letters.length - if letters.length < 7 - sum = values_of_letters.sum - return sum - else + if letters.length < 7 sum = values_of_letters.sum - sum = sum + 50 - return sum - end + return sum + else + sum = values_of_letters.sum + sum = sum + 50 + return sum + end end end end -# def self.highest_score_from(array_of_words) -# end -# end -# end + # def self.highest_score_from(array_of_words) + # end + # end + # end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 17e4bb4d..dcf2a598 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -15,7 +15,7 @@ Scrabble::Scoring.score('pig').must_equal 6 end - it 'adds 50 points for a 7-letter word' do + xit 'adds 50 points for a 7-letter word' do Scrabble::Scoring.score('academy').must_equal 65 end @@ -25,7 +25,7 @@ Scrabble::Scoring.score('DoG').must_equal 5 end - xit 'returns nil for strings containing bad characters' do + it 'returns nil for strings containing bad characters' do Scrabble::Scoring.score('#$%^').must_be_nil Scrabble::Scoring.score('char^').must_be_nil Scrabble::Scoring.score(' ').must_be_nil From 15f5ac547d879cadccbf087aeb3ec6bc9d9fa194 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Wed, 21 Feb 2018 15:29:36 -0800 Subject: [PATCH 11/28] return nil for word lenght > 7 --- lib/scoring.rb | 10 +++++++--- specs/scoring_spec.rb | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 96422f39..c9ad8f9b 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -23,7 +23,7 @@ def self.score(word) # are outside that range. If so, automatically return nil # Else, if all the characters are valid letters, take the value # associated with the key and put that in an array of integers - + if !@all_letters.keys.include?(character) return nil else @@ -32,14 +32,18 @@ def self.score(word) end - if letters.length < 7 + if letters.length > 0 && letters.length < 7 sum = values_of_letters.sum return sum - else + elsif letters.length == 7 sum = values_of_letters.sum sum = sum + 50 return sum + + else + return nil + end end end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index dcf2a598..148e99e5 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -31,7 +31,7 @@ Scrabble::Scoring.score(' ').must_be_nil end - xit 'returns nil for words > 7 letters' do + it 'returns nil for words > 7 letters' do Scrabble::Scoring.score('abcdefgh').must_be_nil end From a16c89643473fbbe8c9cd6506dd32b18fd9c5880 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Wed, 21 Feb 2018 16:35:46 -0800 Subject: [PATCH 12/28] highest score, length == 0 & length == 1 --- lib/scoring.rb | 26 +++++++++++++++++--------- specs/scoring_spec.rb | 14 +++++++++----- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index c9ad8f9b..8043e6a8 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -13,21 +13,24 @@ def self.score(word) } word = word.upcase letters = word.split("") - + character_check = @all_letters.keys values_of_letters = [] letters.each do |character| # Checking if character input is a valid letter or not # First part is splitting the hash into an array of the # key values 'A-Z' and then we are checking if any characters - # are outside that range. If so, automatically return nil + # are outside that range (This includes blank or nothing). + # If so, automatically return nil + # # Else, if all the characters are valid letters, take the value # associated with the key and put that in an array of integers + # - if !@all_letters.keys.include?(character) + if !character_check.include?(character) return nil else - values_of_letters << @all_letters.fetch(character) + values_of_letters << @all_letters.fetch(character) end end @@ -43,13 +46,18 @@ def self.score(word) else return nil + end + end + + + def self.highest_score_from(array_of_words) + new_word = [] + if array_of_words.length == 0 + return nil + elsif array_of_words.length == 1 + new_word << array_of_words end end end end - - # def self.highest_score_from(array_of_words) - # end - # end - # end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 148e99e5..018ce99b 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -9,7 +9,7 @@ describe 'Scoring' do describe 'score' do - it 'correctly scores simple words' do + xit 'correctly scores simple words' do Scrabble::Scoring.score('dog').must_equal 5 Scrabble::Scoring.score('cat').must_equal 5 Scrabble::Scoring.score('pig').must_equal 6 @@ -25,13 +25,13 @@ Scrabble::Scoring.score('DoG').must_equal 5 end - it 'returns nil for strings containing bad characters' do + xit 'returns nil for strings containing bad characters' do Scrabble::Scoring.score('#$%^').must_be_nil Scrabble::Scoring.score('char^').must_be_nil Scrabble::Scoring.score(' ').must_be_nil end - it 'returns nil for words > 7 letters' do + xit 'returns nil for words > 7 letters' do Scrabble::Scoring.score('abcdefgh').must_be_nil end @@ -41,10 +41,14 @@ end describe 'highest_score_from' do - xit 'returns nil if no words were passed' do + it 'returns nil if no words were passed' do + no_words = [] + Scrabble::Scoring.highest_score_from(no_words).must_be_nil end - xit 'returns the only word in a length-1 array' do + it 'returns the only word in a length-1 array' do + one_word = ['one'] + Scrabble::Scoring.highest_score_from(one_word).must_equal [['one']] end xit 'returns the highest word if there are two words' do From 43a5bec6502d145b243d6be7fc63f66e1db8a172 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Wed, 21 Feb 2018 17:10:04 -0800 Subject: [PATCH 13/28] score two words, return highest one --- lib/scoring.rb | 12 ++++++++++++ specs/scoring_spec.rb | 12 +++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 8043e6a8..be763d6b 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -53,11 +53,23 @@ def self.score(word) def self.highest_score_from(array_of_words) new_word = [] + scores = [] + if array_of_words.length == 0 return nil elsif array_of_words.length == 1 new_word << array_of_words + return new_word + elsif array_of_words.length > 1 + array_of_words.each do |word| + scores << self.score(word) + end end + highest_score = scores.max + # array_of_words["Needs to be the max value printed with respect to the index"] + winning_word = array_of_words[scores.index(highest_score)] + # p winning_word + return winning_word end end end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 018ce99b..6a2d4bc6 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -42,16 +42,18 @@ describe 'highest_score_from' do it 'returns nil if no words were passed' do - no_words = [] - Scrabble::Scoring.highest_score_from(no_words).must_be_nil + array_of_words = [] + Scrabble::Scoring.highest_score_from(array_of_words).must_be_nil end it 'returns the only word in a length-1 array' do - one_word = ['one'] - Scrabble::Scoring.highest_score_from(one_word).must_equal [['one']] + array_of_words = ['one'] + Scrabble::Scoring.highest_score_from(array_of_words).must_equal [['one']] end - xit 'returns the highest word if there are two words' do + it 'returns the highest word if there are two words' do + array_of_words = ["ape", "zebra"] + Scrabble::Scoring.highest_score_from(array_of_words).must_equal("zebra") end xit 'if tied, prefer a word with 7 letters' do From f452d971ae85f12732aa627ab5fb75809ccee0be Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Thu, 22 Feb 2018 13:49:09 -0800 Subject: [PATCH 14/28] Refactored high score with hash to find ties --- lib/scoring.rb | 31 +++++++++++++++++-------------- specs/scoring_spec.rb | 8 ++++---- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index be763d6b..c51b4c29 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -11,8 +11,8 @@ def self.score(word) "U"=>1, "V"=>4, "W"=>4, "X"=>8, "Y"=>4, "Z"=>10 } - word = word.upcase - letters = word.split("") + @word = word.upcase + letters = @word.split("") character_check = @all_letters.keys values_of_letters = [] @@ -49,27 +49,30 @@ def self.score(word) end end - - def self.highest_score_from(array_of_words) - new_word = [] - scores = [] + + all_scores = {} if array_of_words.length == 0 return nil + elsif array_of_words.length == 1 - new_word << array_of_words - return new_word + return array_of_words + elsif array_of_words.length > 1 array_of_words.each do |word| - scores << self.score(word) + all_scores[word] = score(word) + # scores << score(word) end + + p all_scores end - highest_score = scores.max - # array_of_words["Needs to be the max value printed with respect to the index"] - winning_word = array_of_words[scores.index(highest_score)] - # p winning_word - return winning_word + + max_score_words = all_scores.delete_if { |word, score| score != all_scores.values.max }.keys + + # p max_score_words + return max_score_words + end end end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 6a2d4bc6..a43911dd 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -41,19 +41,19 @@ end describe 'highest_score_from' do - it 'returns nil if no words were passed' do + xit 'returns nil if no words were passed' do array_of_words = [] Scrabble::Scoring.highest_score_from(array_of_words).must_be_nil end it 'returns the only word in a length-1 array' do array_of_words = ['one'] - Scrabble::Scoring.highest_score_from(array_of_words).must_equal [['one']] + Scrabble::Scoring.highest_score_from(array_of_words).must_equal(['one']) end it 'returns the highest word if there are two words' do - array_of_words = ["ape", "zebra"] - Scrabble::Scoring.highest_score_from(array_of_words).must_equal("zebra") + array_of_words = ["zebraa", "zebras"] + Scrabble::Scoring.highest_score_from(array_of_words).must_equal(["zebraa", "zebras"]) end xit 'if tied, prefer a word with 7 letters' do From 833fa321873fae2492b82b0d1123c71e30cbac32 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Thu, 22 Feb 2018 16:36:49 -0800 Subject: [PATCH 15/28] refactoring --- lib/scoring.rb | 24 +++++------------------- specs/scoring_spec.rb | 10 +++++----- 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index c51b4c29..9f9b9ccd 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -27,50 +27,36 @@ def self.score(word) # associated with the key and put that in an array of integers # - if !character_check.include?(character) + if !character_check.include?(character) || letters.length == 0 || letters.length > 7 return nil else values_of_letters << @all_letters.fetch(character) end end + sum = values_of_letters.sum - if letters.length > 0 && letters.length < 7 - sum = values_of_letters.sum - return sum + return letters.length == 7 ? sum + 50 : sum - elsif letters.length == 7 - sum = values_of_letters.sum - sum = sum + 50 - return sum - - else - return nil - end end def self.highest_score_from(array_of_words) all_scores = {} - if array_of_words.length == 0 - return nil + return nil if array_of_words.length == 0 - elsif array_of_words.length == 1 + if array_of_words.length == 1 return array_of_words elsif array_of_words.length > 1 array_of_words.each do |word| all_scores[word] = score(word) - # scores << score(word) end - - p all_scores end max_score_words = all_scores.delete_if { |word, score| score != all_scores.values.max }.keys - # p max_score_words return max_score_words end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index a43911dd..8a9e6854 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -15,7 +15,7 @@ Scrabble::Scoring.score('pig').must_equal 6 end - xit 'adds 50 points for a 7-letter word' do + it 'adds 50 points for a 7-letter word' do Scrabble::Scoring.score('academy').must_equal 65 end @@ -31,7 +31,7 @@ Scrabble::Scoring.score(' ').must_be_nil end - xit 'returns nil for words > 7 letters' do + it 'returns nil for words > 7 letters' do Scrabble::Scoring.score('abcdefgh').must_be_nil end @@ -41,7 +41,7 @@ end describe 'highest_score_from' do - xit 'returns nil if no words were passed' do + it 'returns nil if no words were passed' do array_of_words = [] Scrabble::Scoring.highest_score_from(array_of_words).must_be_nil end @@ -52,8 +52,8 @@ end it 'returns the highest word if there are two words' do - array_of_words = ["zebraa", "zebras"] - Scrabble::Scoring.highest_score_from(array_of_words).must_equal(["zebraa", "zebras"]) + array_of_words = ["zebraa", "zebra"] + Scrabble::Scoring.highest_score_from(array_of_words).must_equal(["zebraa"]) end xit 'if tied, prefer a word with 7 letters' do From 5fbeeb439c12c25d49b0da286c97c21bd4e9f2cc Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Thu, 22 Feb 2018 18:04:37 -0800 Subject: [PATCH 16/28] if/elsif for tie --- lib/scoring.rb | 25 +++++++++++++++++++------ specs/scoring_spec.rb | 22 ++++++++++++++-------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 9f9b9ccd..9b1863de 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -12,11 +12,11 @@ def self.score(word) "Y"=>4, "Z"=>10 } @word = word.upcase - letters = @word.split("") + @letters = @word.split("") character_check = @all_letters.keys values_of_letters = [] - letters.each do |character| + @letters.each do |character| # Checking if character input is a valid letter or not # First part is splitting the hash into an array of the # key values 'A-Z' and then we are checking if any characters @@ -27,7 +27,7 @@ def self.score(word) # associated with the key and put that in an array of integers # - if !character_check.include?(character) || letters.length == 0 || letters.length > 7 + if !character_check.include?(character) || @letters.length == 0 || @letters.length > 7 return nil else values_of_letters << @all_letters.fetch(character) @@ -36,7 +36,7 @@ def self.score(word) sum = values_of_letters.sum - return letters.length == 7 ? sum + 50 : sum + return @letters.length == 7 ? sum + 50 : sum end @@ -47,17 +47,30 @@ def self.highest_score_from(array_of_words) return nil if array_of_words.length == 0 if array_of_words.length == 1 - return array_of_words + return array_of_words.first elsif array_of_words.length > 1 array_of_words.each do |word| all_scores[word] = score(word) + # scores << score(word) end end + # p all_scores.values.max + max_score_words = all_scores.delete_if { |word, score| score != all_scores.values.max }.keys - return max_score_words + if max_score_words.any? { |word| word.length == 7 } + winner = max_score_words.max_by(&:length) + return winner + elsif max_score_words.all? { |word| word.length == word.length } + winner = max_score_words[0] + return winner + else + winner = max_score_words.min_by(&:length) + return winner + + end end end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 8a9e6854..7d289e0b 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -15,7 +15,7 @@ Scrabble::Scoring.score('pig').must_equal 6 end - it 'adds 50 points for a 7-letter word' do + xit 'adds 50 points for a 7-letter word' do Scrabble::Scoring.score('academy').must_equal 65 end @@ -31,7 +31,7 @@ Scrabble::Scoring.score(' ').must_be_nil end - it 'returns nil for words > 7 letters' do + xit 'returns nil for words > 7 letters' do Scrabble::Scoring.score('abcdefgh').must_be_nil end @@ -48,21 +48,27 @@ it 'returns the only word in a length-1 array' do array_of_words = ['one'] - Scrabble::Scoring.highest_score_from(array_of_words).must_equal(['one']) + Scrabble::Scoring.highest_score_from(array_of_words).must_equal('one') end it 'returns the highest word if there are two words' do - array_of_words = ["zebraa", "zebra"] - Scrabble::Scoring.highest_score_from(array_of_words).must_equal(["zebraa"]) + array_of_words = ["zebra", "zebras"] + Scrabble::Scoring.highest_score_from(array_of_words).must_equal("zebras") end - xit 'if tied, prefer a word with 7 letters' do + it 'if tied, prefer a word with 7 letters' do + array_of_words = ["ww", "bananas"] + Scrabble::Scoring.highest_score_from(array_of_words).must_equal("bananas") end - xit 'if tied and no word has 7 letters, prefers the word with fewer letters' do + it 'if tied and no word has 7 letters, prefers the word with fewer letters' do + array_of_words = ["x", "brasas"] + Scrabble::Scoring.highest_score_from(array_of_words).must_equal("x") end - xit 'returns the first word of a tie with same letter count' do + it 'returns the first word of a tie with same letter count' do + array_of_words = ["zebraa", "zebras"] + Scrabble::Scoring.highest_score_from(array_of_words).must_equal("zebraa") end end end From 00a7a6fcf42235370c3ef68844dd804066b40a63 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Thu, 22 Feb 2018 18:16:12 -0800 Subject: [PATCH 17/28] wave 1 complete w/ edits --- lib/scoring.rb | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 9b1863de..507707e8 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -17,6 +17,7 @@ def self.score(word) values_of_letters = [] @letters.each do |character| + # Checking if character input is a valid letter or not # First part is splitting the hash into an array of the # key values 'A-Z' and then we are checking if any characters @@ -25,7 +26,7 @@ def self.score(word) # # Else, if all the characters are valid letters, take the value # associated with the key and put that in an array of integers - # + if !character_check.include?(character) || @letters.length == 0 || @letters.length > 7 return nil @@ -34,9 +35,9 @@ def self.score(word) end end - sum = values_of_letters.sum + score = values_of_letters.sum - return @letters.length == 7 ? sum + 50 : sum + return @letters.length == 7 ? score + 50 : score end @@ -52,26 +53,21 @@ def self.highest_score_from(array_of_words) elsif array_of_words.length > 1 array_of_words.each do |word| all_scores[word] = score(word) - # scores << score(word) end end - # p all_scores.values.max - max_score_words = all_scores.delete_if { |word, score| score != all_scores.values.max }.keys if max_score_words.any? { |word| word.length == 7 } - winner = max_score_words.max_by(&:length) - return winner + winning_word = max_score_words.max_by(&:length) elsif max_score_words.all? { |word| word.length == word.length } - winner = max_score_words[0] - return winner + winning_word = max_score_words[0] else - winner = max_score_words.min_by(&:length) - return winner - + winning_word = max_score_words.min_by(&:length) end + return winning_word + end end end From 0cd9e181b32aa6e37ec257c1c15dcbc26a5b7113 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Thu, 22 Feb 2018 18:25:52 -0800 Subject: [PATCH 18/28] edit wave 1 --- lib/scoring.rb | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 507707e8..2bba5575 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -26,12 +26,14 @@ def self.score(word) # # Else, if all the characters are valid letters, take the value # associated with the key and put that in an array of integers - + if !character_check.include?(character) || @letters.length == 0 || @letters.length > 7 return nil + else values_of_letters << @all_letters.fetch(character) + end end @@ -48,26 +50,31 @@ def self.highest_score_from(array_of_words) return nil if array_of_words.length == 0 if array_of_words.length == 1 - return array_of_words.first + winning_word = array_of_words.first elsif array_of_words.length > 1 array_of_words.each do |word| all_scores[word] = score(word) - end - end - max_score_words = all_scores.delete_if { |word, score| score != all_scores.values.max }.keys - if max_score_words.any? { |word| word.length == 7 } - winning_word = max_score_words.max_by(&:length) - elsif max_score_words.all? { |word| word.length == word.length } - winning_word = max_score_words[0] - else - winning_word = max_score_words.min_by(&:length) - end + max_score_words = all_scores.delete_if { |word, score| score != all_scores.values.max }.keys + + if max_score_words.any? { |word| word.length == 7 } + winning_word = max_score_words.max_by(&:length) - return winning_word + elsif max_score_words.all? { |word| word.length == word.length } + winning_word = max_score_words[0] + else + winning_word = max_score_words.min_by(&:length) + + end + end + return winning_word + + end end + + end end From ffc2370b577d1ac2c379b1677a94428eddba8017 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Thu, 22 Feb 2018 18:39:20 -0800 Subject: [PATCH 19/28] initiate payer.rb --- player.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 player.rb diff --git a/player.rb b/player.rb new file mode 100644 index 00000000..09034650 --- /dev/null +++ b/player.rb @@ -0,0 +1,30 @@ +require_relative "scoring" + +# The constructor for Scrabble::Player should take exactly one argument: the player's name. Instances of the class should respond to the following messages: +# +# #play(word): Adds the input word to the plays Array +# Returns false if player has already won +# Otherwise returns the score of the word + +# #total_score: Returns the sum of scores of played words +# #won?: If the player has over 100 points, returns true, otherwise returns false +# #highest_scoring_word: Returns the highest scoring played word +# #highest_word_score: Returns the highest_scoring_word score + +module Scrabble + class Player + + def initialize(name) + @name = name + # #name: returns the value of the @name instance variable + + @plays = [] + # #plays: returns an Array of the words played by the player + + @total_score = 0 + @tiles=[] + + end + + end +end From 80e950307e1fac545e662441af802674e5050ccf Mon Sep 17 00:00:00 2001 From: Brittany Jones Date: Thu, 22 Feb 2018 20:07:35 -0800 Subject: [PATCH 20/28] adding tilebag file, and spec.creating new class tilebag and initializing --- lib/scoring.rb | 2 +- lib/tilebag.rb | 12 ++++++++++++ specs/tilebag_spec.rb | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 lib/tilebag.rb create mode 100644 specs/tilebag_spec.rb diff --git a/lib/scoring.rb b/lib/scoring.rb index 507707e8..c4d55de9 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -26,7 +26,7 @@ def self.score(word) # # Else, if all the characters are valid letters, take the value # associated with the key and put that in an array of integers - + if !character_check.include?(character) || @letters.length == 0 || @letters.length > 7 return nil diff --git a/lib/tilebag.rb b/lib/tilebag.rb new file mode 100644 index 00000000..2c81c8f7 --- /dev/null +++ b/lib/tilebag.rb @@ -0,0 +1,12 @@ +module Scrabble + class TileBag +#player can see letters + attr_reader :letter_quantity + + +# Creating an instance of letter quantity to be used though class tilebag. + def initialize + @letter_quantity = {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} + end + end +end diff --git a/specs/tilebag_spec.rb b/specs/tilebag_spec.rb new file mode 100644 index 00000000..eb23f05e --- /dev/null +++ b/specs/tilebag_spec.rb @@ -0,0 +1,17 @@ +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' + +require_relative '../lib/tilebag' +#With this, i wont have to retype scrabble::tilebag.new so many times. +describe "tilebag" do + before do + @tile_bag = Scrabble::TileBag.new + end + + describe "tile bag can be created/initialized" do + it "creates instance of tilebag with a collection of all tiles" do + @tile_bag.must_be_instance_of Scrabble::TileBag + end + end +end From 8283fcffdaef168860ba999d577f8341cfc8884e Mon Sep 17 00:00:00 2001 From: Brittany Jones Date: Thu, 22 Feb 2018 20:51:54 -0800 Subject: [PATCH 21/28] added code for tilebag, and tile_bag spec. all tests passing. added player spec file --- player.rb => lib/player.rb | 0 lib/tilebag.rb | 45 +++++++++++++++++++++++++++++++++++--- specs/player_spec.rb | 1 + specs/tilebag_spec.rb | 30 ++++++++++++++++++++++++- 4 files changed, 72 insertions(+), 4 deletions(-) rename player.rb => lib/player.rb (100%) create mode 100644 specs/player_spec.rb diff --git a/player.rb b/lib/player.rb similarity index 100% rename from player.rb rename to lib/player.rb diff --git a/lib/tilebag.rb b/lib/tilebag.rb index 2c81c8f7..fd677373 100644 --- a/lib/tilebag.rb +++ b/lib/tilebag.rb @@ -1,12 +1,51 @@ module Scrabble class TileBag -#player can see letters + #player can see letters attr_reader :letter_quantity -# Creating an instance of letter quantity to be used though class tilebag. + # Creating an instance of letter quantity to be used though class tilebag. def initialize - @letter_quantity = {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} + @letters_bag_total = {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} end + + # Define tiles remaining method to be used in the method for drawing the tiles. + def tiles_remaining + # still not sure why or how this is working. TODO: may take out later + return @letters_bag_total.inject(0) { |sum, i| sum += i[1] } + end + # Define draw tiles, put the tiles into array. + def draw_tiles(num) + all_tiles = [] + # array will return tiles to player. Needs much refactoring. + if num > tiles_remaining + return nil + #to account for test, returns nil if more tiles are drawn than tiles remain. + else + while all_tiles.length != num + new_tile = rand(@letters_bag_total.size) + i = 0 + @letters_bag_total.each do |letter, total_tiles| + #Need to continue working on, this is becoming harder to read. TODO: REFACTOR! + # if the amount of tiles drawn(starting at 0) is the same as the amount of new tiles drawn, + if i == new_tile + #if the condition above, and the total tiles isnt 0, add the new tile (letter), to all of the tiles (all_tiles array) + if total_tiles != 0 + all_tiles << letter + #Then subtract the letter from the tilebag, reducing the total amount of tiles by 1, and reducing the letter by one specifically from the letters. + @letters_bag_total[letter] = total_tiles - 1 + else + new_tile = rand(@letters_bag_total.size) + end + end + #increases the amount of tiles had by player plus one, each time a tile is drawn + i += 1 + end + end + #returns array of all tiles to player + return all_tiles + end + end + end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb new file mode 100644 index 00000000..fb19c228 --- /dev/null +++ b/specs/player_spec.rb @@ -0,0 +1 @@ +#Player spec diff --git a/specs/tilebag_spec.rb b/specs/tilebag_spec.rb index eb23f05e..2e339761 100644 --- a/specs/tilebag_spec.rb +++ b/specs/tilebag_spec.rb @@ -6,7 +6,7 @@ #With this, i wont have to retype scrabble::tilebag.new so many times. describe "tilebag" do before do - @tile_bag = Scrabble::TileBag.new + @tile_bag = Scrabble::TileBag.new end describe "tile bag can be created/initialized" do @@ -14,4 +14,32 @@ @tile_bag.must_be_instance_of Scrabble::TileBag end end + #testing for edge cases of drawing tiles, like drawing more than are in the tilebag, or drawing no tiles. + describe "draw tiles" do + it "returns nil if input is more than tiles in tilebag" do + @tile_bag.draw_tiles(50000).must_be_nil true + end + #in between case + # Making sure drawn tiles will be returned to player + it "returns tiles to user in array" do + @tile_bag.draw_tiles(5).must_be_kind_of Array + end + # Testing for an edge case. + it "returns an empty array of tiles to user" do + @tile_bag.draw_tiles(0).must_be_empty Array + end + end + + describe "tiles left in tilebag" do + it "takes a tile away from total letter quantity and returns num of remaining + tiles in tilebag" do + @tile_bag.draw_tiles(7) + @tile_bag.tiles_remaining.must_equal 91 + end + it "returns the total letter quantity when no tile(s) is drawn" do + @tile_bag.draw_tiles(0) + @tile_bag.tiles_remaining.must_equal 98 + # Wont need to test for edge case if all the tiles are drawn with return of 0, because we already did this in previous draw tile test. + end +end end From 3c29292d59428b987334e36f5b2f179266046fee Mon Sep 17 00:00:00 2001 From: Brittany Jones Date: Thu, 22 Feb 2018 21:50:10 -0800 Subject: [PATCH 22/28] starting player spec, initialized passes test. --- lib/player.rb | 24 +++++++++++++----------- specs/player_spec.rb | 20 +++++++++++++++++++- specs/tilebag_spec.rb | 4 ---- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 09034650..78ac5a61 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -10,21 +10,23 @@ # #won?: If the player has over 100 points, returns true, otherwise returns false # #highest_scoring_word: Returns the highest scoring played word # #highest_word_score: Returns the highest_scoring_word score +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' module Scrabble class Player - def initialize(name) - @name = name - # #name: returns the value of the @name instance variable - - @plays = [] - # #plays: returns an Array of the words played by the player - - @total_score = 0 - @tiles=[] - - end + attr_accessor :name, :plays, :total_score, :tiles + def initialize(name) + @name = name + # #name: returns the value of the @name instance variable + @plays = [] + # #plays: returns an Array of the words played by the player + @total_score = 0 + @tiles=[] + + end end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index fb19c228..3b92f497 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -1 +1,19 @@ -#Player spec +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' + +require_relative '../lib/player' + +describe 'player' do + before do + @player = Scrabble::Player.new("Maddie") + end + + describe "initialize" do + + it "must be an instance of player class" do + @player.must_be_instance_of Scrabble::Player + end + + end +end diff --git a/specs/tilebag_spec.rb b/specs/tilebag_spec.rb index 2e339761..635a61c3 100644 --- a/specs/tilebag_spec.rb +++ b/specs/tilebag_spec.rb @@ -1,7 +1,3 @@ -require 'minitest/autorun' -require 'minitest/reporters' -require 'minitest/skip_dsl' - require_relative '../lib/tilebag' #With this, i wont have to retype scrabble::tilebag.new so many times. describe "tilebag" do From 27299a0b391ae4844cbe3cb0fca1ee7805d6275f Mon Sep 17 00:00:00 2001 From: Brittany Jones Date: Thu, 22 Feb 2018 21:55:34 -0800 Subject: [PATCH 23/28] #name: returns the value of the @name instance variable --- specs/player_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 3b92f497..136b7714 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -15,5 +15,9 @@ @player.must_be_instance_of Scrabble::Player end + it "must returns the value of the @name instance variable" do + @player.name.must_equal "Maddie" + end + end end From da40316de8830d4fb4ddfff75f9b2e492a156acb Mon Sep 17 00:00:00 2001 From: Brittany Jones Date: Thu, 22 Feb 2018 22:18:51 -0800 Subject: [PATCH 24/28] returns array of words played by player, tests passing --- lib/player.rb | 20 +++++++++++++++++++- specs/player_spec.rb | 9 +++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/player.rb b/lib/player.rb index 78ac5a61..05c8d207 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -12,7 +12,7 @@ # #highest_word_score: Returns the highest_scoring_word score require 'minitest/autorun' require 'minitest/reporters' -require 'minitest/skip_dsl' + module Scrabble class Player @@ -28,5 +28,23 @@ def initialize(name) @tiles=[] end + + def winner + return @total_score > 100 + end + + def play(word) + if winner == false + @plays << word + @total_score += Scoring.score(word) + else + return false + end + end + + + + + end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 136b7714..20f137ad 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -19,5 +19,14 @@ @player.name.must_equal "Maddie" end + describe "plays" do +#TODO: Need to test for 100 points.. + it "must return an array" do + @player.plays.must_be_kind_of Array + end +end + + + end end From 662720460ee1cd322f2192d9bd26b45a84e57314 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Fri, 23 Feb 2018 10:27:35 -0800 Subject: [PATCH 25/28] CONSTANT VARIABLE - for letter --- lib/scoring.rb | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 2bba5575..8b002ce3 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,22 +1,25 @@ + +ALL_LETTERS = { + "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 +} + module Scrabble class Scoring def self.score(word) - @all_letters = { - "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 = word.upcase - @letters = @word.split("") - character_check = @all_letters.keys + + word = word.upcase + letters = word.split("") + character_check = ALL_LETTERS.keys values_of_letters = [] - @letters.each do |character| + letters.each do |character| # Checking if character input is a valid letter or not # First part is splitting the hash into an array of the @@ -28,18 +31,18 @@ def self.score(word) # associated with the key and put that in an array of integers - if !character_check.include?(character) || @letters.length == 0 || @letters.length > 7 + if !character_check.include?(character) || letters.length == 0 || letters.length > 7 return nil else - values_of_letters << @all_letters.fetch(character) + values_of_letters << ALL_LETTERS.fetch(character) end end score = values_of_letters.sum - return @letters.length == 7 ? score + 50 : score + return letters.length == 7 ? score + 50 : score end @@ -56,7 +59,6 @@ def self.highest_score_from(array_of_words) array_of_words.each do |word| all_scores[word] = score(word) - max_score_words = all_scores.delete_if { |word, score| score != all_scores.values.max }.keys if max_score_words.any? { |word| word.length == 7 } @@ -70,11 +72,10 @@ def self.highest_score_from(array_of_words) end end + return winning_word end end - - end end From de18f720aee7da250fc560a3317ffaa1f6ec8252 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Fri, 23 Feb 2018 10:34:06 -0800 Subject: [PATCH 26/28] Refactoring --- lib/tilebag.rb | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/lib/tilebag.rb b/lib/tilebag.rb index fd677373..5df27b1a 100644 --- a/lib/tilebag.rb +++ b/lib/tilebag.rb @@ -18,33 +18,30 @@ def tiles_remaining def draw_tiles(num) all_tiles = [] # array will return tiles to player. Needs much refactoring. - if num > tiles_remaining - return nil - #to account for test, returns nil if more tiles are drawn than tiles remain. - else - while all_tiles.length != num - new_tile = rand(@letters_bag_total.size) - i = 0 - @letters_bag_total.each do |letter, total_tiles| - #Need to continue working on, this is becoming harder to read. TODO: REFACTOR! - # if the amount of tiles drawn(starting at 0) is the same as the amount of new tiles drawn, - if i == new_tile - #if the condition above, and the total tiles isnt 0, add the new tile (letter), to all of the tiles (all_tiles array) - if total_tiles != 0 - all_tiles << letter - #Then subtract the letter from the tilebag, reducing the total amount of tiles by 1, and reducing the letter by one specifically from the letters. - @letters_bag_total[letter] = total_tiles - 1 - else - new_tile = rand(@letters_bag_total.size) - end + return nil if num > tiles_remaining + #to account for test, returns nil if more tiles are drawn than tiles remain. + while all_tiles.length != num + new_tile = rand(@letters_bag_total.size) + i = 0 + @letters_bag_total.each do |letter, total_tiles| + #Need to continue working on, this is becoming harder to read. TODO: REFACTOR! + # if the amount of tiles drawn(starting at 0) is the same as the amount of new tiles drawn, + if i == new_tile + #if the condition above, and the total tiles isnt 0, add the new tile (letter), to all of the tiles (all_tiles array) + if total_tiles != 0 + all_tiles << letter + #Then subtract the letter from the tilebag, reducing the total amount of tiles by 1, and reducing the letter by one specifically from the letters. + @letters_bag_total[letter] = total_tiles - 1 + else + new_tile = rand(@letters_bag_total.size) end - #increases the amount of tiles had by player plus one, each time a tile is drawn - i += 1 end + #increases the amount of tiles had by player plus one, each time a tile is drawn + i += 1 end - #returns array of all tiles to player - return all_tiles end + #returns array of all tiles to player + return all_tiles end end From 6d9183b09e1d5cfe2851b3ff665f3fdaddb94cf4 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Fri, 23 Feb 2018 10:55:32 -0800 Subject: [PATCH 27/28] change variable names --- lib/scoring.rb | 6 +++--- lib/tilebag.rb | 22 +++++++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 8b002ce3..462669e3 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,5 +1,5 @@ -ALL_LETTERS = { +LETTER_VALUES = { "A"=>1, "B"=>3, "C"=>3, "D"=>2, "E"=>1, "F"=>4, "G"=>2, "H"=>4, "I"=>1, "J"=>8, "K"=>5, "L"=>1, @@ -16,7 +16,7 @@ def self.score(word) word = word.upcase letters = word.split("") - character_check = ALL_LETTERS.keys + character_check = LETTER_VALUES.keys values_of_letters = [] letters.each do |character| @@ -35,7 +35,7 @@ def self.score(word) return nil else - values_of_letters << ALL_LETTERS.fetch(character) + values_of_letters << LETTER_VALUES.fetch(character) end end diff --git a/lib/tilebag.rb b/lib/tilebag.rb index 5df27b1a..b14fb1d0 100644 --- a/lib/tilebag.rb +++ b/lib/tilebag.rb @@ -12,24 +12,28 @@ def initialize # Define tiles remaining method to be used in the method for drawing the tiles. def tiles_remaining # still not sure why or how this is working. TODO: may take out later - return @letters_bag_total.inject(0) { |sum, i| sum += i[1] } + remainder = @letters_bag_total.inject(0) { |sum, i| sum += i[1] } + p remainder + + return remainder end # Define draw tiles, put the tiles into array. def draw_tiles(num) - all_tiles = [] + player_tiles = [] # array will return tiles to player. Needs much refactoring. return nil if num > tiles_remaining #to account for test, returns nil if more tiles are drawn than tiles remain. - while all_tiles.length != num + while player_tiles.length != num new_tile = rand(@letters_bag_total.size) - i = 0 + starting_hand = 0 + @letters_bag_total.each do |letter, total_tiles| #Need to continue working on, this is becoming harder to read. TODO: REFACTOR! # if the amount of tiles drawn(starting at 0) is the same as the amount of new tiles drawn, - if i == new_tile - #if the condition above, and the total tiles isnt 0, add the new tile (letter), to all of the tiles (all_tiles array) + if starting_hand == new_tile + #if the condition above, and the total tiles isnt 0, add the new tile (letter), to all of the tiles (player_tiles array) if total_tiles != 0 - all_tiles << letter + player_tiles << letter #Then subtract the letter from the tilebag, reducing the total amount of tiles by 1, and reducing the letter by one specifically from the letters. @letters_bag_total[letter] = total_tiles - 1 else @@ -37,11 +41,11 @@ def draw_tiles(num) end end #increases the amount of tiles had by player plus one, each time a tile is drawn - i += 1 + starting_hand += 1 end end #returns array of all tiles to player - return all_tiles + return player_tiles end end From 1a85d979b69c6af8d610ec89184c0b64dcba65f9 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Fri, 23 Feb 2018 14:19:15 -0800 Subject: [PATCH 28/28] Change var names/refactoring --- lib/tilebag.rb | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/tilebag.rb b/lib/tilebag.rb index b14fb1d0..9bca2b70 100644 --- a/lib/tilebag.rb +++ b/lib/tilebag.rb @@ -6,46 +6,46 @@ class TileBag # Creating an instance of letter quantity to be used though class tilebag. def initialize - @letters_bag_total = {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} + @default_set = {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} end # Define tiles remaining method to be used in the method for drawing the tiles. def tiles_remaining # still not sure why or how this is working. TODO: may take out later - remainder = @letters_bag_total.inject(0) { |sum, i| sum += i[1] } - p remainder - - return remainder + total_num_tiles = @default_set.inject(0) { |letter, letter_quantity| letter += letter_quantity[1] } + p "++++TEST++++" + p total_num_tiles + return total_num_tiles end # Define draw tiles, put the tiles into array. def draw_tiles(num) - player_tiles = [] + player_hand = [] # array will return tiles to player. Needs much refactoring. return nil if num > tiles_remaining #to account for test, returns nil if more tiles are drawn than tiles remain. - while player_tiles.length != num - new_tile = rand(@letters_bag_total.size) + while player_hand.length != num + new_tile = rand(@default_set.size) starting_hand = 0 - @letters_bag_total.each do |letter, total_tiles| + @default_set.each do |letter, letter_quantity| #Need to continue working on, this is becoming harder to read. TODO: REFACTOR! # if the amount of tiles drawn(starting at 0) is the same as the amount of new tiles drawn, - if starting_hand == new_tile - #if the condition above, and the total tiles isnt 0, add the new tile (letter), to all of the tiles (player_tiles array) - if total_tiles != 0 - player_tiles << letter + if starting_hand == new_tile && letter_quantity != 0 + #if the condition above, and the total tiles isnt 0, add the new tile (letter), to all of the tiles (player_hand array) + # if letter_quantity != 0 + player_hand << letter #Then subtract the letter from the tilebag, reducing the total amount of tiles by 1, and reducing the letter by one specifically from the letters. - @letters_bag_total[letter] = total_tiles - 1 - else - new_tile = rand(@letters_bag_total.size) - end + @default_set[letter] = letter_quantity - 1 + else + new_tile = rand(@default_set.size) + end #increases the amount of tiles had by player plus one, each time a tile is drawn starting_hand += 1 end end #returns array of all tiles to player - return player_tiles + return player_hand end end