From 27059d0ddc10ee1d17f678aff8c2e8e57432e263 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Tue, 20 Feb 2018 14:27:37 -0800 Subject: [PATCH 01/40] Added scoring method and fixed typo on the 'game' files - First score test OK --- lib/scoring.rb | 34 ++++++++++++++++++++++++++++++++-- specs/scoring_spec.rb | 12 ++++++------ wave-1-game.rb | 2 +- wave-2-game.rb | 2 +- wave-3-game.rb | 2 +- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index fb3a3f2d..3f374be1 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,9 +1,39 @@ + module Scrabble class Scoring + def self.score(word) + + # TODO: fix it, better way to evaluate, with arrays. + letters_array = word.upcase.split("") + + points = 0 + letters_array.each do |letter| + + if letter == "Q" || letter == "Z" + points += 10 + elsif letter == "D" || letter == "G" + points += 2 + elsif letter =="B" || letter == "C" || letter =="M" || letter == "P" + points += 3 + elsif letter == "F" || letter == "H" || letter == "V" || letter == "W" || letter == "Y" + points += 4 + elsif letter == "K" + points += 5 + elsif letter == "J" || letter == "X" + points += 8 + else #"A", "E", "I", "O", "U", "L", "N", "R", "S", "T" + points += 1 + end + end + return points end + def self.highest_score_from(array_of_words) end - end -end + end #Scoring +end #Scrabble + + +Scrabble::Scoring.score("dog") diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index ab498929..27279d69 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -15,32 +15,32 @@ 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 + xdescribe 'highest_score_from' do it 'returns nil if no words were passed' do end 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 diff --git a/wave-2-game.rb b/wave-2-game.rb index 249368ae..2c7c1ead 100644 --- a/wave-2-game.rb +++ b/wave-2-game.rb @@ -84,7 +84,7 @@ def crown_winner(player) 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 for all players is #{ highest_word }" @players.each do |player| diff --git a/wave-3-game.rb b/wave-3-game.rb index 12821315..f0917b92 100644 --- a/wave-3-game.rb +++ b/wave-3-game.rb @@ -90,7 +90,7 @@ def crown_winner(player) 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 for all players is #{ highest_word }" @players.each do |player| From ad5a0be38a89764cd0c46bd836f403f2479140cd Mon Sep 17 00:00:00 2001 From: Jackie Date: Tue, 20 Feb 2018 14:48:04 -0800 Subject: [PATCH 02/40] adds 50 pts to 7 letter word, returns nil for bad characters --- lib/scoring.rb | 12 ++++++++++-- specs/scoring_spec.rb | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 3f374be1..91dc4d91 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -5,6 +5,11 @@ class Scoring def self.score(word) # TODO: fix it, better way to evaluate, with arrays. + + if word.match?(/\W/) + return nil + end + letters_array = word.upcase.split("") points = 0 @@ -26,7 +31,10 @@ def self.score(word) points += 1 end end - return points + + letters_array.length >= 7 ? points += 50 : points += 0 + + return points end @@ -36,4 +44,4 @@ def self.highest_score_from(array_of_words) end #Scrabble -Scrabble::Scoring.score("dog") +# Scrabble::Scoring.score("dog") diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 27279d69..94923939 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -15,17 +15,17 @@ 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 - xit 'handles all upper- and lower-case letters' do + it '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 - 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 24022ff6a87eabbef32ce2ebe9ff6cff3ac95ff8 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Tue, 20 Feb 2018 14:48:52 -0800 Subject: [PATCH 03/40] .. --- lib/scoring.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 3f374be1..bda0d6fc 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -35,5 +35,5 @@ def self.highest_score_from(array_of_words) end #Scoring end #Scrabble - -Scrabble::Scoring.score("dog") +# Personal tests: +# Scrabble::Scoring.score("dog") From 1b70781d208d6f838f18f2d65cd936d7c30ac0ef Mon Sep 17 00:00:00 2001 From: Jackie Date: Tue, 20 Feb 2018 14:59:11 -0800 Subject: [PATCH 04/40] returns nil for empty words --- lib/scoring.rb | 4 ++++ specs/scoring_spec.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index af4a5bd2..29eed286 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -16,6 +16,10 @@ def self.score(word) letters_array = word.upcase.split("") + if letters_array.empty? + return nil + end + points = 0 letters_array.each do |letter| diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 77f8c8d2..1afaa6ee 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -35,7 +35,7 @@ Scrabble::Scoring.score('abcdefgh').must_be_nil end - xit 'returns nil for empty words' do + it 'returns nil for empty words' do Scrabble::Scoring.score('').must_be_nil end end From f89b00ab1cb20fdac8aefc980f4af719f7e64a3f Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Tue, 20 Feb 2018 15:05:16 -0800 Subject: [PATCH 05/40] added code for 'returns nil if no words were passed' - OK --- lib/scoring.rb | 6 +++++- specs/scoring_spec.rb | 13 +++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 29eed286..1b333232 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -48,7 +48,11 @@ def self.score(word) end - def self.highest_score_from(array_of_words) + def self.highest_score_from(array_of_words = "") + if array_of_words == "" + return nil + end + end end #Scoring end #Scrabble diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 1afaa6ee..b9020bae 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -40,23 +40,24 @@ end end - xdescribe 'highest_score_from' do + describe 'highest_score_from' do it 'returns nil if no words were passed' do + Scrabble::Scoring.highest_score_from().must_be_nil 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 + xit '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 e112d21b3693ce2ec2da1d48f9d576c9e65ece1d Mon Sep 17 00:00:00 2001 From: Jackie Date: Tue, 20 Feb 2018 15:23:28 -0800 Subject: [PATCH 06/40] returns the only word in a length-1 array --- lib/scoring.rb | 11 +++++++++-- specs/scoring_spec.rb | 7 ++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 1b333232..7f047223 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -48,11 +48,18 @@ def self.score(word) end - def self.highest_score_from(array_of_words = "") - if array_of_words == "" + def self.highest_score_from(array_of_words = []) + if array_of_words == [] return nil end + winning_words = [] + + if array_of_words.length == 1 + winning_words << array_of_words + return winning_words[0] + end + end end #Scoring end #Scrabble diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index b9020bae..bfcd21fb 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -45,7 +45,12 @@ Scrabble::Scoring.highest_score_from().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 + winning_word = Scrabble::Scoring.highest_score_from(["hotdog"]) + winning_word[0].must_equal "hotdog" + winning_word.must_be_kind_of Array + winning_word.length.must_equal 1 + end xit 'returns the highest word if there are two words' do From 78c157ba64124dab89f110ac066b0e926607c70c Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Tue, 20 Feb 2018 15:36:18 -0800 Subject: [PATCH 07/40] returns the highest word if there are two words - OK --- lib/scoring.rb | 15 ++++++++++++++- specs/scoring_spec.rb | 4 +++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 7f047223..deb5dfc9 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -4,6 +4,7 @@ class Scoring def self.score(word) + # TODO: fix it, better way to evaluate, with arrays. if word.match?(/\W/) @@ -54,13 +55,25 @@ def self.highest_score_from(array_of_words = []) end winning_words = [] + max_score = 0 + winning_word = "" if array_of_words.length == 1 winning_words << array_of_words return winning_words[0] + else + array_of_words.each do |word| + if score(word) > max_score + max_score = score(word) + winning_word = word + end + end + return winning_word end - end + + + end #Scoring end #Scrabble diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index bfcd21fb..ecf00161 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -53,7 +53,9 @@ end - xit 'returns the highest word if there are two words' do + it 'returns the highest word if there are two words' do + winning_words = Scrabble::Scoring.highest_score_from(["dog", "academy"]) + winning_words.must_equal "academy" end xit 'if tied, prefer a word with 7 letters' do From 68481b7def98245fd752299f8bbc3371509acacd Mon Sep 17 00:00:00 2001 From: Jackie Date: Tue, 20 Feb 2018 15:53:55 -0800 Subject: [PATCH 08/40] if tied, prefer 7 letter word --- lib/scoring.rb | 15 +++++++++++---- specs/scoring_spec.rb | 5 ++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index deb5dfc9..b9903f71 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -56,20 +56,27 @@ def self.highest_score_from(array_of_words = []) winning_words = [] max_score = 0 - winning_word = "" + if array_of_words.length == 1 winning_words << array_of_words return winning_words[0] else array_of_words.each do |word| - if score(word) > max_score + if score(word) > max_score || score(word) == max_score max_score = score(word) - winning_word = word + winning_words << word end end - return winning_word end + + if winning_words.count >= 2 + winner = winning_words.max_by(&:length) + return winner + else + return winning_words[0] + end + end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index ecf00161..3ce3c207 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -58,7 +58,10 @@ winning_words.must_equal "academy" end - xit 'if tied, prefer a word with 7 letters' do + it 'if tied, prefer a word with 7 letters' do + winning_words = Scrabble::Scoring.highest_score_from(["qqqqqj", "aaaaaad"]) + winning_words.must_equal "aaaaaad" + end xit 'if tied and no word has 7 letters, prefers the word with fewer letters' do From 27b48277cad007602ea8e70bdb37bea094ffc959 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Tue, 20 Feb 2018 16:36:32 -0800 Subject: [PATCH 09/40] if tied and no word has 7 letters, prefers the word with fewer letters --- lib/scoring.rb | 35 +++++++++++++---------------------- specs/scoring_spec.rb | 4 +++- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index b9903f71..f8224e65 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -50,39 +50,30 @@ def self.score(word) def self.highest_score_from(array_of_words = []) - if array_of_words == [] - return nil - end + # Create & Populate array of words: winning_words = [] - max_score = 0 - - - if array_of_words.length == 1 + if array_of_words == [] + return nil + elsif array_of_words.length == 1 winning_words << array_of_words return winning_words[0] + elsif array_of_words.max_by(&:length).length == 7 + winner = array_of_words.max_by(&:length) + return winner else - array_of_words.each do |word| - if score(word) > max_score || score(word) == max_score - max_score = score(word) - winning_words << word - end - end - end - - if winning_words.count >= 2 - winner = winning_words.max_by(&:length) + winner = array_of_words.min_by {|word| word.length} return winner - else - return winning_words[0] end - end - end #Scoring end #Scrabble # Personal tests: -# Scrabble::Scoring.score("dog") +# puts Scrabble::Scoring.score("qq") +# puts Scrabble::Scoring.score("kkkk") +# puts Scrabble::Scoring.score("aaaaaad") + +# puts Scrabble::Scoring.highest_score_from(["qq", "kkkk"]) diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 3ce3c207..0c45b5e9 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -64,7 +64,9 @@ 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 + winning_words = Scrabble::Scoring.highest_score_from(["qq", "kkkk"]) + winning_words.must_equal "qq" end xit 'returns the first word of a tie with same letter count' do From 5d1ddef92558936207080e1ef55253c8ec4abbe8 Mon Sep 17 00:00:00 2001 From: Jackie Date: Tue, 20 Feb 2018 16:54:38 -0800 Subject: [PATCH 10/40] returns the first word of a tie with same letter count --- specs/scoring_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 0c45b5e9..5cd6304b 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -69,7 +69,11 @@ winning_words.must_equal "qq" 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 + winning_words = Scrabble::Scoring.highest_score_from(["dd", "ba"]) + winning_words.must_equal "dd" + + end end end From f7dab825f62521cc332c19a71f43d87828f3c4ee Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Tue, 20 Feb 2018 16:55:29 -0800 Subject: [PATCH 11/40] .. --- lib/scoring.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index f8224e65..25f8ddf1 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -43,8 +43,6 @@ def self.score(word) letters_array.length == 7 ? points += 50 : points += 0 - - return points end @@ -72,8 +70,8 @@ def self.highest_score_from(array_of_words = []) end #Scrabble # Personal tests: -# puts Scrabble::Scoring.score("qq") -# puts Scrabble::Scoring.score("kkkk") -# puts Scrabble::Scoring.score("aaaaaad") - -# puts Scrabble::Scoring.highest_score_from(["qq", "kkkk"]) +# puts Scrabble::Scoring.score("dd") +# puts Scrabble::Scoring.score("ba") +# # puts Scrabble::Scoring.score("aaaaaad") +# +# puts Scrabble::Scoring.highest_score_from(["dd", "ba"]) From f3b149faa07147561e8fb00f664d56459642a46d Mon Sep 17 00:00:00 2001 From: Jackie Date: Wed, 21 Feb 2018 14:04:51 -0800 Subject: [PATCH 12/40] Initializes Player class and passes initialize test --- lib/player.rb | 16 ++++++++++++++++ specs/player_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 lib/player.rb create mode 100644 specs/player_spec.rb diff --git a/lib/player.rb b/lib/player.rb new file mode 100644 index 00000000..8e489302 --- /dev/null +++ b/lib/player.rb @@ -0,0 +1,16 @@ + +module Scrabble + + class Player + + attr_reader :name + + def initialize(name) + @name = name + end + + + end + + +end diff --git a/specs/player_spec.rb b/specs/player_spec.rb new file mode 100644 index 00000000..2be63e0a --- /dev/null +++ b/specs/player_spec.rb @@ -0,0 +1,24 @@ +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' + +require_relative '../lib/player' + +Minitest::Reporters.use! +Minitest::Reporters::SpecReporter.new + +describe 'Player' do +describe "#initialize" do + it "Takes a Player name" do + player_1 = Scrabble::Player.new("Patrick") + player_1.must_be_instance_of Scrabble::Player + + player_1.must_respond_to :name + player_1.name.must_equal "Patrick" + player_1.name.must_be_kind_of String + + end + +end + +end From fa5f61a21166598c2d3c56d7e55e01325e4ba86a Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Wed, 21 Feb 2018 14:20:12 -0800 Subject: [PATCH 13/40] 'Added a method 'plays' and passed the test 'Returns an array of words played' --- lib/player.rb | 6 ++++-- specs/player_spec.rb | 29 ++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 8e489302..a16ab31f 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -7,10 +7,12 @@ class Player def initialize(name) @name = name + @plays = [] end + def plays + return @plays + end end - - end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 2be63e0a..a9db77fa 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -8,17 +8,28 @@ Minitest::Reporters::SpecReporter.new describe 'Player' do -describe "#initialize" do - it "Takes a Player name" do - player_1 = Scrabble::Player.new("Patrick") - player_1.must_be_instance_of Scrabble::Player + describe "#initialize" do + it "Takes a Player name" do + player_1 = Scrabble::Player.new("Patrick") + player_1.must_be_instance_of Scrabble::Player - player_1.must_respond_to :name - player_1.name.must_equal "Patrick" - player_1.name.must_be_kind_of String + player_1.must_respond_to :name + player_1.name.must_equal "Patrick" + player_1.name.must_be_kind_of String - end + end + describe "#plays" do + it "Returns an array of words played:" do -end + player_1 = Scrabble::Player.new("Patrick") + player_1.plays.must_be_kind_of Array + + end + end + + + + + end end From 0a34fec642c757dd53a89846c273912a0d3edd28 Mon Sep 17 00:00:00 2001 From: Jackie Date: Wed, 21 Feb 2018 14:31:31 -0800 Subject: [PATCH 14/40] Adds input word to @plays array. --- lib/player.rb | 6 +++++- specs/player_spec.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/player.rb b/lib/player.rb index a16ab31f..ed6f39cf 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -3,7 +3,7 @@ module Scrabble class Player - attr_reader :name + attr_reader :name, :plays def initialize(name) @name = name @@ -14,5 +14,9 @@ def plays return @plays end + def play(word) + @plays << word + end + end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index a9db77fa..aed04ea2 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -23,7 +23,19 @@ player_1 = Scrabble::Player.new("Patrick") player_1.plays.must_be_kind_of Array + end + end + + describe "#play(word)" do + it "Adds input word to @plays array." do + + player_1 = Scrabble::Player.new("Patrick") + player_1.play("orange") + + player_1.plays.must_include "orange" + + end end From 8263c3f51e7f641e3963b97a6571aa0cdba834fa Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Wed, 21 Feb 2018 14:47:13 -0800 Subject: [PATCH 15/40] added total_score method and passed test Returns the sum of scores of played words. --- lib/player.rb | 10 ++++++++++ lib/scoring.rb | 2 +- specs/player_spec.rb | 11 +++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index ed6f39cf..8a810eff 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,3 +1,4 @@ +require_relative 'scoring' module Scrabble @@ -18,5 +19,14 @@ def play(word) @plays << word end + def total_score + sum_of_scores = 0 + @plays.each do |word| + score = Scrabble::Scoring.score(word) + sum_of_scores += score + end + return sum_of_scores + end + end end diff --git a/lib/scoring.rb b/lib/scoring.rb index 25f8ddf1..f809a4bb 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -72,6 +72,6 @@ def self.highest_score_from(array_of_words = []) # Personal tests: # puts Scrabble::Scoring.score("dd") # puts Scrabble::Scoring.score("ba") -# # puts Scrabble::Scoring.score("aaaaaad") +# puts Scrabble::Scoring.score("apples") # # puts Scrabble::Scoring.highest_score_from(["dd", "ba"]) diff --git a/specs/player_spec.rb b/specs/player_spec.rb index aed04ea2..f5bd9496 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -22,6 +22,7 @@ it "Returns an array of words played:" do player_1 = Scrabble::Player.new("Patrick") + player_1.plays.must_be_kind_of Array end end @@ -34,13 +35,19 @@ player_1.plays.must_include "orange" - - end end + describe '#total_score' do + it " Returns the sum of scores of played words" do + player_1 = Scrabble::Player.new("Patrick") + player_1.play("apples") + player_1.play("fuzzy") + player_1.total_score.must_equal 39 + end + end end From 6a082a86b4027c36de7a13209d7632032e3746b8 Mon Sep 17 00:00:00 2001 From: Jackie Date: Wed, 21 Feb 2018 15:20:14 -0800 Subject: [PATCH 16/40] won? returns true if player has over 100 pts --- lib/player.rb | 15 +++++++++++++++ lib/scoring.rb | 9 ++------- specs/player_spec.rb | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 8a810eff..60dac960 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -28,5 +28,20 @@ def total_score return sum_of_scores end + def won? + if total_score > 100 + return true + else + return false + end + + end + end end + +# player_1 = Scrabble::Player.new("Patrick") +# player_1.play("xxxxxxx") +# # # player_1.won?.must_equal true +# # puts player_1.total_score +# puts player_1.won? diff --git a/lib/scoring.rb b/lib/scoring.rb index f809a4bb..3339b570 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -43,6 +43,8 @@ def self.score(word) letters_array.length == 7 ? points += 50 : points += 0 + + return points end @@ -68,10 +70,3 @@ def self.highest_score_from(array_of_words = []) end #Scoring end #Scrabble - -# Personal tests: -# puts Scrabble::Scoring.score("dd") -# puts Scrabble::Scoring.score("ba") -# puts Scrabble::Scoring.score("apples") -# -# puts Scrabble::Scoring.highest_score_from(["dd", "ba"]) diff --git a/specs/player_spec.rb b/specs/player_spec.rb index f5bd9496..389afeef 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -49,6 +49,25 @@ end end + describe '#won?' do + it "Returns true if player has over 100 points" do + player_1 = Scrabble::Player.new("Patrick") + player_1.play("xxxxxxx") + player_1.won?.must_equal true + + end + + it "Returns false if player has over 100 points" do + player_1 = Scrabble::Player.new("Patrick") + player_1.play("apples") + player_1.play("fuzzy") + player_1.won?.must_equal false + + end + + + end + end end From 27db3907fabc63978a92f13692492c60bb1911a1 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Wed, 21 Feb 2018 15:22:03 -0800 Subject: [PATCH 17/40] mmm needed this commit/dont understand why --- lib/scoring.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index f809a4bb..fc5c600f 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -72,6 +72,6 @@ def self.highest_score_from(array_of_words = []) # Personal tests: # puts Scrabble::Scoring.score("dd") # puts Scrabble::Scoring.score("ba") -# puts Scrabble::Scoring.score("apples") +# puts Scrabble::Scoring.score("xxxxxxx") # # puts Scrabble::Scoring.highest_score_from(["dd", "ba"]) From bf0bc47cd7bc1ae90c9c75f924ffd97a4f23f4da Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Wed, 21 Feb 2018 16:20:32 -0800 Subject: [PATCH 18/40] fixed indentention --- specs/player_spec.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 44f30295..3afab657 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -27,8 +27,8 @@ end end - describe "#play(word)" do - it "Adds input word to @plays array." do + describe "#play(word)" do + it "Adds input word to @plays array." do player_1 = Scrabble::Player.new("Patrick") player_1.play("orange") @@ -65,17 +65,17 @@ end -describe '#highest_scoring_word' do - it 'Returns the highest scoring played word' do - player_1 = Scrabble::Player.new("Patrick") - player_1.play("cat") - player_1.play("pig") + describe '#highest_scoring_word' do + it 'Returns the highest scoring played word' do + player_1 = Scrabble::Player.new("Patrick") + player_1.play("cat") + player_1.play("pig") - player_1.highest_scoring_word.must_equal "pig" - player_1.highest_scoring_word.must_be_kind_of String - - end -end + player_1.highest_scoring_word.must_equal "pig" + player_1.highest_scoring_word.must_be_kind_of String + + end + end end From 4abaebadec991243b8352c1aea73a906e5063a8a Mon Sep 17 00:00:00 2001 From: Jackie Date: Wed, 21 Feb 2018 16:41:29 -0800 Subject: [PATCH 19/40] returns score of highest scoring word --- lib/player.rb | 11 +++++++---- specs/player_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 410673b6..0875172f 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -10,6 +10,7 @@ class Player def initialize(name) @name = name @plays = [] + @scoring_table = {} end def plays @@ -41,15 +42,17 @@ def won? def highest_scoring_word - hash = {} @plays.each do |word| score = Scrabble::Scoring.score(word) - hash["#{word}"] = score + @scoring_table["#{word}"] = score end - winning_word_player = hash.key(hash.values.max) - puts hash + winning_word_player = @scoring_table.key(@scoring_table.values.max) return winning_word_player end + def highest_word_score + highest_scoring_word + return @scoring_table.values.max + end end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 3afab657..25901c12 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -75,8 +75,29 @@ player_1.highest_scoring_word.must_be_kind_of String end + + + it 'Returns the highest scoring played word within tie' do + player_1 = Scrabble::Player.new("Patrick") + player_1.play("ba") + player_1.play("dd") + + player_1.highest_scoring_word.must_equal "ba" + end + end + describe '#highest_word_score' do + it 'Returns the score of the highest scoring word' do + player_1 = Scrabble::Player.new("Patrick") + player_1.play("cat") + player_1.play("pig") + + player_1.highest_word_score.must_equal 6 + + end + end + end From b143409c030b9803892986814c4538f39d93f4f7 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Thu, 22 Feb 2018 15:15:17 -0800 Subject: [PATCH 20/40] Created TileBag class and files, initialized the TileBag class and passed the initialize test for it. --- lib/tile_bag.rb | 12 ++++++++++++ specs/tile_bag_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 lib/tile_bag.rb create mode 100644 specs/tile_bag_spec.rb diff --git a/lib/tile_bag.rb b/lib/tile_bag.rb new file mode 100644 index 00000000..1f5810c5 --- /dev/null +++ b/lib/tile_bag.rb @@ -0,0 +1,12 @@ +module Scrabble + class TileBag + + attr_reader :bag + + def initialize(tile_bag_hash) + @bag = tile_bag_hash + end + + + end +end diff --git a/specs/tile_bag_spec.rb b/specs/tile_bag_spec.rb new file mode 100644 index 00000000..8888a315 --- /dev/null +++ b/specs/tile_bag_spec.rb @@ -0,0 +1,26 @@ +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' +require_relative '../lib/tile_bag' + + +Minitest::Reporters.use! +Minitest::Reporters::SpecReporter.new + +describe "TileBag" do + describe 'initialize' do + it 'Takes a TileBag collection' do + tile_bag = Scrabble::TileBag.new({a: 1, b: 2}) + + tile_bag.must_be_instance_of Scrabble::TileBag + tile_bag.must_respond_to :bag + tile_bag.bag.must_be_kind_of Hash + tile_bag.must_equal ({a: 1, b: 2}) + + end + + end + + + +end From 390cabe36a20ec30fe034330f42ac3058a5b5d66 Mon Sep 17 00:00:00 2001 From: Jackie Date: Thu, 22 Feb 2018 15:57:21 -0800 Subject: [PATCH 21/40] draws tiles from the tile bag --- lib/player.rb | 5 ----- lib/tile_bag.rb | 11 +++++++++++ specs/player_spec.rb | 2 +- specs/tile_bag_spec.rb | 22 ++++++++++++++++++++-- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 0875172f..fc4b3957 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -13,10 +13,6 @@ def initialize(name) @scoring_table = {} end - def plays - return @plays - end - def play(word) @plays << word end @@ -25,7 +21,6 @@ def total_score sum_of_scores = 0 @plays.each do |word| score = Scrabble::Scoring.score(word) - puts "score #{word}: #{score}" sum_of_scores += score end diff --git a/lib/tile_bag.rb b/lib/tile_bag.rb index 1f5810c5..cfc79d8b 100644 --- a/lib/tile_bag.rb +++ b/lib/tile_bag.rb @@ -5,8 +5,19 @@ class TileBag def initialize(tile_bag_hash) @bag = tile_bag_hash + @tiles_drawn = [] end + def draw_tiles(num) + while @tiles_drawn.length != num + new_letter = @bag.keys.sample + if @bag[new_letter] > 0 + @tiles_drawn << new_letter + @bag[new_letter] - 1 + end + end + return @tiles_drawn + end end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 25901c12..9711a494 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -77,7 +77,7 @@ end - it 'Returns the highest scoring played word within tie' do + it 'Returns the first word if there is a tie between hightest scoring words"' do player_1 = Scrabble::Player.new("Patrick") player_1.play("ba") player_1.play("dd") diff --git a/specs/tile_bag_spec.rb b/specs/tile_bag_spec.rb index 8888a315..2cc28c23 100644 --- a/specs/tile_bag_spec.rb +++ b/specs/tile_bag_spec.rb @@ -10,12 +10,30 @@ describe "TileBag" do describe 'initialize' do it 'Takes a TileBag collection' do - tile_bag = Scrabble::TileBag.new({a: 1, b: 2}) + tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2}) tile_bag.must_be_instance_of Scrabble::TileBag tile_bag.must_respond_to :bag tile_bag.bag.must_be_kind_of Hash - tile_bag.must_equal ({a: 1, b: 2}) + tile_bag.bag.must_equal ({"a" => 1, "b" => 2}) + + end + + describe '#draw_tiles(num)' do + it 'Draws tiles from the tile bag' do + tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) + + tile_bag.draw_tiles(3).must_be_kind_of Array + tile_bag.draw_tiles(3).must_include ("a" || "b" || "c") + tile_bag.draw_tiles(3).length.must_equal 3 + + end + + # it 'Removes tiles from the default set' do + + + + # end end From e248d2c61e47060b8e5e21323f03533481aeb2df Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Thu, 22 Feb 2018 15:58:09 -0800 Subject: [PATCH 22/40] removed wrong puts that was used for testing --- lib/player.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 0875172f..fc4b3957 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -13,10 +13,6 @@ def initialize(name) @scoring_table = {} end - def plays - return @plays - end - def play(word) @plays << word end @@ -25,7 +21,6 @@ def total_score sum_of_scores = 0 @plays.each do |word| score = Scrabble::Scoring.score(word) - puts "score #{word}: #{score}" sum_of_scores += score end From e745716a534b74f440c6da3eb39e4cd8086742a8 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Thu, 22 Feb 2018 16:25:18 -0800 Subject: [PATCH 23/40] Fixed a typo on tile_bag.rb and passed test Removes tiles from the default set --- lib/tile_bag.rb | 7 ++++++- specs/tile_bag_spec.rb | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/tile_bag.rb b/lib/tile_bag.rb index cfc79d8b..40d685c7 100644 --- a/lib/tile_bag.rb +++ b/lib/tile_bag.rb @@ -10,10 +10,11 @@ def initialize(tile_bag_hash) def draw_tiles(num) while @tiles_drawn.length != num + puts @bag new_letter = @bag.keys.sample if @bag[new_letter] > 0 @tiles_drawn << new_letter - @bag[new_letter] - 1 + @bag[new_letter] -= 1 end end return @tiles_drawn @@ -21,3 +22,7 @@ def draw_tiles(num) end end + +# tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) +# +# tile_bag.draw_tiles(3) diff --git a/specs/tile_bag_spec.rb b/specs/tile_bag_spec.rb index 2cc28c23..94affe36 100644 --- a/specs/tile_bag_spec.rb +++ b/specs/tile_bag_spec.rb @@ -24,12 +24,22 @@ tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) tile_bag.draw_tiles(3).must_be_kind_of Array - tile_bag.draw_tiles(3).must_include ("a" || "b" || "c") + # tile_bag.draw_tiles(3).must_include.any "a" || "b" || "c" tile_bag.draw_tiles(3).length.must_equal 3 end - # it 'Removes tiles from the default set' do + it 'Removes tiles from the default set' do + tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) + + new_bag = tile_bag.draw_tiles(3) + sum_of_original_bag = tile_bag.bag.values.sum + + sum_of_original_bag.must_equal 3 + new_bag.length.must_equal 3 + tile_bag.bag.values.wont_include :<, 0 + + end From e320ff242bb822cfc9efc02866d8aac9ae53af34 Mon Sep 17 00:00:00 2001 From: Jackie Date: Thu, 22 Feb 2018 17:34:00 -0800 Subject: [PATCH 24/40] Draw_tiles(tile_bag) not passing test --- lib/player.rb | 12 +++++- lib/tile_bag.rb | 10 ++++- specs/player_spec.rb | 98 ++++++++++++++++++++++++------------------ specs/tile_bag_spec.rb | 11 ++++- 4 files changed, 85 insertions(+), 46 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index fc4b3957..b0cf3dad 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,16 +1,24 @@ require_relative 'scoring' +require_relative 'tile_bag' module Scrabble - class Player + class Player < TileBag - attr_reader :name, :plays + attr_reader :name, :plays, :tiles_drawn def initialize(name) @name = name @plays = [] @scoring_table = {} + @tiles= [] + # @tiles_drawn = super + end + + def draw_tiles(tile_bag) + # @tiles << super(7 - @tiles.count) + @tiles << super(7 - @tiles.count) end def play(word) diff --git a/lib/tile_bag.rb b/lib/tile_bag.rb index 40d685c7..e556ac0f 100644 --- a/lib/tile_bag.rb +++ b/lib/tile_bag.rb @@ -1,16 +1,18 @@ + + module Scrabble class TileBag - attr_reader :bag + attr_reader :bag, :tiles_drawn def initialize(tile_bag_hash) @bag = tile_bag_hash @tiles_drawn = [] + end def draw_tiles(num) while @tiles_drawn.length != num - puts @bag new_letter = @bag.keys.sample if @bag[new_letter] > 0 @tiles_drawn << new_letter @@ -20,6 +22,10 @@ def draw_tiles(num) return @tiles_drawn end + def tiles_remaining + return @bag.values.sum + end + end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 9711a494..0a247c2f 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -3,6 +3,7 @@ require 'minitest/skip_dsl' require_relative '../lib/player' +require_relative '../lib/tile_bag' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new @@ -37,70 +38,85 @@ end end + end - describe '#total_score' do - it " Returns the sum of scores of played words" do + describe '#total_score' do + it " Returns the sum of scores of played words" do - player_1 = Scrabble::Player.new("Patrick") - player_1.play("apples") - player_1.play("fuzzy") + player_1 = Scrabble::Player.new("Patrick") + player_1.play("apples") + player_1.play("fuzzy") - player_1.total_score.must_equal 39 - end + player_1.total_score.must_equal 39 end + end - describe '#won?' do - it "Returns true if player has over 100 points" do - player_1 = Scrabble::Player.new("Patrick") - player_1.play("xxxxxxx") - player_1.won?.must_equal true + describe '#won?' do + it "Returns true if player has over 100 points" do + player_1 = Scrabble::Player.new("Patrick") + player_1.play("xxxxxxx") + player_1.won?.must_equal true - end + end - it "Returns false if player has over 100 points" do - player_1 = Scrabble::Player.new("Patrick") - player_1.play("apples") - player_1.play("fuzzy") - player_1.won?.must_equal false + it "Returns false if player has over 100 points" do + player_1 = Scrabble::Player.new("Patrick") + player_1.play("apples") + player_1.play("fuzzy") + player_1.won?.must_equal false - end + end + end - describe '#highest_scoring_word' do - it 'Returns the highest scoring played word' do - player_1 = Scrabble::Player.new("Patrick") - player_1.play("cat") - player_1.play("pig") + describe '#highest_scoring_word' do + it 'Returns the highest scoring played word' do + player_1 = Scrabble::Player.new("Patrick") + player_1.play("cat") + player_1.play("pig") - player_1.highest_scoring_word.must_equal "pig" - player_1.highest_scoring_word.must_be_kind_of String + player_1.highest_scoring_word.must_equal "pig" + player_1.highest_scoring_word.must_be_kind_of String - end + end - it 'Returns the first word if there is a tie between hightest scoring words"' do - player_1 = Scrabble::Player.new("Patrick") - player_1.play("ba") - player_1.play("dd") + it 'Returns the first word if there is a tie between hightest scoring words"' do + player_1 = Scrabble::Player.new("Patrick") + player_1.play("ba") + player_1.play("dd") - player_1.highest_scoring_word.must_equal "ba" - end + player_1.highest_scoring_word.must_equal "ba" + end - end + end - describe '#highest_word_score' do - it 'Returns the score of the highest scoring word' do - player_1 = Scrabble::Player.new("Patrick") - player_1.play("cat") - player_1.play("pig") + describe '#highest_word_score' do + it 'Returns the score of the highest scoring word' do + player_1 = Scrabble::Player.new("Patrick") + player_1.play("cat") + player_1.play("pig") - player_1.highest_word_score.must_equal 6 + player_1.highest_word_score.must_equal 6 - end end + end + + describe 'draw_tiles(tile_bag)' do + it 'is a collection of letters that the player can play (max 7)' do + player_1 = Scrabble::Player.new("Patrick") + tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) + + players_tiles = player_1.draw_tiles(tile_bag) + players_tiles.must_be_kind_of Array + # players_tiles.length.wont_be :>, 7 + all_letters = players_tiles.all? { |word| word.type == String } + all_letters.must_equal true end end + # it 'fills tiles array until it has 7 letters from the given tile bag' + # end end diff --git a/specs/tile_bag_spec.rb b/specs/tile_bag_spec.rb index 94affe36..bb8e702e 100644 --- a/specs/tile_bag_spec.rb +++ b/specs/tile_bag_spec.rb @@ -41,10 +41,19 @@ end + end + + describe '#tiles_remaining' do + it "returns number of tiles remaining in bag" do + tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) + tile_bag.draw_tiles(3) - # end + # sum_of_original_bag = tile_bag.bag.values.sum + tile_bag.tiles_remaining.must_equal 3 + + end end end From c4c390021678f7e148b8280757df501840eb6fea Mon Sep 17 00:00:00 2001 From: Jackie Date: Thu, 22 Feb 2018 18:33:54 -0800 Subject: [PATCH 25/40] Draws tiles with max of 7 --- lib/player.rb | 14 +++++++++----- lib/tile_bag.rb | 19 +++++++++++-------- specs/player_spec.rb | 9 +++++---- specs/tile_bag_spec.rb | 2 -- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index b0cf3dad..494600de 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -4,21 +4,20 @@ module Scrabble - class Player < TileBag + class Player - attr_reader :name, :plays, :tiles_drawn + attr_reader :name, :plays def initialize(name) @name = name @plays = [] @scoring_table = {} @tiles= [] - # @tiles_drawn = super end def draw_tiles(tile_bag) - # @tiles << super(7 - @tiles.count) - @tiles << super(7 - @tiles.count) + @tiles = tile_bag.draw_tiles(7 - @tiles.count) + return @tiles end def play(word) @@ -59,3 +58,8 @@ def highest_word_score end end end + +# player_1 = Scrabble::Player.new("Patrick") +# tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) +# +# players_tiles = player_1.draw_tiles(tile_bag) diff --git a/lib/tile_bag.rb b/lib/tile_bag.rb index e556ac0f..530b6085 100644 --- a/lib/tile_bag.rb +++ b/lib/tile_bag.rb @@ -3,25 +3,28 @@ module Scrabble class TileBag - attr_reader :bag, :tiles_drawn + attr_reader :bag def initialize(tile_bag_hash) @bag = tile_bag_hash - @tiles_drawn = [] + # @tiles_drawn = [] end def draw_tiles(num) - while @tiles_drawn.length != num + tiles_drawn = [ ] + + num.times do new_letter = @bag.keys.sample - if @bag[new_letter] > 0 - @tiles_drawn << new_letter - @bag[new_letter] -= 1 - end + tiles_drawn << new_letter + @bag[new_letter] -= 1 + @bag.delete_if { |letter, quantity| quantity == 0 } end - return @tiles_drawn + + return tiles_drawn end + def tiles_remaining return @bag.values.sum end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 0a247c2f..9fe16936 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -104,13 +104,14 @@ describe 'draw_tiles(tile_bag)' do it 'is a collection of letters that the player can play (max 7)' do player_1 = Scrabble::Player.new("Patrick") - tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) - + tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 5}) + players_tiles = player_1.draw_tiles(tile_bag) players_tiles.must_be_kind_of Array - # players_tiles.length.wont_be :>, 7 - all_letters = players_tiles.all? { |word| word.type == String } + puts players_tiles + puts players_tiles[0][0].class + all_letters = players_tiles.all? { |word| word.class == String } all_letters.must_equal true end diff --git a/specs/tile_bag_spec.rb b/specs/tile_bag_spec.rb index bb8e702e..859041bf 100644 --- a/specs/tile_bag_spec.rb +++ b/specs/tile_bag_spec.rb @@ -49,8 +49,6 @@ tile_bag.draw_tiles(3) - # sum_of_original_bag = tile_bag.bag.values.sum - tile_bag.tiles_remaining.must_equal 3 end From cc8d12eec0547152778ee3759c5e6361803cfadb Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Thu, 22 Feb 2018 18:34:29 -0800 Subject: [PATCH 26/40] just running tests.... --- lib/player.rb | 3 ++- lib/tile_bag.rb | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index b0cf3dad..0afa08da 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -18,7 +18,8 @@ def initialize(name) def draw_tiles(tile_bag) # @tiles << super(7 - @tiles.count) - @tiles << super(7 - @tiles.count) + amount = 7 - @tiles.count + @tiles << tile_bag.draw_tiles(amount) end def play(word) diff --git a/lib/tile_bag.rb b/lib/tile_bag.rb index e556ac0f..d8a1653d 100644 --- a/lib/tile_bag.rb +++ b/lib/tile_bag.rb @@ -12,7 +12,7 @@ def initialize(tile_bag_hash) end def draw_tiles(num) - while @tiles_drawn.length != num + while @tiles_drawn.length <= num new_letter = @bag.keys.sample if @bag[new_letter] > 0 @tiles_drawn << new_letter @@ -29,6 +29,6 @@ def tiles_remaining end end -# tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) -# -# tile_bag.draw_tiles(3) +tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) + +puts tile_bag.draw_tiles(3) From 4eb126fdf357643aae8bd5bea3ce9e00d8c78488 Mon Sep 17 00:00:00 2001 From: Jackie Date: Thu, 22 Feb 2018 18:57:15 -0800 Subject: [PATCH 27/40] fills tile array until it reaches 7 --- specs/player_spec.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 9fe16936..9f830bc2 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -109,8 +109,7 @@ players_tiles = player_1.draw_tiles(tile_bag) players_tiles.must_be_kind_of Array - puts players_tiles - puts players_tiles[0][0].class + all_letters = players_tiles.all? { |word| word.class == String } all_letters.must_equal true @@ -118,6 +117,13 @@ end - # it 'fills tiles array until it has 7 letters from the given tile bag' - # end + it 'fills tiles array until it has 7 letters from the given tile bag' do + player_1 = Scrabble::Player.new("Patrick") + tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 5}) + + players_tiles = player_1.draw_tiles(tile_bag) + + players_tiles.length.must_equal 7 + + end end From 0fe3c6cac44e137fde7f856a037ae0daa0010ae6 Mon Sep 17 00:00:00 2001 From: Jackie Date: Thu, 22 Feb 2018 18:58:10 -0800 Subject: [PATCH 28/40] fills tile array until it reaches 7 --- lib/player.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 1f3962ea..494600de 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -16,14 +16,8 @@ def initialize(name) end def draw_tiles(tile_bag) -<<<<<<< HEAD - # @tiles << super(7 - @tiles.count) - amount = 7 - @tiles.count - @tiles << tile_bag.draw_tiles(amount) -======= @tiles = tile_bag.draw_tiles(7 - @tiles.count) return @tiles ->>>>>>> c4c390021678f7e148b8280757df501840eb6fea end def play(word) From 64fec212943a619fafe6db19ea4a12bd128b9ff0 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Thu, 22 Feb 2018 18:58:39 -0800 Subject: [PATCH 29/40] git error... --- lib/player.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 1f3962ea..22ff8fe9 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -16,14 +16,9 @@ def initialize(name) end def draw_tiles(tile_bag) -<<<<<<< HEAD - # @tiles << super(7 - @tiles.count) - amount = 7 - @tiles.count - @tiles << tile_bag.draw_tiles(amount) -======= + @tiles = tile_bag.draw_tiles(7 - @tiles.count) return @tiles ->>>>>>> c4c390021678f7e148b8280757df501840eb6fea end def play(word) From 4e2541855733283709976a712c299d8716815f48 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Thu, 22 Feb 2018 20:53:45 -0800 Subject: [PATCH 30/40] Added descriptive comments on class files. --- lib/player.rb | 28 +++++++++++++++++++++------- lib/scoring.rb | 25 ++++++++++++++++++------- lib/tile_bag.rb | 18 +++++++++++------- 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 494600de..5785e485 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,9 +1,10 @@ +#*********************************************************** +#Class Player in Scrabble Module: + require_relative 'scoring' require_relative 'tile_bag' - module Scrabble - class Player attr_reader :name, :plays @@ -15,15 +16,25 @@ def initialize(name) @tiles= [] end +# _________________DRAW TILES METHOD____________________ +# Randomly chooses as many possible words (max 7) that the player can still draw from the tiles bag by calling the draw_tiles method from the TileBag class. + def draw_tiles(tile_bag) @tiles = tile_bag.draw_tiles(7 - @tiles.count) return @tiles end + +# _________________PLAY METHOD____________________ +# Accepts a word as an argument and add it to the plays array. + def play(word) @plays << word end +# _________________TOTAL SCORE METHOD____________________ +# Calculates and returns the toal score of the words in the plays array by calling the score method of the class Scoring. + def total_score sum_of_scores = 0 @plays.each do |word| @@ -34,6 +45,9 @@ def total_score return sum_of_scores end +# _________________WON? METHOD____________________ +# Returns true if the toal score is more than 100, otherwise returns false. + def won? if total_score > 100 return true @@ -42,6 +56,8 @@ def won? end end +# ___________HIGHEST SCORING WORD METHOD________________ +# Returns the highest scoring played word. def highest_scoring_word @plays.each do |word| @@ -52,14 +68,12 @@ def highest_scoring_word return winning_word_player end +# ______________HIGHEST WORD SCORE METHOD__________________ +# Returns the score of the highest scored word. + def highest_word_score highest_scoring_word return @scoring_table.values.max end end end - -# player_1 = Scrabble::Player.new("Patrick") -# tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) -# -# players_tiles = player_1.draw_tiles(tile_bag) diff --git a/lib/scoring.rb b/lib/scoring.rb index 3339b570..f02ae339 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,29 +1,38 @@ +########################################################### +#Class Scoring in Scrabble Module: module Scrabble class Scoring - def self.score(word) +# _________________SCORE METHOD____________________ +# Calculates points scored with the given word: + def self.score(word) # TODO: fix it, better way to evaluate, with arrays. + # If word contains a character that is not a letter, returns nil: if word.match?(/\W/) return nil end + # If word is has more than 7 characters, returns nil: if word.length > 7 return nil end + # Creates a new array with each letter of the word as a diferent element: letters_array = word.upcase.split("") + # If array with letters is empty, returns nil: if letters_array.empty? return nil end + + # Set the points for that word to initialize at zero and calculate the points of each word, adding them to the points variable: points = 0 letters_array.each do |letter| - if letter == "Q" || letter == "Z" points += 10 elsif letter == "D" || letter == "G" @@ -41,17 +50,19 @@ def self.score(word) end end + # If word has 7 letters add 50 more points: letters_array.length == 7 ? points += 50 : points += 0 - - return points end +# ________________HIGHEST SCORE METHOD___________________ +# Returns the word with highest score, from a given array of words: + def self.highest_score_from(array_of_words = []) - # Create & Populate array of words: + # Evaluate given words to find the one with highest score, according to the game rules. If there is a tie, it will populate a new array with the words in the tie and compare them to find the winner. winning_words = [] if array_of_words == [] return nil @@ -59,8 +70,8 @@ def self.highest_score_from(array_of_words = []) winning_words << array_of_words return winning_words[0] elsif array_of_words.max_by(&:length).length == 7 - winner = array_of_words.max_by(&:length) - return winner + winner = array_of_words.max_by(&:length) + return winner else winner = array_of_words.min_by {|word| word.length} return winner diff --git a/lib/tile_bag.rb b/lib/tile_bag.rb index 6ec0fb8e..1c44cf44 100644 --- a/lib/tile_bag.rb +++ b/lib/tile_bag.rb @@ -1,4 +1,5 @@ - +#*********************************************************** +# Class TileBag in Scrabble Module: module Scrabble class TileBag @@ -7,10 +8,15 @@ class TileBag def initialize(tile_bag_hash) @bag = tile_bag_hash - # @tiles_drawn = [] - end + # _________________DRAW TILES METHOD____________________ + # Accepts a number of tiles that needs to be drawn for the player and creates a loop that will: + # - take a random letter from the bag + # - add this letter to the array of tiles + # - subtract the quantity of that letter available in the tile bag + # - delete the key of this letter from the tile bag if there are none available anymore. + def draw_tiles(num) tiles_drawn = [ ] @@ -24,6 +30,8 @@ def draw_tiles(num) return tiles_drawn end + # _______________TILES REMAINING METHOD_________________ + # Returns the total quantity of tiles still available in the tile bag. def tiles_remaining return @bag.values.sum @@ -31,7 +39,3 @@ def tiles_remaining end end - -tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) - -puts tile_bag.draw_tiles(3) From cd4c9f4dd4e6b8b5cd63d0052c8aa3b1d0a47075 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Thu, 22 Feb 2018 22:47:18 -0800 Subject: [PATCH 31/40] changed how TileBag is initialized (not taking any arguments now), and fixed all tests related to it. --- lib/player.rb | 4 ++++ lib/tile_bag.rb | 10 ++++++++-- specs/player_spec.rb | 6 +++--- specs/tile_bag_spec.rb | 14 +++++++------- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 5785e485..72cf44ca 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -60,6 +60,10 @@ def won? # Returns the highest scoring played word. def highest_scoring_word + + # Should be: + # return Scoring.highest_score_from(@plays) + @plays.each do |word| score = Scrabble::Scoring.score(word) @scoring_table["#{word}"] = score diff --git a/lib/tile_bag.rb b/lib/tile_bag.rb index 1c44cf44..8b22dc6b 100644 --- a/lib/tile_bag.rb +++ b/lib/tile_bag.rb @@ -6,8 +6,14 @@ class TileBag attr_reader :bag - def initialize(tile_bag_hash) - @bag = tile_bag_hash + def initialize #(tile_bag_hash) + # @bag = tile_bag_hash + @bag = { + "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 # _________________DRAW TILES METHOD____________________ diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 9f830bc2..b1727c6a 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -104,12 +104,12 @@ describe 'draw_tiles(tile_bag)' do it 'is a collection of letters that the player can play (max 7)' do player_1 = Scrabble::Player.new("Patrick") - tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 5}) + tile_bag = Scrabble::TileBag.new #({"a" => 1, "b" => 2, "c" => 5}) players_tiles = player_1.draw_tiles(tile_bag) players_tiles.must_be_kind_of Array - + all_letters = players_tiles.all? { |word| word.class == String } all_letters.must_equal true @@ -119,7 +119,7 @@ it 'fills tiles array until it has 7 letters from the given tile bag' do player_1 = Scrabble::Player.new("Patrick") - tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 5}) + tile_bag = Scrabble::TileBag.new #({"a" => 1, "b" => 2, "c" => 5}) players_tiles = player_1.draw_tiles(tile_bag) diff --git a/specs/tile_bag_spec.rb b/specs/tile_bag_spec.rb index 859041bf..fafbff6f 100644 --- a/specs/tile_bag_spec.rb +++ b/specs/tile_bag_spec.rb @@ -10,18 +10,18 @@ describe "TileBag" do describe 'initialize' do it 'Takes a TileBag collection' do - tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2}) + tile_bag = Scrabble::TileBag.new #({"a" => 1, "b" => 2}) tile_bag.must_be_instance_of Scrabble::TileBag tile_bag.must_respond_to :bag tile_bag.bag.must_be_kind_of Hash - tile_bag.bag.must_equal ({"a" => 1, "b" => 2}) + #tile_bag.bag.must_equal ({"a" => 1, "b" => 2}) end describe '#draw_tiles(num)' do it 'Draws tiles from the tile bag' do - tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) + tile_bag = Scrabble::TileBag.new #({"a" => 1, "b" => 2, "c" => 3}) tile_bag.draw_tiles(3).must_be_kind_of Array # tile_bag.draw_tiles(3).must_include.any "a" || "b" || "c" @@ -30,12 +30,12 @@ end it 'Removes tiles from the default set' do - tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) + tile_bag = Scrabble::TileBag.new #({"a" => 1, "b" => 2, "c" => 3}) new_bag = tile_bag.draw_tiles(3) sum_of_original_bag = tile_bag.bag.values.sum - sum_of_original_bag.must_equal 3 + sum_of_original_bag.must_equal 98 - 3 new_bag.length.must_equal 3 tile_bag.bag.values.wont_include :<, 0 @@ -45,11 +45,11 @@ describe '#tiles_remaining' do it "returns number of tiles remaining in bag" do - tile_bag = Scrabble::TileBag.new({"a" => 1, "b" => 2, "c" => 3}) + tile_bag = Scrabble::TileBag.new #({"a" => 1, "b" => 2, "c" => 3}) tile_bag.draw_tiles(3) - tile_bag.tiles_remaining.must_equal 3 + tile_bag.tiles_remaining.must_equal 98 - 3 end end From 3965a05e3f0e09b5209f095566da97f180f65765 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Thu, 22 Feb 2018 23:54:10 -0800 Subject: [PATCH 32/40] Changed the highest_scoring_word(player.rb) to call the highest_score_from(scoring.rb) method to score every word from the given array and find the 'winner word'. --- lib/player.rb | 12 ++---------- lib/scoring.rb | 33 +++++++++++++++++++++++++-------- specs/scoring_spec.rb | 6 +++--- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 72cf44ca..583169d8 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -61,23 +61,15 @@ def won? def highest_scoring_word - # Should be: - # return Scoring.highest_score_from(@plays) + return Scrabble::Scoring.highest_score_from(@plays) - @plays.each do |word| - score = Scrabble::Scoring.score(word) - @scoring_table["#{word}"] = score - end - winning_word_player = @scoring_table.key(@scoring_table.values.max) - return winning_word_player end # ______________HIGHEST WORD SCORE METHOD__________________ # Returns the score of the highest scored word. def highest_word_score - highest_scoring_word - return @scoring_table.values.max + return Scrabble::Scoring.score(highest_scoring_word) end end end diff --git a/lib/scoring.rb b/lib/scoring.rb index f02ae339..4d9c155d 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -61,19 +61,34 @@ def self.score(word) # Returns the word with highest score, from a given array of words: def self.highest_score_from(array_of_words = []) - # Evaluate given words to find the one with highest score, according to the game rules. If there is a tie, it will populate a new array with the words in the tie and compare them to find the winner. - winning_words = [] + + scoring_table = {} + + # Set the scores of each word: + array_of_words.each do |word| + score = score(word) + scoring_table["#{word}"] = score + end + + # Find the words with the maximu score between them: + max = scoring_table.values.max + max_scored_words_hash = Hash[scoring_table.select { |k, v| v == max}] + + # Select only the keys from the has with the highest scored words: + winning_words = max_scored_words_hash.keys + + # Choose and return the winning word according to the rules and solving a tie if any: if array_of_words == [] return nil - elsif array_of_words.length == 1 - winning_words << array_of_words - return winning_words[0] - elsif array_of_words.max_by(&:length).length == 7 - winner = array_of_words.max_by(&:length) + elsif winning_words.length == 1 + winner = winning_words[0] + return winner + elsif winning_words.max_by(&:length).length == 7 + winner = winning_words.max_by(&:length) return winner else - winner = array_of_words.min_by {|word| word.length} + winner = winning_words.min_by {|word| word.length} return winner end end @@ -81,3 +96,5 @@ def self.highest_score_from(array_of_words = []) end #Scoring end #Scrabble + +# winning_words.max_by(&:length).length diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 5cd6304b..db12f2f6 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -47,9 +47,9 @@ it 'returns the only word in a length-1 array' do winning_word = Scrabble::Scoring.highest_score_from(["hotdog"]) - winning_word[0].must_equal "hotdog" - winning_word.must_be_kind_of Array - winning_word.length.must_equal 1 + winning_word.must_equal "hotdog" + winning_word.must_be_kind_of String + # winning_word.length.must_equal 1 end From c21f74db3f8a6b2cb93757e57202d2a5e1da8a73 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Thu, 22 Feb 2018 23:57:28 -0800 Subject: [PATCH 33/40] Deleted unecessary testing comments. --- lib/scoring.rb | 2 -- lib/tile_bag.rb | 3 +-- specs/player_spec.rb | 12 ++++-------- specs/scoring_spec.rb | 1 - specs/tile_bag_spec.rb | 10 ++++------ 5 files changed, 9 insertions(+), 19 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 4d9c155d..d9a4c80c 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -96,5 +96,3 @@ def self.highest_score_from(array_of_words = []) end #Scoring end #Scrabble - -# winning_words.max_by(&:length).length diff --git a/lib/tile_bag.rb b/lib/tile_bag.rb index 8b22dc6b..29978639 100644 --- a/lib/tile_bag.rb +++ b/lib/tile_bag.rb @@ -6,8 +6,7 @@ class TileBag attr_reader :bag - def initialize #(tile_bag_hash) - # @bag = tile_bag_hash + def initialize @bag = { "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, diff --git a/specs/player_spec.rb b/specs/player_spec.rb index b1727c6a..9d260ba4 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -21,16 +21,15 @@ end describe "#plays" do it "Returns an array of words played:" do - player_1 = Scrabble::Player.new("Patrick") player_1.plays.must_be_kind_of Array + end end describe "#play(word)" do it "Adds input word to @plays array." do - player_1 = Scrabble::Player.new("Patrick") player_1.play("orange") @@ -42,7 +41,6 @@ describe '#total_score' do it " Returns the sum of scores of played words" do - player_1 = Scrabble::Player.new("Patrick") player_1.play("apples") player_1.play("fuzzy") @@ -86,6 +84,7 @@ player_1.play("dd") player_1.highest_scoring_word.must_equal "ba" + end end @@ -104,14 +103,12 @@ describe 'draw_tiles(tile_bag)' do it 'is a collection of letters that the player can play (max 7)' do player_1 = Scrabble::Player.new("Patrick") - tile_bag = Scrabble::TileBag.new #({"a" => 1, "b" => 2, "c" => 5}) + tile_bag = Scrabble::TileBag.new players_tiles = player_1.draw_tiles(tile_bag) players_tiles.must_be_kind_of Array - all_letters = players_tiles.all? { |word| word.class == String } - all_letters.must_equal true end @@ -119,10 +116,9 @@ it 'fills tiles array until it has 7 letters from the given tile bag' do player_1 = Scrabble::Player.new("Patrick") - tile_bag = Scrabble::TileBag.new #({"a" => 1, "b" => 2, "c" => 5}) + tile_bag = Scrabble::TileBag.new players_tiles = player_1.draw_tiles(tile_bag) - players_tiles.length.must_equal 7 end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index db12f2f6..11d1a7dd 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -49,7 +49,6 @@ winning_word = Scrabble::Scoring.highest_score_from(["hotdog"]) winning_word.must_equal "hotdog" winning_word.must_be_kind_of String - # winning_word.length.must_equal 1 end diff --git a/specs/tile_bag_spec.rb b/specs/tile_bag_spec.rb index fafbff6f..fe8d57b0 100644 --- a/specs/tile_bag_spec.rb +++ b/specs/tile_bag_spec.rb @@ -10,18 +10,17 @@ describe "TileBag" do describe 'initialize' do it 'Takes a TileBag collection' do - tile_bag = Scrabble::TileBag.new #({"a" => 1, "b" => 2}) + tile_bag = Scrabble::TileBag.new tile_bag.must_be_instance_of Scrabble::TileBag tile_bag.must_respond_to :bag tile_bag.bag.must_be_kind_of Hash - #tile_bag.bag.must_equal ({"a" => 1, "b" => 2}) end describe '#draw_tiles(num)' do it 'Draws tiles from the tile bag' do - tile_bag = Scrabble::TileBag.new #({"a" => 1, "b" => 2, "c" => 3}) + tile_bag = Scrabble::TileBag.new tile_bag.draw_tiles(3).must_be_kind_of Array # tile_bag.draw_tiles(3).must_include.any "a" || "b" || "c" @@ -30,7 +29,7 @@ end it 'Removes tiles from the default set' do - tile_bag = Scrabble::TileBag.new #({"a" => 1, "b" => 2, "c" => 3}) + tile_bag = Scrabble::TileBag.new new_bag = tile_bag.draw_tiles(3) sum_of_original_bag = tile_bag.bag.values.sum @@ -45,10 +44,9 @@ describe '#tiles_remaining' do it "returns number of tiles remaining in bag" do - tile_bag = Scrabble::TileBag.new #({"a" => 1, "b" => 2, "c" => 3}) + tile_bag = Scrabble::TileBag.new tile_bag.draw_tiles(3) - tile_bag.tiles_remaining.must_equal 98 - 3 end From be94de4f5f43d60aa5cf326fc830a90d04157b72 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Fri, 23 Feb 2018 13:56:48 -0800 Subject: [PATCH 34/40] Refactor some iterations/conditional blocks into one line. --- lib/player.rb | 8 +------- lib/scoring.rb | 5 +---- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 583169d8..7a83594c 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -49,20 +49,14 @@ def total_score # Returns true if the toal score is more than 100, otherwise returns false. def won? - if total_score > 100 - return true - else - return false - end + total_score > 100 ? true : false end # ___________HIGHEST SCORING WORD METHOD________________ # Returns the highest scoring played word. def highest_scoring_word - return Scrabble::Scoring.highest_score_from(@plays) - end # ______________HIGHEST WORD SCORE METHOD__________________ diff --git a/lib/scoring.rb b/lib/scoring.rb index d9a4c80c..a1d1bfaa 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -66,10 +66,7 @@ def self.highest_score_from(array_of_words = []) scoring_table = {} # Set the scores of each word: - array_of_words.each do |word| - score = score(word) - scoring_table["#{word}"] = score - end + array_of_words.each {|word| scoring_table["#{word}"] = score(word)} # Find the words with the maximu score between them: max = scoring_table.values.max From 3ab27bff62f5469a2a35d8e169bb36b18bc134c8 Mon Sep 17 00:00:00 2001 From: Jackie Date: Fri, 23 Feb 2018 14:27:14 -0800 Subject: [PATCH 35/40] Removed unnecessary scoring table hash variable from player.rb, added assertion to draw_tiles player_spec. --- lib/player.rb | 1 - specs/tile_bag_spec.rb | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 7a83594c..e65e418c 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -12,7 +12,6 @@ class Player def initialize(name) @name = name @plays = [] - @scoring_table = {} @tiles= [] end diff --git a/specs/tile_bag_spec.rb b/specs/tile_bag_spec.rb index fe8d57b0..8fd19f06 100644 --- a/specs/tile_bag_spec.rb +++ b/specs/tile_bag_spec.rb @@ -23,7 +23,10 @@ tile_bag = Scrabble::TileBag.new tile_bag.draw_tiles(3).must_be_kind_of Array - # tile_bag.draw_tiles(3).must_include.any "a" || "b" || "c" + all_letters = tile_bag.draw_tiles(3).all? { |letter| letter.class == String } + + all_letters.must_equal true + tile_bag.draw_tiles(3).length.must_equal 3 end From eac411cc1a84e611cce251edafd908172d27e023 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Fri, 23 Feb 2018 15:14:01 -0800 Subject: [PATCH 36/40] Changed method score in score.rb to calculate points using 'case' instead of if statments. --- lib/scoring.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index a1d1bfaa..1cbb28b4 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -32,18 +32,20 @@ def self.score(word) # Set the points for that word to initialize at zero and calculate the points of each word, adding them to the points variable: points = 0 + letters_array.each do |letter| - if letter == "Q" || letter == "Z" + case letter + when "Q", "Z" points += 10 - elsif letter == "D" || letter == "G" + when "D", "G" points += 2 - elsif letter =="B" || letter == "C" || letter =="M" || letter == "P" + when "B", "C", "M", "P" points += 3 - elsif letter == "F" || letter == "H" || letter == "V" || letter == "W" || letter == "Y" + when "F", "H", "V", "W", "Y" points += 4 - elsif letter == "K" + when "K" points += 5 - elsif letter == "J" || letter == "X" + when "J", "X" points += 8 else #"A", "E", "I", "O", "U", "L", "N", "R", "S", "T" points += 1 From deeb039d4e35dee27e493a25903d3920b202a98e Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Fri, 23 Feb 2018 15:16:20 -0800 Subject: [PATCH 37/40] Merge after changing score method to use case instead of if to evaluate points --- lib/scoring.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 1cbb28b4..97852266 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -9,8 +9,6 @@ class Scoring def self.score(word) - # TODO: fix it, better way to evaluate, with arrays. - # If word contains a character that is not a letter, returns nil: if word.match?(/\W/) return nil From 897a2c8f1669162c103f200032029808c7384e39 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Sun, 25 Feb 2018 12:06:38 -0800 Subject: [PATCH 38/40] Changed how the highest_score_from is called on player.rb and deleted a unecessary require command at the top of file. --- lib/player.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index e65e418c..049af9a5 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -2,7 +2,6 @@ #Class Player in Scrabble Module: require_relative 'scoring' -require_relative 'tile_bag' module Scrabble class Player @@ -37,7 +36,7 @@ def play(word) def total_score sum_of_scores = 0 @plays.each do |word| - score = Scrabble::Scoring.score(word) + score = Scoring.score(word) sum_of_scores += score end @@ -55,14 +54,14 @@ def won? # Returns the highest scoring played word. def highest_scoring_word - return Scrabble::Scoring.highest_score_from(@plays) + return Scoring.highest_score_from(@plays) end # ______________HIGHEST WORD SCORE METHOD__________________ # Returns the score of the highest scored word. def highest_word_score - return Scrabble::Scoring.score(highest_scoring_word) + return Scoring.score(highest_scoring_word) end end end From 0dfb93e133e0acd49c80dd2ded69774b430a5c60 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Mon, 26 Feb 2018 15:04:49 -0800 Subject: [PATCH 39/40] Refactor the project to atend to the necessary changes on the first 4 comments from CheezItMan --- lib/player.rb | 2 +- lib/scoring.rb | 50 +++++++++++++++++++++++++++---------------- specs/player_spec.rb | 7 ++++++ specs/scoring_spec.rb | 3 +++ 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 049af9a5..d8197551 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -27,7 +27,7 @@ def draw_tiles(tile_bag) # Accepts a word as an argument and add it to the plays array. def play(word) - @plays << word + won? ? false : @plays << word end # _________________TOTAL SCORE METHOD____________________ diff --git a/lib/scoring.rb b/lib/scoring.rb index 97852266..02019ea0 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -31,24 +31,38 @@ def self.score(word) # Set the points for that word to initialize at zero and calculate the points of each word, adding them to the points variable: points = 0 - letters_array.each do |letter| - case letter - when "Q", "Z" - points += 10 - when "D", "G" - points += 2 - when "B", "C", "M", "P" - points += 3 - when "F", "H", "V", "W", "Y" - points += 4 - when "K" - points += 5 - when "J", "X" - points += 8 - else #"A", "E", "I", "O", "U", "L", "N", "R", "S", "T" - points += 1 + points_hash = { + 10 => ["Q", "Z"], + 2 => ["D", "G"], + 3 => ["B", "C", "M", "P"], + 4 => ["F", "H", "V", "W", "Y"], + 5 => 'K', + 8 => ["J", "X"], + 1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"] } + + + letters_array.each do |letter| + points_hash.each { |key, value| value.include?(letter) ? points += key : + 0 } end - end + + # Before it was: + # case letter + # when "Q", "Z" + # points += 10 + # when "D", "G" + # points += 2 + # when "B", "C", "M", "P" + # points += 3 + # when "F", "H", "V", "W", "Y" + # points += 4 + # when "K" + # points += 5 + # when "J", "X" + # points += 8 + # else #"A", "E", "I", "O", "U", "L", "N", "R", "S", "T" + # points += 1 + # end + # end # If word has 7 letters add 50 more points: letters_array.length == 7 ? points += 50 : points += 0 @@ -66,7 +80,7 @@ def self.highest_score_from(array_of_words = []) scoring_table = {} # Set the scores of each word: - array_of_words.each {|word| scoring_table["#{word}"] = score(word)} + array_of_words.each {|word| scoring_table[word] = score(word)} # Find the words with the maximu score between them: max = scoring_table.values.max diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 9d260ba4..a774e016 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -36,6 +36,13 @@ player_1.plays.must_include "orange" end + + it "Returns false if player already won" do + player_1 = Scrabble::Player.new("Patrick") + player_1.play("xxxxxxx") + + player_1.play("orange").must_equal false + end end end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 11d1a7dd..6ce16cf0 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -55,6 +55,9 @@ it 'returns the highest word if there are two words' do winning_words = Scrabble::Scoring.highest_score_from(["dog", "academy"]) winning_words.must_equal "academy" + + winning_words = Scrabble::Scoring.highest_score_from(["academy", "dog"]) + winning_words.must_equal "academy" end it 'if tied, prefer a word with 7 letters' do From 2405cff857c108d02f50ffd130d228518a747244 Mon Sep 17 00:00:00 2001 From: Jackie Date: Mon, 26 Feb 2018 17:03:38 -0800 Subject: [PATCH 40/40] Added edge cases to scoring_spec and tile_bag --- lib/player.rb | 2 ++ lib/scoring.rb | 17 ++++++++--------- lib/tile_bag.rb | 6 +++++- specs/player_spec.rb | 24 +++++++++++++++++++++++- specs/tile_bag_spec.rb | 7 +++++++ 5 files changed, 45 insertions(+), 11 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index d8197551..fff64100 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -27,6 +27,8 @@ def draw_tiles(tile_bag) # Accepts a word as an argument and add it to the plays array. def play(word) + return nil if word == nil || word.match?(/\W|\s/) || word.length > 7 || word.length == 0 + won? ? false : @plays << word end diff --git a/lib/scoring.rb b/lib/scoring.rb index 02019ea0..091ea162 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -10,22 +10,22 @@ class Scoring def self.score(word) # If word contains a character that is not a letter, returns nil: - if word.match?(/\W/) + if word.match?(/\W/) || word.length > 7 || word == nil || word.length == 0 return nil end # If word is has more than 7 characters, returns nil: - if word.length > 7 - return nil - end + # if word.length > 7 + # return nil + # end # Creates a new array with each letter of the word as a diferent element: letters_array = word.upcase.split("") # If array with letters is empty, returns nil: - if letters_array.empty? - return nil - end + # if letters_array.empty? + # return nil + # end # Set the points for that word to initialize at zero and calculate the points of each word, adding them to the points variable: @@ -90,7 +90,7 @@ def self.highest_score_from(array_of_words = []) winning_words = max_scored_words_hash.keys # Choose and return the winning word according to the rules and solving a tie if any: - if array_of_words == [] + if array_of_words.class != Array || array_of_words.empty? return nil elsif winning_words.length == 1 winner = winning_words[0] @@ -104,6 +104,5 @@ def self.highest_score_from(array_of_words = []) end end - end #Scoring end #Scrabble diff --git a/lib/tile_bag.rb b/lib/tile_bag.rb index 29978639..52103bf7 100644 --- a/lib/tile_bag.rb +++ b/lib/tile_bag.rb @@ -7,7 +7,8 @@ class TileBag attr_reader :bag def initialize - @bag = { + @bag = + { "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, @@ -24,6 +25,9 @@ def initialize def draw_tiles(num) tiles_drawn = [ ] + if tiles_remaining < num + raise ArgumentError.new "NO" + end num.times do new_letter = @bag.keys.sample diff --git a/specs/player_spec.rb b/specs/player_spec.rb index a774e016..91b741f1 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -43,6 +43,28 @@ player_1.play("orange").must_equal false end + + it "Returns nil for invalid inputs of word" do + player_1 = Scrabble::Player.new("Patrick") + + player_1.play(nil).must_be_nil + player_1.play("xxxxxxxxx").must_be_nil + player_1.play("123*&$").must_be_nil + player_1.play("").must_be_nil + end + + it "Returns nil for multiple words entered" do + player_1 = Scrabble::Player.new("Patrick") + player_1.play("hello hello").must_be_nil + + end + + it "Will not let player play word if they already won" do + player_1 = Scrabble::Player.new("Patrick") + player_1.play("xxxxxxx") + player_1.play("dog").must_equal false + end + end end @@ -64,7 +86,7 @@ end - it "Returns false if player has over 100 points" do + it "Returns false if player has less than 100 points" do player_1 = Scrabble::Player.new("Patrick") player_1.play("apples") player_1.play("fuzzy") diff --git a/specs/tile_bag_spec.rb b/specs/tile_bag_spec.rb index 8fd19f06..d4a7e621 100644 --- a/specs/tile_bag_spec.rb +++ b/specs/tile_bag_spec.rb @@ -31,6 +31,12 @@ end + it "Does not remove tiles if player draws more than there are in tile bag" do + tile_bag = Scrabble::TileBag.new + proc { tile_bag.draw_tiles(300)}.must_raise ArgumentError + + end + it 'Removes tiles from the default set' do tile_bag = Scrabble::TileBag.new @@ -53,6 +59,7 @@ tile_bag.tiles_remaining.must_equal 98 - 3 end + end end