From f7c64f07a4f0e7a9178251db8c6b0faf4046bd10 Mon Sep 17 00:00:00 2001 From: Leah Date: Mon, 7 Mar 2016 16:50:50 -0800 Subject: [PATCH 01/33] trying to commit entire directory --- .ruby-gemset | 1 + .ruby-version | 1 + 2 files changed, 2 insertions(+) create mode 100644 .ruby-gemset create mode 100644 .ruby-version diff --git a/.ruby-gemset b/.ruby-gemset new file mode 100644 index 00000000..b0582e1f --- /dev/null +++ b/.ruby-gemset @@ -0,0 +1 @@ +Scrabble diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..276cbf9e --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.3.0 From 2e5a8eae453d558a954b56ed2939448935c39dd9 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Mon, 7 Mar 2016 16:52:59 -0800 Subject: [PATCH 02/33] Created spec/lib folder and respective files --- Scrabble.rb | 0 lib/player.rb | 0 lib/scoring.rb | 0 specs/player_spec.rb | 0 specs/scoring_spec.rb | 0 5 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 Scrabble.rb create mode 100644 lib/player.rb create mode 100644 lib/scoring.rb create mode 100644 specs/player_spec.rb create mode 100644 specs/scoring_spec.rb diff --git a/Scrabble.rb b/Scrabble.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/player.rb b/lib/player.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/scoring.rb b/lib/scoring.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/player_spec.rb b/specs/player_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb new file mode 100644 index 00000000..e69de29b From 6d22def6099adb85cbaa96ef00cced45daa27834 Mon Sep 17 00:00:00 2001 From: Leah Date: Mon, 7 Mar 2016 17:04:15 -0800 Subject: [PATCH 03/33] Added Rakefile and first test, made first test pass --- Rakefile | 8 ++++++++ Scrabble.rb | 2 ++ specs/scoring_spec.rb | 9 +++++++++ specs/spec_helper.rb | 7 +++++++ 4 files changed, 26 insertions(+) create mode 100644 Rakefile create mode 100644 specs/spec_helper.rb diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..f10c9770 --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +require 'rake/testtask' +Rake::TestTask.new do |t| + t.libs = ["lib"] + t.warning = true + t.test_files = FileList['specs/*_spec.rb'] +end + +task default: :test \ No newline at end of file diff --git a/Scrabble.rb b/Scrabble.rb index e69de29b..f03bab98 100644 --- a/Scrabble.rb +++ b/Scrabble.rb @@ -0,0 +1,2 @@ +class Scoring +end \ No newline at end of file diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index e69de29b..58975ffc 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -0,0 +1,9 @@ +require_relative './spec_helper' +require_relative '../scrabble' + +# write a failing test!!! +describe Scoring do + it "Is there a class? Anything?" do + Scoring.wont_be_nil + end +end \ No newline at end of file diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..3f6632d9 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,7 @@ +require 'minitest' +require 'minitest/spec' +require 'minitest/autorun' # run all tests +require 'minitest/reporters' # bring in extra reports from minireporters gem + +# give us some really pretty output +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From 1102970bbe741db3539aa5c306fef8455196759d Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Tue, 8 Mar 2016 10:08:44 -0800 Subject: [PATCH 04/33] Required simplecov in spec helper. Required player and scoring into scrabble.rb. --- Scrabble.rb | 6 ++++-- lib/player.rb | 8 ++++++++ lib/scoring.rb | 8 ++++++++ specs/spec_helper.rb | 3 +++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Scrabble.rb b/Scrabble.rb index f03bab98..6e760cf6 100644 --- a/Scrabble.rb +++ b/Scrabble.rb @@ -1,2 +1,4 @@ -class Scoring -end \ No newline at end of file +module Scrabble + class Scoring + end +end diff --git a/lib/player.rb b/lib/player.rb index e69de29b..1ef4bfd0 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -0,0 +1,8 @@ +require '../scrabble' + +class Player + include Scrabble + def initialize(file) + @scrabble_file = import(file) + end +end diff --git a/lib/scoring.rb b/lib/scoring.rb index e69de29b..5008d20f 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -0,0 +1,8 @@ +require '../scrabble' + +class Scoring + include Scrabble + def initialize(file) + @scrabble_file = import(file) + end +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 3f6632d9..6d18d736 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' # run all tests From 507e44dcec2794df515681157a512fdd653a322a Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Tue, 8 Mar 2016 10:11:36 -0800 Subject: [PATCH 05/33] Last commit was not up to date. --- Scrabble.rb | 19 +++++++++++++++++-- lib/player.rb | 10 ++++------ lib/scoring.rb | 7 +------ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Scrabble.rb b/Scrabble.rb index 6e760cf6..e18a1c1e 100644 --- a/Scrabble.rb +++ b/Scrabble.rb @@ -1,4 +1,19 @@ +require './lib/player' +require './lib/scoring' + module Scrabble - class Scoring - end + + # class Player + # include Player + # def initialize(file) + # @player_file = import(file) + # end + # end + # + # class Scoring + # include Scoring + # def initialize(file) + # @scoring_file = import(file) + # end + # end end diff --git a/lib/player.rb b/lib/player.rb index 1ef4bfd0..b2be304d 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,8 +1,6 @@ -require '../scrabble' - class Player - include Scrabble - def initialize(file) - @scrabble_file = import(file) - end + # include Scrabble + # def initialize(file) + # @scrabble_file = import(file) + # end end diff --git a/lib/scoring.rb b/lib/scoring.rb index 5008d20f..1e51938f 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,8 +1,3 @@ -require '../scrabble' +module Scoring -class Scoring - include Scrabble - def initialize(file) - @scrabble_file = import(file) - end end From 44ecb0cd776d71031cec1dc2904dff1ee18aa51e Mon Sep 17 00:00:00 2001 From: Leah Date: Tue, 8 Mar 2016 10:50:06 -0800 Subject: [PATCH 06/33] Added a hash to store values and letters, we still don't think the files are set up right.. --- Scrabble.rb | 15 +-------------- lib/player.rb | 4 +++- lib/scoring.rb | 27 ++++++++++++++++++++++++++- specs/player_spec.rb | 9 +++++++++ specs/scoring_spec.rb | 12 +++++++++++- 5 files changed, 50 insertions(+), 17 deletions(-) diff --git a/Scrabble.rb b/Scrabble.rb index e18a1c1e..3042b20e 100644 --- a/Scrabble.rb +++ b/Scrabble.rb @@ -2,18 +2,5 @@ require './lib/scoring' module Scrabble - - # class Player - # include Player - # def initialize(file) - # @player_file = import(file) - # end - # end - # - # class Scoring - # include Scoring - # def initialize(file) - # @scoring_file = import(file) - # end - # end + end diff --git a/lib/player.rb b/lib/player.rb index b2be304d..a99f13a7 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,4 +1,6 @@ -class Player +require_relative '../scrabble' + +class Scrabble::Player # include Scrabble # def initialize(file) # @scrabble_file = import(file) diff --git a/lib/scoring.rb b/lib/scoring.rb index 1e51938f..5485b92c 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,3 +1,28 @@ -module Scoring +require_relative '../scrabble' +class Scrabble::Scoring + # letters and corresponding values for scoring the words + LETTERS = { + 1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], + 2 => ["D", "G"], + 3 => ["B", "C", "M", "P"], + 4 => ["F", "H", "V", "W", "Y"], + 5 => ["K"], + 8 => ["J", "X"], + 10 => ["Q", "Z"] + } + + def self.score(word) + score = 0 + word.each |letter| do + LETTERS.each |k, v| do + if v.include?(letter) + score += k + end + end + end + end + + # def self.highest_score_from(array_of_words) + # end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index e69de29b..54b7f1b1 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -0,0 +1,9 @@ +require_relative './spec_helper' +require_relative '../scrabble' + +# write a failing test!!! +describe Player do + it "Is there a class? Anything?" do + Player.wont_be_nil + end +end \ No newline at end of file diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 58975ffc..8e31c206 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -6,4 +6,14 @@ it "Is there a class? Anything?" do Scoring.wont_be_nil end -end \ No newline at end of file + +describe "Scoring#score" do + it "Should generate the score of word being 8" do + self.score("WORD").must_equal(8) + end +end + + # it "Is there a method?" do + # self.highest_score_from(["a", "b", "x"]).wont_be_nil + # end +#end \ No newline at end of file From 75769344ff71497f7ef2097cecea454540f9cec6 Mon Sep 17 00:00:00 2001 From: Leah Date: Tue, 8 Mar 2016 10:53:54 -0800 Subject: [PATCH 07/33] required ./scrabble in the scoring and player rb files --- lib/player.rb | 2 +- lib/scoring.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index a99f13a7..1e8fc7dd 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,4 +1,4 @@ -require_relative '../scrabble' +require '../scrabble' class Scrabble::Player # include Scrabble diff --git a/lib/scoring.rb b/lib/scoring.rb index 5485b92c..055d910b 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,4 +1,4 @@ -require_relative '../scrabble' +require '../scrabble' class Scrabble::Scoring # letters and corresponding values for scoring the words LETTERS = { From 9a5aa159d1d793ae8efeb1ee6acb2e61efaf5034 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Tue, 8 Mar 2016 11:19:16 -0800 Subject: [PATCH 08/33] trying to get rid of require error messages with jeremy. this sucks --- Scrabble.rb | 6 +++--- lib/player.rb | 14 +++++++------- lib/scoring.rb | 10 +++++++--- specs/player_spec.rb | 14 +++++++------- specs/scoring_spec.rb | 31 +++++++++++++++++++------------ 5 files changed, 43 insertions(+), 32 deletions(-) diff --git a/Scrabble.rb b/Scrabble.rb index 3042b20e..4c32a4ed 100644 --- a/Scrabble.rb +++ b/Scrabble.rb @@ -1,6 +1,6 @@ -require './lib/player' -require './lib/scoring' module Scrabble - end + +require './lib/scoring' +# require_relative './lib/player' diff --git a/lib/player.rb b/lib/player.rb index 1e8fc7dd..1b46271b 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,8 +1,8 @@ -require '../scrabble' +# require '../Scrabble' -class Scrabble::Player - # include Scrabble - # def initialize(file) - # @scrabble_file = import(file) - # end -end +# class Scrabble::Player +# # include Scrabble +# # def initialize(file) +# # @scrabble_file = import(file) +# # end +# end diff --git a/lib/scoring.rb b/lib/scoring.rb index 055d910b..1ef4fd32 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,4 +1,4 @@ -require '../scrabble' +require '../Scrabble' class Scrabble::Scoring # letters and corresponding values for scoring the words LETTERS = { @@ -11,10 +11,14 @@ class Scrabble::Scoring 10 => ["Q", "Z"] } + def self.test + puts "MEOW" + end + def self.score(word) score = 0 - word.each |letter| do - LETTERS.each |k, v| do + word.each do |letter| + LETTERS.each do |k, v| if v.include?(letter) score += k end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 54b7f1b1..703f5996 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -1,9 +1,9 @@ -require_relative './spec_helper' -require_relative '../scrabble' +# require_relative './spec_helper' +# require_relative '../Scrabble' # write a failing test!!! -describe Player do - it "Is there a class? Anything?" do - Player.wont_be_nil - end -end \ No newline at end of file +# describe Player do +# it "Is there a class? Anything?" do +# Player.wont_be_nil +# end +# end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 8e31c206..44069528 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -1,19 +1,26 @@ require_relative './spec_helper' -require_relative '../scrabble' +require_relative '../Scrabble' # write a failing test!!! -describe Scoring do - it "Is there a class? Anything?" do - Scoring.wont_be_nil - end - -describe "Scoring#score" do - it "Should generate the score of word being 8" do - self.score("WORD").must_equal(8) - end -end +# describe Scoring do +# it "Is there a class? Anything?" do +# Scoring.wont_be_nil +# end +# +# # describe "Scoring#score" do +# # it "Should generate the score of word being 8" do +# # self.score("WORD").must_equal(8) +# # end +# # end +# +# describe "Scoring#test" do +# it "Should generate 'MEOW'" do +# Scoring.score.must_equal("MEOW") +# end +# end +# end # it "Is there a method?" do # self.highest_score_from(["a", "b", "x"]).wont_be_nil # end -#end \ No newline at end of file +#end From e031d9ab2a587fa0f239ef95122cf56d8798946a Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Tue, 8 Mar 2016 11:20:57 -0800 Subject: [PATCH 09/33] changed capital S to lowercase.rb --- Scrabble.rb => scrabble.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Scrabble.rb => scrabble.rb (100%) diff --git a/Scrabble.rb b/scrabble.rb similarity index 100% rename from Scrabble.rb rename to scrabble.rb From 1739dc6fbc9227f643c6d535c27a46a64a557fbc Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Tue, 8 Mar 2016 11:33:25 -0800 Subject: [PATCH 10/33] fixed require errors by taking out require scrabble in scoring.rb. Made .test method return MEOW. Figured out how to effectively run all test files and methods. --- lib/scoring.rb | 6 +++--- scrabble.rb | 2 +- specs/scoring_spec.rb | 36 +++++++++++++++++++----------------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 1ef4fd32..af1a2f02 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,4 +1,4 @@ -require '../Scrabble' +# require_relative '../scrabble' class Scrabble::Scoring # letters and corresponding values for scoring the words LETTERS = { @@ -12,12 +12,12 @@ class Scrabble::Scoring } def self.test - puts "MEOW" + return "MEOW" end def self.score(word) score = 0 - word.each do |letter| + word.each do |letter| #maybe it's not stepping through each letter. Check after lunch LETTERS.each do |k, v| if v.include?(letter) score += k diff --git a/scrabble.rb b/scrabble.rb index 4c32a4ed..6913f894 100644 --- a/scrabble.rb +++ b/scrabble.rb @@ -2,5 +2,5 @@ module Scrabble end -require './lib/scoring' +require_relative './lib/scoring' # require_relative './lib/player' diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 44069528..bab1afb7 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -1,23 +1,25 @@ require_relative './spec_helper' -require_relative '../Scrabble' +require_relative '../scrabble' # write a failing test!!! -# describe Scoring do -# it "Is there a class? Anything?" do -# Scoring.wont_be_nil -# end -# -# # describe "Scoring#score" do -# # it "Should generate the score of word being 8" do -# # self.score("WORD").must_equal(8) -# # end -# # end -# -# describe "Scoring#test" do -# it "Should generate 'MEOW'" do -# Scoring.score.must_equal("MEOW") -# end -# end +describe Scrabble::Scoring do + it "Is there a class? Anything?" do + Scrabble::Scoring.wont_be_nil + end + + describe "Scrabble::Scoring#score" do + it "Should generate the score of word being 8" do + Scrabble::Scoring.score("WORD").must_equal(8) + end + end + + describe "Scrabble::Scoring#test" do + it "Should generate 'MEOW'" do + Scrabble::Scoring.test.must_equal("MEOW") + end + end +end + # end # it "Is there a method?" do From 38ff41ae422b386671177da079fb80cc4aa94ab5 Mon Sep 17 00:00:00 2001 From: Leah Date: Tue, 8 Mar 2016 13:08:10 -0800 Subject: [PATCH 11/33] got self.score test passing, method is generating score for single word --- lib/scoring.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index af1a2f02..748149a9 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -17,13 +17,14 @@ def self.test def self.score(word) score = 0 - word.each do |letter| #maybe it's not stepping through each letter. Check after lunch + word.each_char do |letter| LETTERS.each do |k, v| if v.include?(letter) score += k end end end + return score end # def self.highest_score_from(array_of_words) From cf4f49c63fb1478fae4862c58983cf320a016ab8 Mon Sep 17 00:00:00 2001 From: Leah Date: Tue, 8 Mar 2016 13:36:52 -0800 Subject: [PATCH 12/33] created method and test for highest_score_from which takes in array, creates scores for each word in array and returns the highest scored word --- lib/scoring.rb | 16 ++++++++++++++-- specs/scoring_spec.rb | 22 +++++++++++++--------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 748149a9..d7aa1ef9 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -16,6 +16,7 @@ def self.test end def self.score(word) + word.upcase! score = 0 word.each_char do |letter| LETTERS.each do |k, v| @@ -27,7 +28,18 @@ def self.score(word) return score end - # def self.highest_score_from(array_of_words) - # end + def self.highest_score_from(array_of_words) + temp_array = [0, "word"] + loop_score = 0 + temp_score = 0 + array_of_words.each do |word| + temp_score = Scrabble::Scoring.score(word) + if temp_score > loop_score + loop_score = temp_array[0] + temp_array = [loop_score, word] + end + end + return temp_array[1] + end end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index bab1afb7..0980c93e 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -8,9 +8,20 @@ end describe "Scrabble::Scoring#score" do - it "Should generate the score of word being 8" do + it "WORD should generate the score of word being 8" do Scrabble::Scoring.score("WORD").must_equal(8) end + + it "word should generate the score of word being 8" do + Scrabble::Scoring.score("word").must_equal(8) + end + end + + describe "Scrabble::Scoring#highest_score_from" do + + it "returns the word in array with the highest score" do + Scrabble::Scoring.highest_score_from(["WORD", "CAT", "MEOW", "ZEBRA"]).must_equal("ZEBRA") + end end describe "Scrabble::Scoring#test" do @@ -18,11 +29,4 @@ Scrabble::Scoring.test.must_equal("MEOW") end end -end - -# end - - # it "Is there a method?" do - # self.highest_score_from(["a", "b", "x"]).wont_be_nil - # end -#end +end \ No newline at end of file From 4db068863c5c9a2605cff614342b0978be72fdae Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Tue, 8 Mar 2016 14:04:54 -0800 Subject: [PATCH 13/33] reworked highest_score_from to store score and words in separate arrays, then zip them and sort. And dropped all arrays that were not equal to the highest score. --- lib/scoring.rb | 35 ++++++++++++++++++++++++++--------- specs/scoring_spec.rb | 9 +++++++-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index d7aa1ef9..4c978bf8 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -18,7 +18,7 @@ def self.test def self.score(word) word.upcase! score = 0 - word.each_char do |letter| + word.each_char do |letter| LETTERS.each do |k, v| if v.include?(letter) score += k @@ -29,17 +29,34 @@ def self.score(word) end def self.highest_score_from(array_of_words) - temp_array = [0, "word"] - loop_score = 0 - temp_score = 0 + score_array = [] + word_array = [] + array_of_words.each do |word| temp_score = Scrabble::Scoring.score(word) - if temp_score > loop_score - loop_score = temp_array[0] - temp_array = [loop_score, word] - end + score_array << temp_score + word_array << word end - return temp_array[1] + + total_array = score_array.zip(word_array) + sorted_array = total_array.sort_by{|score| score[0]} + highest_score = sorted_array[-1][0] + + return sorted_array.drop_while{|array| array[0] != highest_score} + + + # temp_array = [0, "word"] + # loop_score = 0 + # temp_score = 0 + # array_of_words.each do |word| + # temp_score = Scrabble::Scoring.score(word) + # + # if temp_score > loop_score + # loop_score = temp_array[0] + # temp_array = [loop_score, word] + # end + # end + # return temp_array[1] end end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 0980c93e..8ee768a9 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -19,9 +19,14 @@ describe "Scrabble::Scoring#highest_score_from" do - it "returns the word in array with the highest score" do + it "returns the word in array with the highest score" do Scrabble::Scoring.highest_score_from(["WORD", "CAT", "MEOW", "ZEBRA"]).must_equal("ZEBRA") end + + it "returns an error message" do + Scrabble::Scoring.highest_score_from(["WORD", "CAT", "ZEBRE", "ZEBRA"]).must_be_nil + end + end describe "Scrabble::Scoring#test" do @@ -29,4 +34,4 @@ Scrabble::Scoring.test.must_equal("MEOW") end end -end \ No newline at end of file +end From 99879262cc4872bba68d88b28408dd787fb92c05 Mon Sep 17 00:00:00 2001 From: Leah Date: Tue, 8 Mar 2016 14:44:02 -0800 Subject: [PATCH 14/33] Added conditional to deal with same length words, returns word which appeared first in original array of words as winner --- lib/scoring.rb | 28 +++++++++++++++++++++++++++- specs/scoring_spec.rb | 8 ++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 4c978bf8..262aaefb 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -42,7 +42,33 @@ def self.highest_score_from(array_of_words) sorted_array = total_array.sort_by{|score| score[0]} highest_score = sorted_array[-1][0] - return sorted_array.drop_while{|array| array[0] != highest_score} + tie_array = sorted_array.delete_if{|array| array[0] != highest_score} + if tie_array.length == 1 + return tie_array[0][1] + else + tie_array.each do |array| + len = array[1].length + array[0] = len + end + end + + sorted_tie_array = tie_array.sort_by { |array| array[0] } + shortest_word = sorted_tie_array[0][0] + shortest_word_array = sorted_tie_array.delete_if{|array| array[0] != shortest_word } + + if shortest_word_array.length == 1 + return shortest_word_array[0][1] + # else there are multiple words with the same length + else + array_of_words.each do |word| + shortest_word_array.each do |array| + if word == array[1] + return array[1] + end + end + end + end + # temp_array = [0, "word"] diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 8ee768a9..3fc54e2d 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -19,12 +19,12 @@ describe "Scrabble::Scoring#highest_score_from" do - it "returns the word in array with the highest score" do - Scrabble::Scoring.highest_score_from(["WORD", "CAT", "MEOW", "ZEBRA"]).must_equal("ZEBRA") - end + # it "returns the word in array with the highest score" do + # Scrabble::Scoring.highest_score_from(["WORD", "CAT", "MEOW", "ZEBRA"]).must_equal("ZEBRA") + # end it "returns an error message" do - Scrabble::Scoring.highest_score_from(["WORD", "CAT", "ZEBRE", "ZEBRA"]).must_be_nil + Scrabble::Scoring.highest_score_from(["WORD", "QRSTLNE", "ZEBRE", "ZEBRA"]).must_equal("ZEBRE") end end From a4826c65b2f0551f676efcb8544d3b67a7ad4bbe Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Tue, 8 Mar 2016 15:20:22 -0800 Subject: [PATCH 15/33] Accounted for 7 letter words in both scoring methods. Commented code. A --- lib/scoring.rb | 32 ++++++++++++++++++++++++++------ specs/scoring_spec.rb | 19 ++++++++++++++----- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 262aaefb..db6bcbff 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -25,6 +25,11 @@ def self.score(word) end end end + + if word.length == 7 + score += 50 + end + return score end @@ -32,34 +37,49 @@ def self.highest_score_from(array_of_words) score_array = [] word_array = [] + # creates separate score and word arrays based on array input array_of_words.each do |word| temp_score = Scrabble::Scoring.score(word) score_array << temp_score word_array << word end - total_array = score_array.zip(word_array) + # combines the score and word array AND sorts + total_array = score_array.zip(word_array) # => [[score,word]] sorted_array = total_array.sort_by{|score| score[0]} - highest_score = sorted_array[-1][0] + highest_score = sorted_array[-1][0] # gets highest score + # deletes sub-arrays that are not equal to the highest score tie_array = sorted_array.delete_if{|array| array[0] != highest_score} + # if there's one word left if tie_array.length == 1 - return tie_array[0][1] - else + return tie_array[0][1] # this is the answer + # if there's a tie, reassign score to equal length of word => [[length,word]] + else tie_array.each do |array| len = array[1].length - array[0] = len + array[0] = len end end + # sorts the tied array by length sorted_tie_array = tie_array.sort_by { |array| array[0] } + # if there are 7 letters, that is the answer + sorted_tie_array.each do |array| + if array[0] == 7 + return array[1] + end + end + + # find shortest word => length shortest_word = sorted_tie_array[0][0] + # deletes sub-arrays that do not equal the length of the shortest word shortest_word_array = sorted_tie_array.delete_if{|array| array[0] != shortest_word } if shortest_word_array.length == 1 return shortest_word_array[0][1] # else there are multiple words with the same length - else + else array_of_words.each do |word| shortest_word_array.each do |array| if word == array[1] diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 3fc54e2d..6c1861c1 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -15,16 +15,25 @@ it "word should generate the score of word being 8" do Scrabble::Scoring.score("word").must_equal(8) end + + it "QRSTLNE should generate the score of 66" do + Scrabble::Scoring.score("QRSTLNE").must_equal(66) + end + end describe "Scrabble::Scoring#highest_score_from" do - # it "returns the word in array with the highest score" do - # Scrabble::Scoring.highest_score_from(["WORD", "CAT", "MEOW", "ZEBRA"]).must_equal("ZEBRA") - # end + it "returns the word in array with the highest score => ZEBRA " do + Scrabble::Scoring.highest_score_from(["WORD", "CAT", "MEOW", "ZEBRA"]).must_equal("ZEBRA") + end + + it "returns ZEBRE because it occurs before ZEBRA and both have 16 points" do + Scrabble::Scoring.highest_score_from(["WORD", "CAT", "ZEBRE", "ZEBRA"]).must_equal("ZEBRE") + end - it "returns an error message" do - Scrabble::Scoring.highest_score_from(["WORD", "QRSTLNE", "ZEBRE", "ZEBRA"]).must_equal("ZEBRE") + it "returns QRSTLNE because it has 7 letters and is tied for 16 pts" do + Scrabble::Scoring.highest_score_from(["WORD", "QRSTLNE", "ZEBRE", "ZEBRA"]).must_equal("QRSTLNE") end end From 9240c59df3b692c392df51f06dad78b6a3d340e3 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Tue, 8 Mar 2016 15:34:09 -0800 Subject: [PATCH 16/33] Added more comments. Looked at index.html and deleted useless lines of code. Added a ZEDREE test. Everything rakes. --- lib/scoring.rb | 35 +++++++++-------------------------- specs/scoring_spec.rb | 3 +++ 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index db6bcbff..dd84290f 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -11,10 +11,12 @@ class Scrabble::Scoring 10 => ["Q", "Z"] } + # meow meow meow def self.test return "MEOW" end + # returns score (single word) def self.score(word) word.upcase! score = 0 @@ -29,10 +31,11 @@ def self.score(word) if word.length == 7 score += 50 end - + return score end + # returns highest scored word ([array]) def self.highest_score_from(array_of_words) score_array = [] word_array = [] @@ -55,7 +58,7 @@ def self.highest_score_from(array_of_words) if tie_array.length == 1 return tie_array[0][1] # this is the answer # if there's a tie, reassign score to equal length of word => [[length,word]] - else + else tie_array.each do |array| len = array[1].length array[0] = len @@ -64,21 +67,17 @@ def self.highest_score_from(array_of_words) # sorts the tied array by length sorted_tie_array = tie_array.sort_by { |array| array[0] } - # if there are 7 letters, that is the answer - sorted_tie_array.each do |array| - if array[0] == 7 - return array[1] - end - end - # find shortest word => length shortest_word = sorted_tie_array[0][0] # deletes sub-arrays that do not equal the length of the shortest word shortest_word_array = sorted_tie_array.delete_if{|array| array[0] != shortest_word } + # if there's one word left, it's obvs the answer if shortest_word_array.length == 1 return shortest_word_array[0][1] - # else there are multiple words with the same length + + # else there are multiple words with the same length + # select the one that came first (based on user input) else array_of_words.each do |word| shortest_word_array.each do |array| @@ -88,21 +87,5 @@ def self.highest_score_from(array_of_words) end end end - - - - # temp_array = [0, "word"] - # loop_score = 0 - # temp_score = 0 - # array_of_words.each do |word| - # temp_score = Scrabble::Scoring.score(word) - # - # if temp_score > loop_score - # loop_score = temp_array[0] - # temp_array = [loop_score, word] - # end - # end - # return temp_array[1] end - end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 6c1861c1..2a3b8206 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -36,6 +36,9 @@ Scrabble::Scoring.highest_score_from(["WORD", "QRSTLNE", "ZEBRE", "ZEBRA"]).must_equal("QRSTLNE") end + it "returns ZEBRA because it has the shortest length -- both 16 points" do + Scrabble::Scoring.highest_score_from(["ZEDREE", "ZEBRA"]).must_equal("ZEBRA") + end end describe "Scrabble::Scoring#test" do From 81a8b84c94739e8678832c3ec8ac0afa7cfba8df Mon Sep 17 00:00:00 2001 From: Leah Date: Tue, 8 Mar 2016 15:59:38 -0800 Subject: [PATCH 17/33] Created a method to clean up redudency in searching arrays for ties --- lib/scoring.rb | 44 ++++++++++++++++++++----------------------- sandbox.rb | 9 +++++++++ specs/scoring_spec.rb | 6 ------ 3 files changed, 29 insertions(+), 30 deletions(-) create mode 100644 sandbox.rb diff --git a/lib/scoring.rb b/lib/scoring.rb index dd84290f..3b4b1dec 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -11,11 +11,6 @@ class Scrabble::Scoring 10 => ["Q", "Z"] } - # meow meow meow - def self.test - return "MEOW" - end - # returns score (single word) def self.score(word) word.upcase! @@ -27,15 +22,22 @@ def self.score(word) end end end - + # special bonus if you use 7 tiles if word.length == 7 score += 50 end - + # return score as int return score end - # returns highest scored word ([array]) + # method that sorts array or arrays by array[0], finds ties and returns array with only ties + def self.sort_drop(array_of_arrays, index) + sorted_array = array_of_arrays.sort_by { |array| array[0] } + min_max = sorted_array[index][0] + sorted_array.delete_if{|array| array[0] != min_max } + return sorted_array + end + # returns highest scored word as ([array]) def self.highest_score_from(array_of_words) score_array = [] word_array = [] @@ -49,38 +51,32 @@ def self.highest_score_from(array_of_words) # combines the score and word array AND sorts total_array = score_array.zip(word_array) # => [[score,word]] - sorted_array = total_array.sort_by{|score| score[0]} - highest_score = sorted_array[-1][0] # gets highest score + # call method to sort and return only score ties + sorted_array = self.sort_drop(total_array, -1) - # deletes sub-arrays that are not equal to the highest score - tie_array = sorted_array.delete_if{|array| array[0] != highest_score} # if there's one word left - if tie_array.length == 1 - return tie_array[0][1] # this is the answer + if sorted_array.length == 1 + return sorted_array[0][1] # this is the answer # if there's a tie, reassign score to equal length of word => [[length,word]] else - tie_array.each do |array| + sorted_array.each do |array| len = array[1].length array[0] = len end end - # sorts the tied array by length - sorted_tie_array = tie_array.sort_by { |array| array[0] } - # find shortest word => length - shortest_word = sorted_tie_array[0][0] - # deletes sub-arrays that do not equal the length of the shortest word - shortest_word_array = sorted_tie_array.delete_if{|array| array[0] != shortest_word } + # call method to sort and return only length ties + sorted_tie_array = self.sort_drop(sorted_array, 0) # if there's one word left, it's obvs the answer - if shortest_word_array.length == 1 - return shortest_word_array[0][1] + if sorted_tie_array.length == 1 + return sorted_tie_array[0][1] # else there are multiple words with the same length # select the one that came first (based on user input) else array_of_words.each do |word| - shortest_word_array.each do |array| + sorted_tie_array.each do |array| if word == array[1] return array[1] end diff --git a/sandbox.rb b/sandbox.rb new file mode 100644 index 00000000..a0caa48d --- /dev/null +++ b/sandbox.rb @@ -0,0 +1,9 @@ +def self.sort_drop(array_of_arrays, index) + sorted_array = array_of_arrays.sort_by { |array| array[0] } + min_max = sorted_array[index][0] + sorted_array.delete_if{|array| array[0] != min_max } + return sorted_array +end + +self.sort_drop(total_array, -1) +self.sort_drop(sorted_tie_array, 0) \ No newline at end of file diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 2a3b8206..4feec712 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -40,10 +40,4 @@ Scrabble::Scoring.highest_score_from(["ZEDREE", "ZEBRA"]).must_equal("ZEBRA") end end - - describe "Scrabble::Scoring#test" do - it "Should generate 'MEOW'" do - Scrabble::Scoring.test.must_equal("MEOW") - end - end end From 3ac492ee872434cc5eb19a44c7f6ce51fac1805f Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Tue, 8 Mar 2016 16:55:07 -0800 Subject: [PATCH 18/33] added basic wave2 methods to player and started a player_spec. --- lib/player.rb | 36 ++++++++++++++++++++++++++++-------- lib/scoring.rb | 1 - scrabble.rb | 2 +- specs/player_spec.rb | 22 ++++++++++++++-------- 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 1b46271b..0d9a4c50 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,8 +1,28 @@ -# require '../Scrabble' - -# class Scrabble::Player -# # include Scrabble -# # def initialize(file) -# # @scrabble_file = import(file) -# # end -# end +class Scrabble::Player + attr_reader :name + def initialize(name) + @name = name + end + + def self.plays + played_words = [] + end + + def self.play(word) + played_words << word + end + + def self.total_score + end + + def self.won? + end + + def self.highest_scoring_word + end + + # Returns the highest_scoring_word score + def self.highest_word_score + end + +end diff --git a/lib/scoring.rb b/lib/scoring.rb index 3b4b1dec..6cb833a4 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,4 +1,3 @@ -# require_relative '../scrabble' class Scrabble::Scoring # letters and corresponding values for scoring the words LETTERS = { diff --git a/scrabble.rb b/scrabble.rb index 6913f894..e892905a 100644 --- a/scrabble.rb +++ b/scrabble.rb @@ -3,4 +3,4 @@ module Scrabble end require_relative './lib/scoring' -# require_relative './lib/player' +require_relative './lib/player' diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 703f5996..c56496f5 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -1,9 +1,15 @@ -# require_relative './spec_helper' -# require_relative '../Scrabble' +require_relative './spec_helper' +require_relative '../scrabble' -# write a failing test!!! -# describe Player do -# it "Is there a class? Anything?" do -# Player.wont_be_nil -# end -# end +#write a failing test +describe Scrabble::Player do + it "Is there a class? Anything?" do + Scrabble::Player.wont_be_nil + end + + #make a bob, do certain plays and assert what's in bob + it "returns a name" do + bob = Scrabble::Player.new("Bob") + bob.name.must_equal("Bob") + end +end From 5bde06600003e6f1e53f373ff2e203c519df52e9 Mon Sep 17 00:00:00 2001 From: Leah Date: Wed, 9 Mar 2016 15:53:16 -0800 Subject: [PATCH 19/33] Added played_words in initializer, made it an attr_reader and created a test to test that it returns an array of played words --- lib/player.rb | 11 ++++------- specs/player_spec.rb | 7 ++++++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 0d9a4c50..1dcd630f 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,11 +1,8 @@ class Scrabble::Player - attr_reader :name - def initialize(name) - @name = name - end - - def self.plays - played_words = [] + attr_reader :name, :played_words + def initialize(hash) + @name = hash[:name] + @played_words = hash[:words] end def self.play(word) diff --git a/specs/player_spec.rb b/specs/player_spec.rb index c56496f5..5054c8dd 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -9,7 +9,12 @@ #make a bob, do certain plays and assert what's in bob it "returns a name" do - bob = Scrabble::Player.new("Bob") + bob = Scrabble::Player.new(name: "Bob", words: ["house", "cat", "zebra"]) bob.name.must_equal("Bob") end + + it "returns the played words" do + bob = Scrabble::Player.new(name: "Bob", words: ["house", "cat", "zebra"]) + bob.played_words.must_equal(["house", "cat", "zebra"]) + end end From 66d9546d5c6e5f9d0010831bee3eef118d164e51 Mon Sep 17 00:00:00 2001 From: Leah Date: Wed, 9 Mar 2016 16:25:24 -0800 Subject: [PATCH 20/33] Added play(words) method and test to take in newly played word and return score and add to played_words array --- lib/player.rb | 14 ++++++++------ specs/player_spec.rb | 10 ++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 1dcd630f..786d7254 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -5,21 +5,23 @@ def initialize(hash) @played_words = hash[:words] end - def self.play(word) - played_words << word + def play(word) + # returns false if already won + @played_words << word + Scrabble::Scoring.score(word) end - def self.total_score + def total_score end - def self.won? + def won? end - def self.highest_scoring_word + def highest_scoring_word end # Returns the highest_scoring_word score - def self.highest_word_score + def highest_word_score end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 5054c8dd..98b6eb0d 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -13,8 +13,18 @@ bob.name.must_equal("Bob") end + # make sure played_words returns array of words it "returns the played words" do bob = Scrabble::Player.new(name: "Bob", words: ["house", "cat", "zebra"]) bob.played_words.must_equal(["house", "cat", "zebra"]) end + + # test play(word) returns score + it "returns score of newly played word" do + bob = Scrabble::Player.new(name: "Bob", words: ["house", "cat"]) + bob.play("zebra").must_equal(16) + + end + + end From 39c677c34fb43c8842c28e6dbdbdb8f34436c156 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Wed, 9 Mar 2016 16:34:25 -0800 Subject: [PATCH 21/33] Added total_score method and test. It works.. --- lib/player.rb | 5 +++++ specs/player_spec.rb | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/lib/player.rb b/lib/player.rb index 786d7254..fa283c9a 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -12,6 +12,11 @@ def play(word) end def total_score + points = 0 + played_words.each do |word| + points += Scrabble::Scoring.score(word) + end + return points end def won? diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 98b6eb0d..3c3c77b4 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -23,8 +23,14 @@ it "returns score of newly played word" do bob = Scrabble::Player.new(name: "Bob", words: ["house", "cat"]) bob.play("zebra").must_equal(16) + end + # test total score based on array of words + it "returns the total score of all the words in the array" do + bob = Scrabble::Player.new(name: "Bob", words: ["zebre","zebra"]) + bob.total_score.must_equal(32) end + end From f1711d76c4ddea268a0ea62a8939b8d1c4f08fd8 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Wed, 9 Mar 2016 17:00:38 -0800 Subject: [PATCH 22/33] Added won? method and called it into play(word). Next we will work on highest_scoring_word. --- lib/player.rb | 9 +++++++-- specs/player_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index fa283c9a..78b930b3 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -7,8 +7,12 @@ def initialize(hash) def play(word) # returns false if already won - @played_words << word - Scrabble::Scoring.score(word) + if won? + return false + else + @played_words << word + Scrabble::Scoring.score(word) + end end def total_score @@ -20,6 +24,7 @@ def total_score end def won? + total_score >= 100 ? true : false end def highest_scoring_word diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 3c3c77b4..631d34c7 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -31,6 +31,27 @@ bob.total_score.must_equal(32) end + # test to see if player won based on total score = true + it "returns true/false based on player winning" do + bob = Scrabble::Player.new(name: "Bob", words: ["zzzzzzz","zzzzzzz"]) + bob.won?.must_equal true + end + + # test to see if player has NOT won => false + it "returns true/false based on player winning" do + bob = Scrabble::Player.new(name: "Bob", words: ["cat"]) + bob.won?.must_equal false + end + # test to see if player has NOT won => false + it "returns true/false based on player winning" do + bob = Scrabble::Player.new(name: "Bob", words: ["cat"]) + bob.play("zebra") + bob.played_words.must_equal(["CAT", "ZEBRA"]) + bob.total_score.must_equal(21) + bob.play("zzzzzzz") + bob.total_score.must_equal(141) + bob.play("bees").must_equal false + end end From 9667c20bdb833ee1d8d195ffd0d85442d4369d61 Mon Sep 17 00:00:00 2001 From: Leah Date: Thu, 10 Mar 2016 15:08:47 -0800 Subject: [PATCH 23/33] Added highest_scoring_word and highest_word_score methods and tests --- lib/player.rb | 4 ++++ specs/player_spec.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/player.rb b/lib/player.rb index 78b930b3..b06e8db1 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -27,11 +27,15 @@ def won? total_score >= 100 ? true : false end + # Returns the highest scoring played word def highest_scoring_word + played_words.max_by {|word| Scrabble::Scoring.score(word)} end # Returns the highest_scoring_word score def highest_word_score + highest_word = played_words.max_by {|word| Scrabble::Scoring.score(word)} + Scrabble::Scoring.score(highest_word) end end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 631d34c7..91172739 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -54,4 +54,16 @@ bob.play("bees").must_equal false end + # test to see highest_scoring_word returns correctly + it "returns highest scoring word" do + bob = Scrabble::Player.new(name: "Bob", words: ["cat", "house", "zebra", "ZZZZZZZ"]) + bob.highest_scoring_word.must_equal "ZZZZZZZ" + end + + # test to see highest_word_score returns correctly + it "returns highest score" do + bob = Scrabble::Player.new(name: "Bob", words: ["cat", "house", "zebra", "ZZZZZZZ"]) + bob.highest_word_score.must_equal 120 + end + end From 62f6cc6f9125d086c4cf9e3d1b89d6f6313ecbb6 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Thu, 10 Mar 2016 16:20:32 -0800 Subject: [PATCH 24/33] added the entire tilebag class and specs without interim commits.. woops. Everything passes though. --- specs/tilebag_spec.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 specs/tilebag_spec.rb diff --git a/specs/tilebag_spec.rb b/specs/tilebag_spec.rb new file mode 100644 index 00000000..76b88a01 --- /dev/null +++ b/specs/tilebag_spec.rb @@ -0,0 +1,26 @@ +require_relative './spec_helper' +require_relative '../scrabble' + +#write a failing test +describe Scrabble::TileBag do + it "Is there a class? Anything?" do + Scrabble::TileBag.wont_be_nil + end + + it "should return 26 == number of letters in the alphabet" do + game = Scrabble::TileBag.new + game.tilebag.length.must_equal(26) + end + + it "should return length of selected tiles" do + game = Scrabble::TileBag.new + game.draw_tiles(5).length.must_equal(5) + end + + it "should return number of tiles left in bag" do + game = Scrabble::TileBag.new + game.draw_tiles(40) + game.tiles_remaining.must_equal(58) + end + +end From 0bf15affe6998af3ce87dffa2f0a3c6982cb5c7a Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Thu, 10 Mar 2016 16:22:27 -0800 Subject: [PATCH 25/33] messed up last push (twas a mere folder). this should have everything. --- lib/tilebag.rb | 36 ++++++++++++++++++++++++++++++++++++ scrabble.rb | 1 + 2 files changed, 37 insertions(+) create mode 100644 lib/tilebag.rb diff --git a/lib/tilebag.rb b/lib/tilebag.rb new file mode 100644 index 00000000..94c659fe --- /dev/null +++ b/lib/tilebag.rb @@ -0,0 +1,36 @@ +class Scrabble::TileBag + attr_reader :tilebag + def initialize + array_letters = ("A".."Z").to_a + array_numbers = [9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, + 6, 4, 2, 2, 1, 2, 1] + @tilebag = array_letters.zip(array_numbers) + end + + def draw_tiles(num) + letter = [] + num.times do |tile| + index = rand(0..tilebag.length-1) + # shovels letters into array + letter << tilebag[index][0] + # removes 1 from letter count if tile is drawn + @tilebag[index][1] = tilebag[index][1] - 1 + + if tilebag[index][1] == 0 + @tilebag.delete(tilebag[index]) + end + end + return letter + # + + end + + def tiles_remaining + tiles = 0 + tilebag.each do |letter, number| + tiles = tiles + number + end + return tiles + end + +end diff --git a/scrabble.rb b/scrabble.rb index e892905a..45cff048 100644 --- a/scrabble.rb +++ b/scrabble.rb @@ -4,3 +4,4 @@ module Scrabble require_relative './lib/scoring' require_relative './lib/player' +require_relative './lib/tilebag' From 08a7ed0c02c4673ab03bf5cd4b34b11f43dc0bad Mon Sep 17 00:00:00 2001 From: Leah Date: Thu, 10 Mar 2016 16:57:46 -0800 Subject: [PATCH 26/33] Start by correcting tiles_method to just remove played chars and repopulated tiles --- lib/player.rb | 20 +++++++++++++++++--- lib/tilebag.rb | 14 +++++++++----- specs/player_spec.rb | 5 +++++ specs/tilebag_spec.rb | 15 +++++++++++++++ 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index b06e8db1..4644d655 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,8 +1,9 @@ class Scrabble::Player - attr_reader :name, :played_words + attr_reader :name, :played_words, :tiles def initialize(hash) - @name = hash[:name] - @played_words = hash[:words] + @name = hash[:name] + @played_words = hash[:words] + @tiles = Scrabble::TileBag.draw_tiles(7) end def play(word) @@ -13,6 +14,7 @@ def play(word) @played_words << word Scrabble::Scoring.score(word) end + # pass new word to tiles_method to remove those letters from @tiles end def total_score @@ -38,4 +40,16 @@ def highest_word_score Scrabble::Scoring.score(highest_word) end + # removes letters from played word and repopulates tiles to 7 + def tiles_method(word) + len = word.length + + # word.each_char do |char| + # if tiles.include?(char) + # @tiles.delete(char) + # end + # end + @tiles << Scrabble::TileBag.draw_tiles(len) + return @tiles + end end diff --git a/lib/tilebag.rb b/lib/tilebag.rb index 94c659fe..79c99ce0 100644 --- a/lib/tilebag.rb +++ b/lib/tilebag.rb @@ -10,27 +10,31 @@ def initialize def draw_tiles(num) letter = [] num.times do |tile| + # make sure there are tiles left in bag + if tilebag.length == 0 + puts "No more tiles left." + break + end index = rand(0..tilebag.length-1) # shovels letters into array letter << tilebag[index][0] # removes 1 from letter count if tile is drawn @tilebag[index][1] = tilebag[index][1] - 1 - + # if letter amount = 0 remove entire subarray if tilebag[index][1] == 0 @tilebag.delete(tilebag[index]) end end - return letter - # - + return letter end + # method to return number of tiles remaining def tiles_remaining tiles = 0 + # counter numbers from each letter subarray tilebag.each do |letter, number| tiles = tiles + number end return tiles end - end diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 91172739..427a1df3 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -66,4 +66,9 @@ bob.highest_word_score.must_equal 120 end + it "returns highest score" do + bob = Scrabble::Player.new(name: "Bob", words: ["cat", "house", "zebra", "ZZZZZZZ"]) + bob.highest_word_score.must_equal 120 + end + end diff --git a/specs/tilebag_spec.rb b/specs/tilebag_spec.rb index 76b88a01..e699aa89 100644 --- a/specs/tilebag_spec.rb +++ b/specs/tilebag_spec.rb @@ -17,10 +17,25 @@ game.draw_tiles(5).length.must_equal(5) end + # check tiles_remaining corresponds to draw_tiles it "should return number of tiles left in bag" do game = Scrabble::TileBag.new game.draw_tiles(40) game.tiles_remaining.must_equal(58) end + # make sure tiles_remaining is zero when all tiles are removed + it "should return 0" do + game = Scrabble::TileBag.new + game.draw_tiles(99) + game.tiles_remaining.must_equal(0) + end + + # remove all the tiles + it "what happens when you remove 99 tiles!?!?!?" do + game = Scrabble::TileBag.new + game.draw_tiles(99).must_equal("dogs") + end + + end From 15b51f76bc07844270c3572d6c8aa9ba8339b5c9 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Fri, 11 Mar 2016 10:51:28 -0800 Subject: [PATCH 27/33] To fix player.rb we had to create an instance of tilebag and refer to it to call methods. Added draw method. Everything passes. --- lib/player.rb | 23 +++++++++++------------ scrabble.rb | 5 ++--- specs/player_spec.rb | 15 +++++++++++---- specs/tilebag_spec.rb | 8 -------- 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 4644d655..f34d7bc9 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,12 +1,19 @@ class Scrabble::Player - attr_reader :name, :played_words, :tiles + attr_reader :name, :played_words, :bag, :tiles def initialize(hash) @name = hash[:name] @played_words = hash[:words] - @tiles = Scrabble::TileBag.draw_tiles(7) + @bag = Scrabble::TileBag.new + @tiles = @bag.draw_tiles(7) end def play(word) + # removes number of played tiles and re-draws from tile bag + length = word.length + @tiles.pop(length) + # pass new word to tiles_method to remove those letters from @tiles + draw(length) + # returns false if already won if won? return false @@ -14,7 +21,6 @@ def play(word) @played_words << word Scrabble::Scoring.score(word) end - # pass new word to tiles_method to remove those letters from @tiles end def total_score @@ -41,15 +47,8 @@ def highest_word_score end # removes letters from played word and repopulates tiles to 7 - def tiles_method(word) - len = word.length - - # word.each_char do |char| - # if tiles.include?(char) - # @tiles.delete(char) - # end - # end - @tiles << Scrabble::TileBag.draw_tiles(len) + def draw(length) + @tiles += @bag.draw_tiles(length) return @tiles end end diff --git a/scrabble.rb b/scrabble.rb index 45cff048..45effd1d 100644 --- a/scrabble.rb +++ b/scrabble.rb @@ -1,7 +1,6 @@ module Scrabble end - -require_relative './lib/scoring' -require_relative './lib/player' require_relative './lib/tilebag' +require_relative './lib/player' +require_relative './lib/scoring' diff --git a/specs/player_spec.rb b/specs/player_spec.rb index 427a1df3..c63b85cb 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -7,7 +7,7 @@ Scrabble::Player.wont_be_nil end - #make a bob, do certain plays and assert what's in bob + # make a bob, do certain plays and assert what's in bob it "returns a name" do bob = Scrabble::Player.new(name: "Bob", words: ["house", "cat", "zebra"]) bob.name.must_equal("Bob") @@ -66,9 +66,16 @@ bob.highest_word_score.must_equal 120 end - it "returns highest score" do - bob = Scrabble::Player.new(name: "Bob", words: ["cat", "house", "zebra", "ZZZZZZZ"]) - bob.highest_word_score.must_equal 120 + it "should start with 7 tiles " do + bob = Scrabble::Player.new(name: "Bob", words: ["dog"]) + bob.tiles.length.must_equal(7) + end + + it "should always have 7 tiles to play with after playing words " do + bob = Scrabble::Player.new(name: "Bob", words: ["dog"]) + bob.tiles + bob.play("abcd") + bob.tiles.length.must_equal(7) end end diff --git a/specs/tilebag_spec.rb b/specs/tilebag_spec.rb index e699aa89..9bef546b 100644 --- a/specs/tilebag_spec.rb +++ b/specs/tilebag_spec.rb @@ -30,12 +30,4 @@ game.draw_tiles(99) game.tiles_remaining.must_equal(0) end - - # remove all the tiles - it "what happens when you remove 99 tiles!?!?!?" do - game = Scrabble::TileBag.new - game.draw_tiles(99).must_equal("dogs") - end - - end From 13064740d06b53a50c11c262b7b41ea271399320 Mon Sep 17 00:00:00 2001 From: Leah Date: Fri, 11 Mar 2016 10:58:05 -0800 Subject: [PATCH 28/33] Cleaned up player_spec, add two final tests with two player instances. --- specs/player_spec.rb | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/specs/player_spec.rb b/specs/player_spec.rb index c63b85cb..037518de 100644 --- a/specs/player_spec.rb +++ b/specs/player_spec.rb @@ -1,8 +1,8 @@ require_relative './spec_helper' require_relative '../scrabble' -#write a failing test describe Scrabble::Player do + # make sure there is a Player class it "Is there a class? Anything?" do Scrabble::Player.wont_be_nil end @@ -60,17 +60,19 @@ bob.highest_scoring_word.must_equal "ZZZZZZZ" end - # test to see highest_word_score returns correctly + # test to see highest_word_score returns correctly it "returns highest score" do bob = Scrabble::Player.new(name: "Bob", words: ["cat", "house", "zebra", "ZZZZZZZ"]) bob.highest_word_score.must_equal 120 end + # make sure player instances are created with 7 tiles to play with it "should start with 7 tiles " do bob = Scrabble::Player.new(name: "Bob", words: ["dog"]) bob.tiles.length.must_equal(7) end + # make sure play and draw method work together to keep 7 tiles to play with it "should always have 7 tiles to play with after playing words " do bob = Scrabble::Player.new(name: "Bob", words: ["dog"]) bob.tiles @@ -78,4 +80,21 @@ bob.tiles.length.must_equal(7) end + # holistic test with two player instances + it "check that Darren's score is 20 " do + bob = Scrabble::Player.new(name: "Bob", words: ["dog"]) + darren = Scrabble::Player.new(name: "Darren", words: ["house"]) + bob.play("mouse") + darren.play("box") + darren.total_score.must_equal(20) + end + + # holistic test with two player instances PART 2! + it "check that Bob's score is 12 " do + bob = Scrabble::Player.new(name: "Bob", words: ["dog"]) + darren = Scrabble::Player.new(name: "Darren", words: ["house"]) + bob.play("mouse") + darren.play("box") + bob.total_score.must_equal(12) + end end From ac55e6b11e9b30045190fa5a4a2a9712bc3da9e4 Mon Sep 17 00:00:00 2001 From: Leah Date: Fri, 11 Mar 2016 11:13:16 -0800 Subject: [PATCH 29/33] Cleaned up scoring_spec, added spec to check constant was set correctly --- lib/scoring.rb | 1 + specs/scoring_spec.rb | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 6cb833a4..0d1252e8 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,4 +1,5 @@ class Scrabble::Scoring + attr_reader :LETTERS # letters and corresponding values for scoring the words LETTERS = { 1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index 4feec712..bb703e74 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -1,41 +1,46 @@ require_relative './spec_helper' require_relative '../scrabble' -# write a failing test!!! describe Scrabble::Scoring do + # make sure there is Scoring class it "Is there a class? Anything?" do Scrabble::Scoring.wont_be_nil end + # make sure constant array is initialized (array of arrays) holding letter/value info + it "Is the constant array of letter/values correct?" + Scrabble::Scoring.LETTERS.length.must_equal(26) + end + describe "Scrabble::Scoring#score" do + # check score method, should return SCORE of played word it "WORD should generate the score of word being 8" do Scrabble::Scoring.score("WORD").must_equal(8) end - it "word should generate the score of word being 8" do - Scrabble::Scoring.score("word").must_equal(8) - end - + # check that scoring on huge words words it "QRSTLNE should generate the score of 66" do Scrabble::Scoring.score("QRSTLNE").must_equal(66) end - end describe "Scrabble::Scoring#highest_score_from" do - + # returns word in played_words that has the highest score it "returns the word in array with the highest score => ZEBRA " do Scrabble::Scoring.highest_score_from(["WORD", "CAT", "MEOW", "ZEBRA"]).must_equal("ZEBRA") end + # checks that if a tie, word that was passed first in initial array wins it "returns ZEBRE because it occurs before ZEBRA and both have 16 points" do Scrabble::Scoring.highest_score_from(["WORD", "CAT", "ZEBRE", "ZEBRA"]).must_equal("ZEBRE") end + # if tie return word using 7 tiles it "returns QRSTLNE because it has 7 letters and is tied for 16 pts" do Scrabble::Scoring.highest_score_from(["WORD", "QRSTLNE", "ZEBRE", "ZEBRA"]).must_equal("QRSTLNE") end + # if tied return word with shortest length it "returns ZEBRA because it has the shortest length -- both 16 points" do Scrabble::Scoring.highest_score_from(["ZEDREE", "ZEBRA"]).must_equal("ZEBRA") end From 81229354e065454046bf8a8c4addf5115bad8ae7 Mon Sep 17 00:00:00 2001 From: Leah Date: Fri, 11 Mar 2016 11:14:47 -0800 Subject: [PATCH 30/33] Changed language in second scoring_spec spec --- specs/scoring_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index bb703e74..c3266068 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -7,8 +7,8 @@ Scrabble::Scoring.wont_be_nil end - # make sure constant array is initialized (array of arrays) holding letter/value info - it "Is the constant array of letter/values correct?" + # make sure constant array is initialized (array of arrays) holding letter/amount info + it "Is the constant array of letter/amount correct?" Scrabble::Scoring.LETTERS.length.must_equal(26) end From 5deeb78937a15996ad5ac7056fe97369b8131b9f Mon Sep 17 00:00:00 2001 From: Leah Date: Fri, 11 Mar 2016 11:15:57 -0800 Subject: [PATCH 31/33] Cleaned up tilebag_spec, added comments --- specs/tilebag_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/specs/tilebag_spec.rb b/specs/tilebag_spec.rb index 9bef546b..34b218e4 100644 --- a/specs/tilebag_spec.rb +++ b/specs/tilebag_spec.rb @@ -1,17 +1,19 @@ require_relative './spec_helper' require_relative '../scrabble' -#write a failing test describe Scrabble::TileBag do + # make sure there is a TileBag class it "Is there a class? Anything?" do Scrabble::TileBag.wont_be_nil end + # make sure a new instance creates a tilebag correctly it "should return 26 == number of letters in the alphabet" do game = Scrabble::TileBag.new game.tilebag.length.must_equal(26) end + # make sure draw_tiles method words correctly with passed parameter it "should return length of selected tiles" do game = Scrabble::TileBag.new game.draw_tiles(5).length.must_equal(5) From d8f750e80f3e1a0fcf1bb359554c4d338f2a7e09 Mon Sep 17 00:00:00 2001 From: Leah Date: Fri, 11 Mar 2016 11:18:28 -0800 Subject: [PATCH 32/33] Cleaned up player.rb added comments --- lib/player.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/player.rb b/lib/player.rb index f34d7bc9..1eecf940 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -4,16 +4,17 @@ def initialize(hash) @name = hash[:name] @played_words = hash[:words] @bag = Scrabble::TileBag.new + # initialize bag of tiles to play with - always 7 @tiles = @bag.draw_tiles(7) end + # method to take in word, remove length from tiles, see if player won and generate score for word def play(word) # removes number of played tiles and re-draws from tile bag length = word.length @tiles.pop(length) # pass new word to tiles_method to remove those letters from @tiles draw(length) - # returns false if already won if won? return false @@ -23,6 +24,7 @@ def play(word) end end + # take all played words and generate score (sum of all) def total_score points = 0 played_words.each do |word| @@ -31,6 +33,7 @@ def total_score return points end + # method to track if player has won def won? total_score >= 100 ? true : false end From effe50338ac3b9d00a1dd6882316fa43abbf46db Mon Sep 17 00:00:00 2001 From: Leah Date: Fri, 11 Mar 2016 11:21:01 -0800 Subject: [PATCH 33/33] Cleaned up tilebag.rg added comments --- lib/tilebag.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/tilebag.rb b/lib/tilebag.rb index 79c99ce0..1ff648b3 100644 --- a/lib/tilebag.rb +++ b/lib/tilebag.rb @@ -1,5 +1,6 @@ class Scrabble::TileBag attr_reader :tilebag + # initialize an array of array holding letter/amount info def initialize array_letters = ("A".."Z").to_a array_numbers = [9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, @@ -7,6 +8,7 @@ def initialize @tilebag = array_letters.zip(array_numbers) end + # method to randomly select tiles def draw_tiles(num) letter = [] num.times do |tile|