From ccd536548deb56cd686459b12b8e4d93c27cd5d9 Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Mon, 29 Aug 2016 16:51:50 -0700 Subject: [PATCH 01/28] add Rakefile, specs folder,spec_helper.rb; create scrabble.rb and scrabble_spec.rb --- Rakefile | 7 +++++++ scrabble.rb | 13 +++++++++++++ specs/scrabble_spec.rb | 10 ++++++++++ specs/spec_helper.rb | 7 +++++++ 4 files changed, 37 insertions(+) create mode 100644 Rakefile create mode 100644 scrabble.rb create mode 100644 specs/scrabble_spec.rb create mode 100644 specs/spec_helper.rb diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..82e5a661 --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.test_files = FileList['specs/*_spec.rb'] + end + +task default: :test diff --git a/scrabble.rb b/scrabble.rb new file mode 100644 index 00000000..3faa81dc --- /dev/null +++ b/scrabble.rb @@ -0,0 +1,13 @@ +module Scrabble + class Scoring + LETTERS = { + 1 => %w(A E I O U L N R S T), + 2 => %w(D G), + 3 => %w(B C M P), + 4 => %w(F H V W Y), + 5 => ["K"], + 8 => %w(J X), + 10 => %w(Q Z) + } + end +end diff --git a/specs/scrabble_spec.rb b/specs/scrabble_spec.rb new file mode 100644 index 00000000..efd36779 --- /dev/null +++ b/specs/scrabble_spec.rb @@ -0,0 +1,10 @@ +require_relative 'spec_helper' +require_relative '../scrabble' + +describe Scrabble do +# describe Scoring do + it "should have a constant equal to a hash" do + Scrabble::Scoring::LETTERS.must_be_instance_of(Hash) + end +# end +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..7b9117e1 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,7 @@ +require 'minitest' +require 'minitest/spec' +require "minitest/autorun" +require "minitest/reporters" +require 'minitest/pride' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From f7745683cb012b3fc1aad1f559516e2166696197 Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Tue, 30 Aug 2016 10:31:15 -0700 Subject: [PATCH 02/28] renamed scrabble_spec.rb to scoring_spec.rb --- scrabble.rb => scoring.rb | 0 specs/{scrabble_spec.rb => scoring_spec.rb} | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename scrabble.rb => scoring.rb (100%) rename specs/{scrabble_spec.rb => scoring_spec.rb} (84%) diff --git a/scrabble.rb b/scoring.rb similarity index 100% rename from scrabble.rb rename to scoring.rb diff --git a/specs/scrabble_spec.rb b/specs/scoring_spec.rb similarity index 84% rename from specs/scrabble_spec.rb rename to specs/scoring_spec.rb index efd36779..f65507da 100644 --- a/specs/scrabble_spec.rb +++ b/specs/scoring_spec.rb @@ -2,9 +2,9 @@ require_relative '../scrabble' describe Scrabble do -# describe Scoring do + describe Scrabble::Scoring do it "should have a constant equal to a hash" do Scrabble::Scoring::LETTERS.must_be_instance_of(Hash) end -# end + end end From de3e52cf54f2f0fba88ce408a5591f0df6f1235c Mon Sep 17 00:00:00 2001 From: Yeni Date: Tue, 30 Aug 2016 11:21:18 -0700 Subject: [PATCH 03/28] updated files --- scoring.rb | 4 ++++ specs/scoring_spec.rb | 14 +++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/scoring.rb b/scoring.rb index 3faa81dc..adbf7cc8 100644 --- a/scoring.rb +++ b/scoring.rb @@ -9,5 +9,9 @@ class Scoring 8 => %w(J X), 10 => %w(Q Z) } + + def self.score(word) + + end end end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index f65507da..18ca6db4 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -1,10 +1,18 @@ require_relative 'spec_helper' -require_relative '../scrabble' +require_relative '../scoring' -describe Scrabble do describe Scrabble::Scoring do it "should have a constant equal to a hash" do Scrabble::Scoring::LETTERS.must_be_instance_of(Hash) end + + describe "self.score" do + it "should take one argument only" do + proc { Scrabble::Scoring.score }.must_raise ArgumentError + end + + + + end + end -end From 66b5fdbb693b52ee645b0d4feba7564bc2cd3789 Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Tue, 30 Aug 2016 12:27:25 -0700 Subject: [PATCH 04/28] implemented score method. WIP --- scoring.rb | 17 ++++++++++++++++- specs/scoring_spec.rb | 29 ++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/scoring.rb b/scoring.rb index adbf7cc8..ed94e03f 100644 --- a/scoring.rb +++ b/scoring.rb @@ -11,7 +11,22 @@ class Scoring } def self.score(word) - + if word == "" + raise ArgumentError.new("No empty strings") + end + score = 0 + user_word = word.upcase.split("") + user_word.each do |letter| + LETTERS.each do |key, value| + if value.include?(letter) + score += key + end + end + end + if user_word.length == 7 + score += 50 + end + return score end end end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 18ca6db4..0a0441da 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -1,18 +1,29 @@ require_relative 'spec_helper' require_relative '../scoring' - describe Scrabble::Scoring do - it "should have a constant equal to a hash" do - Scrabble::Scoring::LETTERS.must_be_instance_of(Hash) - end - - describe "self.score" do - it "should take one argument only" do - proc { Scrabble::Scoring.score }.must_raise ArgumentError - end +known_words = { + "word" => 8, # 4,1,1,2 + "cat" => 5, # 3,1,1 + "ferrets" => 60, # 50 + 4,1,1,1,1,1,1 +} +describe Scrabble::Scoring do + it "should have a constant equal to a hash" do + Scrabble::Scoring::LETTERS.must_be_instance_of(Hash) + end + describe "self.score" do + it "must correctly score known words" do + known_words.each do |word, expected_score| + Scrabble::Scoring.score(word).must_equal(expected_score) + end + end + it "must not have an empty string argument" do + proc { Scrabble::Scoring.score("") }.must_raise(ArgumentError) end + # write assertion for words greater than 7 end + +end From 9cbb5b5cf9ced0708da778cd08205c262c73d88c Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Tue, 30 Aug 2016 15:24:01 -0700 Subject: [PATCH 05/28] Added ArgError handling for words = 0 or greater than 7. --- scoring.rb | 3 ++- specs/scoring_spec.rb | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/scoring.rb b/scoring.rb index ed94e03f..af5bdc77 100644 --- a/scoring.rb +++ b/scoring.rb @@ -11,9 +11,10 @@ class Scoring } def self.score(word) - if word == "" + if word.length < 1 || word.length > 7 raise ArgumentError.new("No empty strings") end + score = 0 user_word = word.upcase.split("") user_word.each do |letter| diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 0a0441da..138441b0 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -24,6 +24,10 @@ end # write assertion for words greater than 7 + + it "must not take an argument greater than 7 letters" do + proc {Scrabble::Scoring.score("caterpillar")}.must_raise(ArgumentError) + end end end From 4a28361139f730da51c1498072cd9672bb0997ca Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Tue, 30 Aug 2016 15:26:23 -0700 Subject: [PATCH 06/28] Changed something?? --- specs/scoring_spec.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 138441b0..9f859a67 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -23,8 +23,6 @@ proc { Scrabble::Scoring.score("") }.must_raise(ArgumentError) end - # write assertion for words greater than 7 - it "must not take an argument greater than 7 letters" do proc {Scrabble::Scoring.score("caterpillar")}.must_raise(ArgumentError) end From 3db257fe9a8d582f68ca0418a5cacad62430b3dd Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Tue, 30 Aug 2016 16:05:18 -0700 Subject: [PATCH 07/28] Tried test for checking for non-letter characters. Doesn't work yet. --- scoring.rb | 6 ++++++ specs/scoring_spec.rb | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/scoring.rb b/scoring.rb index af5bdc77..7fa5e838 100644 --- a/scoring.rb +++ b/scoring.rb @@ -15,6 +15,12 @@ def self.score(word) raise ArgumentError.new("No empty strings") end + # raise ArgumentError.new("Invalid characters") + +# if !LETTERS.values.include?(word.split("")) +# puts "no!" +# end + score = 0 user_word = word.upcase.split("") user_word.each do |letter| diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 9f859a67..a2dec70a 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -19,6 +19,10 @@ end end + # it "should raise an error if there are any non-letter characters in a word" do + # proc { Scrabble::Scoring.score("1de") }.must_raise(ArgumentError) + # end + it "must not have an empty string argument" do proc { Scrabble::Scoring.score("") }.must_raise(ArgumentError) end From a0dc9bfa6a208c13f61d8901a016f203423eb5d4 Mon Sep 17 00:00:00 2001 From: Yeni Date: Tue, 30 Aug 2016 16:40:51 -0700 Subject: [PATCH 08/28] added highest_score_from method --- scoring.rb | 10 ++++++++++ specs/scoring_spec.rb | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/scoring.rb b/scoring.rb index 7fa5e838..d8020a63 100644 --- a/scoring.rb +++ b/scoring.rb @@ -35,5 +35,15 @@ def self.score(word) end return score end + + def self.highest_score_from(array_of_words) + scoring_hash = {} + raise ArgumentError.new("Invalid input type") if !array_of_words.is_a?(Array) + array_of_words.each do |word| + scoring_hash[word] = Scrabble::Scoring.score(word) + end + # scoring_hash.max_by{|k, v| return k} + return scoring_hash + end end end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index a2dec70a..3c5c6ca1 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -2,11 +2,13 @@ require_relative '../scoring' known_words = { - "word" => 8, # 4,1,1,2 + "farm" => 9, # 4,1,1,3 "cat" => 5, # 3,1,1 "ferrets" => 60, # 50 + 4,1,1,1,1,1,1 } +test_words = ["farm", "cat", "ferrets"] + describe Scrabble::Scoring do it "should have a constant equal to a hash" do Scrabble::Scoring::LETTERS.must_be_instance_of(Hash) @@ -32,4 +34,17 @@ end end + describe "self.highest_score_from" do + + it "should raise an error when non-array is passed in" do + proc{ Scrabble::Scoring.highest_score_from({}) }.must_raise(ArgumentError) + end + + it "must return the word with the highest score" do + Scrabble::Scoring.highest_score_from(test_words).must_equal("ferrets") + + end + + end + end From e81aa3cdc23faa9c739816658605f219d2c0dc4f Mon Sep 17 00:00:00 2001 From: Yeni Date: Tue, 30 Aug 2016 16:49:48 -0700 Subject: [PATCH 09/28] completed first part of highest_score_from --- scoring.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scoring.rb b/scoring.rb index d8020a63..484552b2 100644 --- a/scoring.rb +++ b/scoring.rb @@ -42,8 +42,8 @@ def self.highest_score_from(array_of_words) array_of_words.each do |word| scoring_hash[word] = Scrabble::Scoring.score(word) end - # scoring_hash.max_by{|k, v| return k} - return scoring_hash + max_val = scoring_hash.values.max + return scoring_hash.index(max_val) end end end From 45ff629f09570298a61059bb0305c840dfd15e9d Mon Sep 17 00:00:00 2001 From: Yeni Date: Tue, 30 Aug 2016 16:54:51 -0700 Subject: [PATCH 10/28] adding comments on what to do next --- specs/scoring_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 3c5c6ca1..1ac78ef3 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -9,6 +9,8 @@ test_words = ["farm", "cat", "ferrets"] +#create test array with tied score words with different lengths, one with seven + describe Scrabble::Scoring do it "should have a constant equal to a hash" do Scrabble::Scoring::LETTERS.must_be_instance_of(Hash) @@ -45,6 +47,8 @@ end + #separate tests with each tie breaker + end end From 9fff1dee61d88b85d835985da7cdd4032766fed2 Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Wed, 31 Aug 2016 10:31:47 -0700 Subject: [PATCH 11/28] completed implementation of tiebreakers (and tests) in self.highest_score_from --- scoring.rb | 16 +++++++++++++++- specs/scoring_spec.rb | 21 ++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/scoring.rb b/scoring.rb index 484552b2..a6978469 100644 --- a/scoring.rb +++ b/scoring.rb @@ -38,12 +38,26 @@ def self.score(word) def self.highest_score_from(array_of_words) scoring_hash = {} + max_words = [] raise ArgumentError.new("Invalid input type") if !array_of_words.is_a?(Array) array_of_words.each do |word| scoring_hash[word] = Scrabble::Scoring.score(word) end + max_val = scoring_hash.values.max - return scoring_hash.index(max_val) + scoring_hash.each do |key, value| + if value == max_val + max_words << key + end + end + + max_words.sort_by! {|word| word.length} + + if max_words.last.length == 7 + return max_words.last + else + return max_words.first + end end end end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 1ac78ef3..12eb77c9 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -9,7 +9,14 @@ test_words = ["farm", "cat", "ferrets"] -#create test array with tied score words with different lengths, one with seven +#create test array with tied score words with different lengths, +#one with seven + +tied_scores = ["zoos", "hex", "kittys"] + +tied_scores_seven = ["staring", "zzzzzx"] + +tied_scores_same_len = ["hex", "fox", "kittys", "zoos", "wix"] describe Scrabble::Scoring do it "should have a constant equal to a hash" do @@ -49,6 +56,18 @@ #separate tests with each tie breaker + it "should return the word with the fewest letters in case of tie" do + Scrabble::Scoring.highest_score_from(tied_scores).must_equal("hex") + end + + it "should return a seven-letter word in case of a tie" do + Scrabble::Scoring.highest_score_from(tied_scores_seven).must_equal("staring") + end + + it "should return the first word if all winning words are the same length" do + Scrabble::Scoring.highest_score_from(tied_scores_same_len).must_equal("hex") + end + end end From bfdf9f2c4a5548c9f675fdc6e72647958720fc71 Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Wed, 31 Aug 2016 11:26:12 -0700 Subject: [PATCH 12/28] added tests for invalid characters in self.score arguments --- scoring.rb | 20 ++++++++++++++------ specs/scoring_spec.rb | 6 +++--- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/scoring.rb b/scoring.rb index a6978469..7bc69855 100644 --- a/scoring.rb +++ b/scoring.rb @@ -11,18 +11,26 @@ class Scoring } def self.score(word) + score = 0 + user_word = word.upcase.split("") + if word.length < 1 || word.length > 7 raise ArgumentError.new("No empty strings") end - # raise ArgumentError.new("Invalid characters") + valid_letters = [] + LETTERS.values.each do |array| + array.each do |letter| + valid_letters << letter + end + end -# if !LETTERS.values.include?(word.split("")) -# puts "no!" -# end + user_word.each do |letter| + if !valid_letters.include?(letter) + raise ArgumentError.new("Invalid characters") + end + end - score = 0 - user_word = word.upcase.split("") user_word.each do |letter| LETTERS.each do |key, value| if value.include?(letter) diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 12eb77c9..cea68469 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -30,9 +30,9 @@ end end - # it "should raise an error if there are any non-letter characters in a word" do - # proc { Scrabble::Scoring.score("1de") }.must_raise(ArgumentError) - # end + it "should raise an error if there are any non-letter characters in a word" do + proc { Scrabble::Scoring.score("1de") }.must_raise(ArgumentError) + end it "must not have an empty string argument" do proc { Scrabble::Scoring.score("") }.must_raise(ArgumentError) From a3acc891376a4044278bf5cb0b29d6224b72b002 Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Wed, 31 Aug 2016 11:56:02 -0700 Subject: [PATCH 13/28] added SimpleCov to spec_helper --- specs/spec_helper.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 7b9117e1..ef5c926d 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1,3 +1,6 @@ +require 'simplecov' +SimpleCov.start + require 'minitest' require 'minitest/spec' require "minitest/autorun" From e79c4fad6ac77f23a47f5e47fc8ddda3b03237f4 Mon Sep 17 00:00:00 2001 From: Yeni Date: Wed, 31 Aug 2016 14:50:57 -0700 Subject: [PATCH 14/28] created player.rb and player_spec.rb / completed Player initialize play(word) methods --- player.rb | 20 +++++++++++++++++ specs/player_spec.rb | 53 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 player.rb create mode 100644 specs/player_spec.rb diff --git a/player.rb b/player.rb new file mode 100644 index 00000000..5e8b9bd1 --- /dev/null +++ b/player.rb @@ -0,0 +1,20 @@ +require_relative 'scoring' + +module Scrabble + class Player + attr_reader :name, :plays + def initialize(name) + @name = name + @plays = [] + end + + def play(word) + @plays << word + return Scrabble::Scoring.score(word) + end + + def total_score + + end + end +end diff --git a/specs/player_spec.rb b/specs/player_spec.rb new file mode 100644 index 00000000..e29d02fb --- /dev/null +++ b/specs/player_spec.rb @@ -0,0 +1,53 @@ +require_relative 'spec_helper' +require_relative '../player' + +describe Scrabble::Player do + + describe "#initialize" do + jane = Scrabble::Player.new("Jane") + + it "should create an instance of Player" do + jane.must_be_instance_of(Scrabble::Player) + end + + it "should be able to return player's name" do + jane.must_respond_to(:name) + end + + it "must be able to return player's plays" do + jane.must_respond_to(:plays) + end + end + + + describe "#play(word)" do + test_array = ["hex", "bats", "kittys"] + + it "should add player's words to @plays array" do + john = Scrabble::Player.new("John") + test_array.each do |word| + john.play(word) + end + john.plays.length.must_equal(3) + end + + it "should return score of word" do + john = Scrabble::Player.new("John") + john.play("fox").must_equal(13) + end + end + + describe "#total_score" do + other_test = ["kittys", "fox", "cat", "dog"] + it "should return sum of all words' scores" do + john = Scrabble::Player.new("John") + other_test.each do |word| + john.play(word) + end + john.total_score.must_equal(36) + end + + + end + +end From 3aff7f68012540ba0cb4db2432d4f25e20c6ad9a Mon Sep 17 00:00:00 2001 From: Yeni Date: Wed, 31 Aug 2016 14:59:50 -0700 Subject: [PATCH 15/28] completed total score implementation --- player.rb | 7 ++++++- specs/player_spec.rb | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/player.rb b/player.rb index 5e8b9bd1..ad70f7e3 100644 --- a/player.rb +++ b/player.rb @@ -14,7 +14,12 @@ def play(word) end def total_score - + score_sum = 0 + @plays.each do |word| + score = Scrabble::Scoring.score(word) + score_sum += score + end + return score_sum end end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index e29d02fb..db7ec9ac 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -46,8 +46,10 @@ end john.total_score.must_equal(36) end + end - + describe "#won?" do + end end From d201bf7f6383156edae6bd49941bcba97c4aae3d Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Wed, 31 Aug 2016 15:53:34 -0700 Subject: [PATCH 16/28] implemented won? and highest_scoring_word methods and tests --- player.rb | 18 ++++++++++++++++-- specs/player_spec.rb | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/player.rb b/player.rb index ad70f7e3..9cb2f055 100644 --- a/player.rb +++ b/player.rb @@ -9,8 +9,12 @@ def initialize(name) end def play(word) - @plays << word - return Scrabble::Scoring.score(word) + if won? + return false + else + @plays << word + return Scrabble::Scoring.score(word) + end end def total_score @@ -21,5 +25,15 @@ def total_score end return score_sum end + + def won? + total_score > 100 + end + + def highest_scoring_word + highest = Scrabble::Scoring.highest_score_from(@plays) + return highest + end + end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index db7ec9ac..b6213784 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -22,6 +22,7 @@ describe "#play(word)" do test_array = ["hex", "bats", "kittys"] + winning_array = ["staring", "zzzzzx"] it "should add player's words to @plays array" do john = Scrabble::Player.new("John") @@ -35,6 +36,15 @@ john = Scrabble::Player.new("John") john.play("fox").must_equal(13) end + + it "should return false if player has won" do + john = Scrabble::Player.new("John") + winning_array.each do |word| + john.play(word) + end + + john.play("fox").must_equal(false) + end end describe "#total_score" do @@ -49,7 +59,31 @@ end describe "#won?" do - + winning_array = ["staring", "zzzzzx"] + + it "should return true if score is > 100" do + john = Scrabble::Player.new("John") + + winning_array.each do |word| + john.play(word) + end + + john.won?.must_equal(true) + end + end + + describe "#highest_scoring_word" do + other_test = ["kittys", "fox", "cat", "dog"] # => "fox" + + it "should return the highest scoring word played" do + john = Scrabble::Player.new("John") + other_test.each do |word| + john.play(word) + end + + john.highest_scoring_word.must_equal("fox") + end + end end From 5a2538bbc5500f4cb7c8c1383068404fd2e573f0 Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Wed, 31 Aug 2016 16:08:09 -0700 Subject: [PATCH 17/28] Edited ArgError msg for word length --- scoring.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scoring.rb b/scoring.rb index 7bc69855..30847afd 100644 --- a/scoring.rb +++ b/scoring.rb @@ -15,7 +15,7 @@ def self.score(word) user_word = word.upcase.split("") if word.length < 1 || word.length > 7 - raise ArgumentError.new("No empty strings") + raise ArgumentError.new("Invalid word length") end valid_letters = [] From 4aa3f1b1223ae62524d2aec89eef0db5d8f5ad6d Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Wed, 31 Aug 2016 16:08:54 -0700 Subject: [PATCH 18/28] implemented highest_word_score and highest_scoring_word methods and tests. --- player.rb | 5 ++ specs/player_spec.rb | 129 +++++++++++++++++++++++-------------------- 2 files changed, 75 insertions(+), 59 deletions(-) diff --git a/player.rb b/player.rb index 9cb2f055..377d0200 100644 --- a/player.rb +++ b/player.rb @@ -35,5 +35,10 @@ def highest_scoring_word return highest end + def highest_word_score + highest = Scrabble::Scoring.highest_score_from(@plays) + highest_score = Scrabble::Scoring.score(highest) + return highest_score + end end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index b6213784..7ea51c8d 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -3,87 +3,98 @@ describe Scrabble::Player do - describe "#initialize" do - jane = Scrabble::Player.new("Jane") + describe "#initialize" do + jane = Scrabble::Player.new("Jane") - it "should create an instance of Player" do - jane.must_be_instance_of(Scrabble::Player) - end + it "should create an instance of Player" do + jane.must_be_instance_of(Scrabble::Player) + end - it "should be able to return player's name" do - jane.must_respond_to(:name) - end + it "should be able to return player's name" do + jane.must_respond_to(:name) + end - it "must be able to return player's plays" do - jane.must_respond_to(:plays) + it "must be able to return player's plays" do + jane.must_respond_to(:plays) + end end - end - describe "#play(word)" do - test_array = ["hex", "bats", "kittys"] - winning_array = ["staring", "zzzzzx"] + describe "#play(word)" do + test_array = ["hex", "bats", "kittys"] + winning_array = ["staring", "zzzzzx"] - it "should add player's words to @plays array" do - john = Scrabble::Player.new("John") - test_array.each do |word| - john.play(word) - end - john.plays.length.must_equal(3) - end - - it "should return score of word" do - john = Scrabble::Player.new("John") - john.play("fox").must_equal(13) - end + it "should add player's words to @plays array" do + john = Scrabble::Player.new("John") + test_array.each do |word| + john.play(word) + end + john.plays.length.must_equal(3) + end - it "should return false if player has won" do - john = Scrabble::Player.new("John") - winning_array.each do |word| - john.play(word) + it "should return score of word" do + john = Scrabble::Player.new("John") + john.play("fox").must_equal(13) end - john.play("fox").must_equal(false) + it "should return false if player has won" do + john = Scrabble::Player.new("John") + winning_array.each do |word| + john.play(word) + end + + john.play("fox").must_equal(false) + end end - end - - describe "#total_score" do - other_test = ["kittys", "fox", "cat", "dog"] - it "should return sum of all words' scores" do - john = Scrabble::Player.new("John") - other_test.each do |word| - john.play(word) - end - john.total_score.must_equal(36) + + describe "#total_score" do + other_test = ["kittys", "fox", "cat", "dog"] + it "should return sum of all words' scores" do + john = Scrabble::Player.new("John") + other_test.each do |word| + john.play(word) + end + john.total_score.must_equal(36) + end end - end - describe "#won?" do - winning_array = ["staring", "zzzzzx"] + describe "#won?" do + winning_array = ["staring", "zzzzzx"] - it "should return true if score is > 100" do - john = Scrabble::Player.new("John") + it "should return true if score is > 100" do + john = Scrabble::Player.new("John") - winning_array.each do |word| - john.play(word) - end + winning_array.each do |word| + john.play(word) + end - john.won?.must_equal(true) + john.won?.must_equal(true) + end end - end - describe "#highest_scoring_word" do - other_test = ["kittys", "fox", "cat", "dog"] # => "fox" + describe "#highest_scoring_word" do + other_test = ["kittys", "fox", "cat", "dog"] # => "fox" + + it "should return the highest scoring word played" do + john = Scrabble::Player.new("John") + other_test.each do |word| + john.play(word) + end - it "should return the highest scoring word played" do - john = Scrabble::Player.new("John") - other_test.each do |word| - john.play(word) + john.highest_scoring_word.must_equal("fox") end - john.highest_scoring_word.must_equal("fox") end - end + describe "#highest_word_score" do + it "must return the numerical score of highest_scoring_word" do + john = Scrabble::Player.new("John") + other_test = ["kittys", "fox", "cat", "dog"] + other_test.each do |word| + john.play(word) + end + john.highest_word_score.must_equal(13) + end + end end From c8994b7ad5f433b5128d98d7349b5372564a3fec Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Wed, 31 Aug 2016 16:25:24 -0700 Subject: [PATCH 19/28] added edge case tests for won? method (empty array and losing array) --- specs/player_spec.rb | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 7ea51c8d..1a64fd42 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -19,7 +19,6 @@ end end - describe "#play(word)" do test_array = ["hex", "bats", "kittys"] winning_array = ["staring", "zzzzzx"] @@ -60,6 +59,8 @@ describe "#won?" do winning_array = ["staring", "zzzzzx"] + empty_array =[] + losing_array = ["cat", "dog"] it "should return true if score is > 100" do john = Scrabble::Player.new("John") @@ -70,6 +71,24 @@ john.won?.must_equal(true) end + + it "should return false if empty" do + john = Scrabble::Player.new("John") + + empty_array.each do |thing| + john.play(thing) + end + + john.won?.must_equal(false) + end + + it "should return false if not winning" do + john = Scrabble::Player.new("John") + losing_array.each do |word| + john.play(word) + end + john.won?.must_equal(false) + end end describe "#highest_scoring_word" do From ef74c4127b9e40851aaa1d70978e4cae1fc588d4 Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Wed, 31 Aug 2016 16:56:38 -0700 Subject: [PATCH 20/28] added tests and handling for empty arrays in highest word score, highest scoring word --- player.rb | 17 ++++++++++++----- specs/player_spec.rb | 15 ++++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/player.rb b/player.rb index 377d0200..37909701 100644 --- a/player.rb +++ b/player.rb @@ -31,14 +31,21 @@ def won? end def highest_scoring_word - highest = Scrabble::Scoring.highest_score_from(@plays) - return highest + if @plays.empty? + return "NO WORDS PLAYED" + else + highest = Scrabble::Scoring.highest_score_from(@plays) + return highest + end end def highest_word_score - highest = Scrabble::Scoring.highest_score_from(@plays) - highest_score = Scrabble::Scoring.score(highest) - return highest_score + if @plays.empty? + return 0 + else + highest_score = Scrabble::Scoring.score(highest_scoring_word) + return highest_score + end end end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 1a64fd42..246a2734 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -74,11 +74,6 @@ it "should return false if empty" do john = Scrabble::Player.new("John") - - empty_array.each do |thing| - john.play(thing) - end - john.won?.must_equal(false) end @@ -103,6 +98,11 @@ john.highest_scoring_word.must_equal("fox") end + it "should return 'NO WORDS PLAYED' if no words have been played" do + john = Scrabble::Player.new("John") + john.highest_scoring_word.must_equal("NO WORDS PLAYED") + end + end describe "#highest_word_score" do @@ -114,6 +114,11 @@ end john.highest_word_score.must_equal(13) end + + it "should return 0 if no words in array" do + john = Scrabble::Player.new("John") + john.highest_word_score.must_equal(0) + end end end From 722d088c472188e1d03714353763c5fcb50ee268 Mon Sep 17 00:00:00 2001 From: Yeni Date: Thu, 1 Sep 2016 11:48:04 -0700 Subject: [PATCH 21/28] added test if no words have been played for total score --- specs/player_spec.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 246a2734..f062ede2 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -55,11 +55,16 @@ end john.total_score.must_equal(36) end + + it "should return 0 if no words have been played" do + john = Scrabble::Player.new("John") + john.total_score.must_equal(0) + + end end describe "#won?" do winning_array = ["staring", "zzzzzx"] - empty_array =[] losing_array = ["cat", "dog"] it "should return true if score is > 100" do From e7eb85b855cd741a181d65db2de5ef1b415d4dde Mon Sep 17 00:00:00 2001 From: Yeni Date: Thu, 1 Sep 2016 12:07:50 -0700 Subject: [PATCH 22/28] minor cleanup of player.rb and scoring.rb --- player.rb | 3 ++- scoring.rb | 14 ++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/player.rb b/player.rb index 37909701..53683907 100644 --- a/player.rb +++ b/player.rb @@ -13,7 +13,8 @@ def play(word) return false else @plays << word - return Scrabble::Scoring.score(word) + word_score = Scrabble::Scoring.score(word) + return word_score end end diff --git a/scoring.rb b/scoring.rb index 30847afd..56874be8 100644 --- a/scoring.rb +++ b/scoring.rb @@ -5,7 +5,7 @@ class Scoring 2 => %w(D G), 3 => %w(B C M P), 4 => %w(F H V W Y), - 5 => ["K"], + 5 => %w(K), 8 => %w(J X), 10 => %w(Q Z) } @@ -13,12 +13,8 @@ class Scoring def self.score(word) score = 0 user_word = word.upcase.split("") - - if word.length < 1 || word.length > 7 - raise ArgumentError.new("Invalid word length") - end - valid_letters = [] + LETTERS.values.each do |array| array.each do |letter| valid_letters << letter @@ -31,6 +27,10 @@ def self.score(word) end end + if word.length < 1 || word.length > 7 + raise ArgumentError.new("Invalid word length") + end + user_word.each do |letter| LETTERS.each do |key, value| if value.include?(letter) @@ -47,7 +47,9 @@ def self.score(word) def self.highest_score_from(array_of_words) scoring_hash = {} max_words = [] + raise ArgumentError.new("Invalid input type") if !array_of_words.is_a?(Array) + array_of_words.each do |word| scoring_hash[word] = Scrabble::Scoring.score(word) end From 68917238d9a704e1a127fe0cce36561c2bcdaa18 Mon Sep 17 00:00:00 2001 From: Yeni Date: Thu, 1 Sep 2016 12:32:01 -0700 Subject: [PATCH 23/28] cleaned up specs --- specs/player_spec.rb | 28 +++++++++++++++++----------- specs/scoring_spec.rb | 39 ++++++++++++++++----------------------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/specs/player_spec.rb b/specs/player_spec.rb index f062ede2..e2be384b 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -20,12 +20,12 @@ end describe "#play(word)" do - test_array = ["hex", "bats", "kittys"] - winning_array = ["staring", "zzzzzx"] it "should add player's words to @plays array" do john = Scrabble::Player.new("John") - test_array.each do |word| + played_words = ["hex", "bats", "kittys"] + + played_words.each do |word| john.play(word) end john.plays.length.must_equal(3) @@ -38,6 +38,8 @@ it "should return false if player has won" do john = Scrabble::Player.new("John") + winning_array = ["staring", "zzzzzx"] + winning_array.each do |word| john.play(word) end @@ -47,10 +49,12 @@ end describe "#total_score" do - other_test = ["kittys", "fox", "cat", "dog"] + it "should return sum of all words' scores" do john = Scrabble::Player.new("John") - other_test.each do |word| + animals = ["kittys", "fox", "cat", "dog"] + + animals.each do |word| john.play(word) end john.total_score.must_equal(36) @@ -64,11 +68,10 @@ end describe "#won?" do - winning_array = ["staring", "zzzzzx"] - losing_array = ["cat", "dog"] it "should return true if score is > 100" do john = Scrabble::Player.new("John") + winning_array = ["staring", "zzzzzx"] winning_array.each do |word| john.play(word) @@ -84,6 +87,8 @@ it "should return false if not winning" do john = Scrabble::Player.new("John") + losing_array = ["cat", "dog"] + losing_array.each do |word| john.play(word) end @@ -92,11 +97,12 @@ end describe "#highest_scoring_word" do - other_test = ["kittys", "fox", "cat", "dog"] # => "fox" it "should return the highest scoring word played" do john = Scrabble::Player.new("John") - other_test.each do |word| + animals = ["kittys", "fox", "cat", "dog"] # => "fox" + + animals.each do |word| john.play(word) end @@ -113,8 +119,8 @@ describe "#highest_word_score" do it "must return the numerical score of highest_scoring_word" do john = Scrabble::Player.new("John") - other_test = ["kittys", "fox", "cat", "dog"] - other_test.each do |word| + animals = ["kittys", "fox", "cat", "dog"] + animals.each do |word| john.play(word) end john.highest_word_score.must_equal(13) diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index cea68469..2372cb65 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -1,23 +1,6 @@ require_relative 'spec_helper' require_relative '../scoring' -known_words = { - "farm" => 9, # 4,1,1,3 - "cat" => 5, # 3,1,1 - "ferrets" => 60, # 50 + 4,1,1,1,1,1,1 -} - -test_words = ["farm", "cat", "ferrets"] - -#create test array with tied score words with different lengths, -#one with seven - -tied_scores = ["zoos", "hex", "kittys"] - -tied_scores_seven = ["staring", "zzzzzx"] - -tied_scores_same_len = ["hex", "fox", "kittys", "zoos", "wix"] - describe Scrabble::Scoring do it "should have a constant equal to a hash" do Scrabble::Scoring::LETTERS.must_be_instance_of(Hash) @@ -25,13 +8,22 @@ describe "self.score" do it "must correctly score known words" do + known_words = { + "farm" => 9, # 4,1,1,3 + "cat" => 5, # 3,1,1 + "ferrets" => 60, # 50 + 4,1,1,1,1,1,1 + } + known_words.each do |word, expected_score| Scrabble::Scoring.score(word).must_equal(expected_score) end end it "should raise an error if there are any non-letter characters in a word" do - proc { Scrabble::Scoring.score("1de") }.must_raise(ArgumentError) + invalid_chars = ["1de", "cat! 2", "com,ma"] + invalid_chars.each do |word| + proc { Scrabble::Scoring.score(word) }.must_raise(ArgumentError) + end end it "must not have an empty string argument" do @@ -50,22 +42,23 @@ end it "must return the word with the highest score" do - Scrabble::Scoring.highest_score_from(test_words).must_equal("ferrets") - + highest_score_array = ["farm", "cat", "ferrets"] + Scrabble::Scoring.highest_score_from(highest_score_array).must_equal("ferrets") end - #separate tests with each tie breaker - it "should return the word with the fewest letters in case of tie" do + tied_scores = ["zoos", "hex", "kittys"] Scrabble::Scoring.highest_score_from(tied_scores).must_equal("hex") end it "should return a seven-letter word in case of a tie" do + tied_scores_seven = ["staring", "zzzzzx", "cat"] Scrabble::Scoring.highest_score_from(tied_scores_seven).must_equal("staring") end it "should return the first word if all winning words are the same length" do - Scrabble::Scoring.highest_score_from(tied_scores_same_len).must_equal("hex") + same_len_tied = ["hex", "fox", "kittys", "zoos", "wix"] + Scrabble::Scoring.highest_score_from(same_len_tied).must_equal("hex") end end From e1d521498b733441b1a6f2f2ec9e3b5846ec86fa Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Thu, 1 Sep 2016 15:41:46 -0700 Subject: [PATCH 24/28] wrote tests for initalize and draw_tiles(num) - WIP. --- specs/tilebag_spec.rb | 65 +++++++++++++++++++++++++++++++++++++++++++ tilebag.rb | 36 ++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 specs/tilebag_spec.rb create mode 100644 tilebag.rb diff --git a/specs/tilebag_spec.rb b/specs/tilebag_spec.rb new file mode 100644 index 00000000..5b4f4440 --- /dev/null +++ b/specs/tilebag_spec.rb @@ -0,0 +1,65 @@ +require_relative 'spec_helper' +require_relative '../tilebag' + +describe Scrabble::TileBag do + describe "#initialize" do + + it "must create an instance of TileBag" do + new_game = Scrabble::TileBag.new + new_game.must_be_instance_of(Scrabble::TileBag) + end + + it "must be initialized with the proper distribution of letters" do + new_game = Scrabble::TileBag.new + tiles = { + "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 + } + + tiles.each do |key, value| + new_game.tiles[key].must_equal(value) + end + end + + it "must be initialized with all available letters" do + new_game = Scrabble::TileBag.new + available_tiles = ["A", "B", "C", "D", "E", + "F", "G", "H", "I", "J", "K", "L", "M", + "N", "O", "P", "Q", "R", "S", "T", "U", + "V", "W", "X", "Y", "Z"] + + new_game.available_tiles.must_equal(available_tiles) + end + end + + describe "#draw_tiles(num)" do + it "should raise an error when a non-Fixnum is passed in" do + new_game = Scrabble::TileBag.new + bad_args = ["a", 1.2, "2a", "2", "!"] + + bad_args.each do |item| + proc { new_game.draw_tiles(item) }.must_raise(ArgumentError) + end + end + + it "should return an array containing num tiles" do + new_game = Scrabble::TileBag.new + new_game.draw_tiles(3).length.must_equal(3) + end + + it "should remove the tiles" do + + end + end +end diff --git a/tilebag.rb b/tilebag.rb new file mode 100644 index 00000000..9ee3fe5b --- /dev/null +++ b/tilebag.rb @@ -0,0 +1,36 @@ +module Scrabble + class TileBag + attr_reader :tiles + attr_accessor :available_tiles + + def initialize + @tiles = { + "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 + } + + @available_tiles = @tiles.keys + + end + + def draw_tiles(num) + raise ArgumentError.new("Invalid argument type") if !num.is_a?(Fixnum) + + # remove tiles from @available_tiles if @tiles[key] == 0 + # decrement value when a tile key is selected + # return [num_tiles] + return [3,3,3] + end + end +end From eba0faa54109e2dd42d9da163fb1120f265fbfa4 Mon Sep 17 00:00:00 2001 From: Yeni Date: Fri, 2 Sep 2016 12:24:10 -0700 Subject: [PATCH 25/28] finished draw_tiles(num) method definition and tests --- specs/tilebag_spec.rb | 31 +++++++++++++++++++++---------- tilebag.rb | 17 ++++++++++++----- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/specs/tilebag_spec.rb b/specs/tilebag_spec.rb index 5b4f4440..7bdd2463 100644 --- a/specs/tilebag_spec.rb +++ b/specs/tilebag_spec.rb @@ -32,15 +32,15 @@ end end - it "must be initialized with all available letters" do - new_game = Scrabble::TileBag.new - available_tiles = ["A", "B", "C", "D", "E", - "F", "G", "H", "I", "J", "K", "L", "M", - "N", "O", "P", "Q", "R", "S", "T", "U", - "V", "W", "X", "Y", "Z"] - - new_game.available_tiles.must_equal(available_tiles) - end + # it "must be initialized with all available letters" do + # new_game = Scrabble::TileBag.new + # available_tiles = ["A", "B", "C", "D", "E", + # "F", "G", "H", "I", "J", "K", "L", "M", + # "N", "O", "P", "Q", "R", "S", "T", "U", + # "V", "W", "X", "Y", "Z"] + # + # new_game.available_tiles.must_equal(available_tiles) + # end end describe "#draw_tiles(num)" do @@ -58,8 +58,19 @@ new_game.draw_tiles(3).length.must_equal(3) end - it "should remove the tiles" do + it "should not pick a tile[key] that has tile[value] = 0" do + new_game = Scrabble::TileBag.new + new_game.draw_tiles(98) #98 is total num of tiles + all_tiles = new_game.tiles.values.reduce(:+) + all_tiles.must_equal(0) + end + it "should decrease the num of available tiles inside tiles hash" do + new_game = Scrabble::TileBag.new + original_values = new_game.tiles.values.clone + new_game.draw_tiles(1) + new_game.tiles.values.wont_equal(original_values) end end + end diff --git a/tilebag.rb b/tilebag.rb index 9ee3fe5b..8083c61e 100644 --- a/tilebag.rb +++ b/tilebag.rb @@ -20,17 +20,24 @@ def initialize "Y" => 2, "Z" => 1 } - @available_tiles = @tiles.keys + # @available_tiles = @tiles.keys end def draw_tiles(num) raise ArgumentError.new("Invalid argument type") if !num.is_a?(Fixnum) - # remove tiles from @available_tiles if @tiles[key] == 0 - # decrement value when a tile key is selected - # return [num_tiles] - return [3,3,3] + pick = [] + until pick.length == num + tile = @tiles.keys.sample + until @tiles[tile] != 0 + tile = @tiles.keys.sample + end + pick << tile + @tiles[tile] -= 1 + end + + return pick end end end From a274903666cd16990f8b0479e5334d73363789e2 Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Fri, 2 Sep 2016 13:56:08 -0700 Subject: [PATCH 26/28] implemented tiles_remaining and modified draw_tiles method to prevent infinite loops. --- specs/tilebag_spec.rb | 14 ++++++++++++++ tilebag.rb | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/specs/tilebag_spec.rb b/specs/tilebag_spec.rb index 7bdd2463..1fd1268e 100644 --- a/specs/tilebag_spec.rb +++ b/specs/tilebag_spec.rb @@ -71,6 +71,20 @@ new_game.draw_tiles(1) new_game.tiles.values.wont_equal(original_values) end + + it "should return remaining tiles if num is greater than remaining tiles" do + new_game = Scrabble::TileBag.new + new_game.draw_tiles(96) + new_game.draw_tiles(3).length.must_equal(2) # Only two tiles left + end end + describe "#tiles_remaining" do + + it "must return the numbers of tiles left in tilebag" do + another_game = Scrabble::TileBag.new + another_game.draw_tiles(2) + another_game.tiles_remaining.must_equal(96) + end + end end diff --git a/tilebag.rb b/tilebag.rb index 8083c61e..649496cf 100644 --- a/tilebag.rb +++ b/tilebag.rb @@ -28,6 +28,9 @@ def draw_tiles(num) raise ArgumentError.new("Invalid argument type") if !num.is_a?(Fixnum) pick = [] + if num > tiles_remaining + num = tiles_remaining + end until pick.length == num tile = @tiles.keys.sample until @tiles[tile] != 0 @@ -39,5 +42,10 @@ def draw_tiles(num) return pick end + + def tiles_remaining + remaining_val = @tiles.values.reduce(:+) + return remaining_val + end end end From cffd278574e8fb09f3da8df169b352e041b5e6e4 Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Fri, 2 Sep 2016 15:11:47 -0700 Subject: [PATCH 27/28] updated Player specs and class to include tiles and draw_tiles(tile_bag) --- player.rb | 100 ++++++++++++++++++++++++++----------------- specs/player_spec.rb | 19 ++++++++ 2 files changed, 79 insertions(+), 40 deletions(-) diff --git a/player.rb b/player.rb index 53683907..19a769d9 100644 --- a/player.rb +++ b/player.rb @@ -1,52 +1,72 @@ require_relative 'scoring' module Scrabble - class Player - attr_reader :name, :plays - def initialize(name) - @name = name - @plays = [] - end + class Player + MAX_TILES = 7 - def play(word) - if won? - return false - else - @plays << word - word_score = Scrabble::Scoring.score(word) - return word_score - end - end + attr_reader :name, :plays, :tiles - def total_score - score_sum = 0 - @plays.each do |word| - score = Scrabble::Scoring.score(word) - score_sum += score - end - return score_sum - end + def initialize(name) + @name = name + @plays = [] + @tiles = [] - def won? - total_score > 100 - end +# if @tiles.length > MAX_TILES +# raise ArgumentError.new("Too many tiles") +# end + end - def highest_scoring_word - if @plays.empty? - return "NO WORDS PLAYED" - else - highest = Scrabble::Scoring.highest_score_from(@plays) - return highest + def play(word) + if won? + return false + else + @plays << word + word_score = Scrabble::Scoring.score(word) + return word_score + end end - end - def highest_word_score - if @plays.empty? - return 0 - else - highest_score = Scrabble::Scoring.score(highest_scoring_word) - return highest_score + def total_score + score_sum = 0 + @plays.each do |word| + score = Scrabble::Scoring.score(word) + score_sum += score + end + return score_sum + end + + def won? + total_score > 100 + end + + def highest_scoring_word + if @plays.empty? + return "NO WORDS PLAYED" + else + highest = Scrabble::Scoring.highest_score_from(@plays) + return highest + end + end + + def highest_word_score + if @plays.empty? + return 0 + else + highest_score = Scrabble::Scoring.score(highest_scoring_word) + return highest_score + end + end + + def draw_tiles(tile_bag) + difference = MAX_TILES - @tiles.length + holding = [] + if @tiles.length < MAX_TILES + holding = tile_bag.draw_tiles(difference) + end + + holding.each do |letter| + @tiles << letter + end end end - end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index e2be384b..697503ca 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -1,5 +1,6 @@ require_relative 'spec_helper' require_relative '../player' +require_relative '../tilebag' describe Scrabble::Player do @@ -17,6 +18,16 @@ it "must be able to return player's plays" do jane.must_respond_to(:plays) end + + it "must be able to return player's tiles" do + jane.must_respond_to(:tiles) + end + +# it "must not have more than 7 tiles" do +# kelly = Scrabble::Player.new("Kelly") +# proc { kelly.tiles = ["a", "b", "c", "d", "e", "f", "g", "h"] }.must_raise(ArgumentError) +# end + end describe "#play(word)" do @@ -132,4 +143,12 @@ end end + describe "draw_tiles(tile_bag)" do + it "must fill tile array until tiles is seven letters" do + noelle = Scrabble::Player.new("Noelle") + tile_bag = Scrabble::TileBag.new + noelle.draw_tiles(tile_bag) + noelle.tiles.length.must_equal(7) + end + end end From 7f53b31a617b7166d7379ab11eb3852e43b7627b Mon Sep 17 00:00:00 2001 From: "Johna N. Morris" Date: Fri, 2 Sep 2016 15:14:36 -0700 Subject: [PATCH 28/28] minor cleanup of unnecessary comments --- specs/tilebag_spec.rb | 10 ---------- tilebag.rb | 2 -- 2 files changed, 12 deletions(-) diff --git a/specs/tilebag_spec.rb b/specs/tilebag_spec.rb index 1fd1268e..f4611ed8 100644 --- a/specs/tilebag_spec.rb +++ b/specs/tilebag_spec.rb @@ -31,16 +31,6 @@ new_game.tiles[key].must_equal(value) end end - - # it "must be initialized with all available letters" do - # new_game = Scrabble::TileBag.new - # available_tiles = ["A", "B", "C", "D", "E", - # "F", "G", "H", "I", "J", "K", "L", "M", - # "N", "O", "P", "Q", "R", "S", "T", "U", - # "V", "W", "X", "Y", "Z"] - # - # new_game.available_tiles.must_equal(available_tiles) - # end end describe "#draw_tiles(num)" do diff --git a/tilebag.rb b/tilebag.rb index 649496cf..6757722e 100644 --- a/tilebag.rb +++ b/tilebag.rb @@ -20,8 +20,6 @@ def initialize "Y" => 2, "Z" => 1 } - # @available_tiles = @tiles.keys - end def draw_tiles(num)