From 3210c0ef53ea9365ecea6a34314a5935cfa03940 Mon Sep 17 00:00:00 2001 From: Sarah Date: Mon, 7 Mar 2016 17:04:16 -0800 Subject: [PATCH 01/24] primary requirements --- Rakefile | 9 +++++++++ lib/player.rb | 2 ++ lib/scoring.rb | 2 ++ scrabble.rb | 8 ++++++++ specs/player-spec.rb | 0 specs/scoring-spec.rb | 0 specs/spec_helper.rb | 5 +++++ 7 files changed, 26 insertions(+) create mode 100644 Rakefile create mode 100644 lib/player.rb create mode 100644 lib/scoring.rb create mode 100644 scrabble.rb create mode 100644 specs/player-spec.rb create mode 100644 specs/scoring-spec.rb create mode 100644 specs/spec_helper.rb diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..deb52f2c --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ +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 diff --git a/lib/player.rb b/lib/player.rb new file mode 100644 index 00000000..8e636451 --- /dev/null +++ b/lib/player.rb @@ -0,0 +1,2 @@ +class Player +end diff --git a/lib/scoring.rb b/lib/scoring.rb new file mode 100644 index 00000000..79f0c612 --- /dev/null +++ b/lib/scoring.rb @@ -0,0 +1,2 @@ +class Scoring +end diff --git a/scrabble.rb b/scrabble.rb new file mode 100644 index 00000000..6a693a6e --- /dev/null +++ b/scrabble.rb @@ -0,0 +1,8 @@ +require_relative '.lib/scoring' +require_relative '.lib/player' + +module Scrabble + include Scoring + + include Player +end 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 diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..396c7c8e --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,5 @@ +require 'minitest' +require 'minitest/spec' +require 'minitest/autorun' +require 'minitest/reporters' +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From ff6d6bf7cb7b413554348f7a4cc7faa3f0d3b516 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Tue, 8 Mar 2016 10:39:24 -0800 Subject: [PATCH 02/24] added first test. got spec to work. Added self.score to Scoring class --- Rakefile | 2 +- lib/scoring.rb | 24 ++++++++++++++++++++++++ scrabble.rb | 2 -- specs/scoring-spec.rb | 9 +++++++++ specs/spec_helper.rb | 3 +++ 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Rakefile b/Rakefile index deb52f2c..358454d5 100644 --- a/Rakefile +++ b/Rakefile @@ -3,7 +3,7 @@ require 'rake/testtask' Rake::TestTask.new do |t| t.libs = ["lib"] t.warning = true - t.test_files = FileList['specs/*_spec.rb'] + t.test_files = FileList['specs/*-spec.rb'] end task default: :test diff --git a/lib/scoring.rb b/lib/scoring.rb index 79f0c612..bf40d3d7 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,2 +1,26 @@ class Scoring + LETTER_SCORES = { + 1 => ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'], + 2 => ['D', 'G'], + 3 => ['B', 'C', 'M', 'P'], + 4 => ['F', 'H', 'V', 'W', 'Y'], + 5 => ['K'], + 8 => ['J', 'X'], + 10 => ['Q', 'Z'] + } + + def self.score(word) + word_score = 0 + letter_array = word.upcase.split + letter_array.each do |letter| + LETTER_SCORES.each do |key, value| + if value.include? letter + word_score += key + end + end + end + + word_score + end + end diff --git a/scrabble.rb b/scrabble.rb index 6a693a6e..5a0a8331 100644 --- a/scrabble.rb +++ b/scrabble.rb @@ -2,7 +2,5 @@ require_relative '.lib/player' module Scrabble - include Scoring - include Player end diff --git a/specs/scoring-spec.rb b/specs/scoring-spec.rb index e69de29b..6d252086 100644 --- a/specs/scoring-spec.rb +++ b/specs/scoring-spec.rb @@ -0,0 +1,9 @@ +require_relative './spec_helper' +require_relative '../lib/scoring' + +describe Scoring do + it "is an object we have access to" 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 index 396c7c8e..24450977 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 c7006d8483718792e27bb3dd9970c619e0c5a60c Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Tue, 8 Mar 2016 10:56:21 -0800 Subject: [PATCH 03/24] test to check that we can create array of letters from a given word --- lib/scoring.rb | 20 +++++++++++--------- specs/scoring-spec.rb | 10 ++++++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index bf40d3d7..6dbd535d 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -11,16 +11,18 @@ class Scoring def self.score(word) word_score = 0 - letter_array = word.upcase.split - letter_array.each do |letter| - LETTER_SCORES.each do |key, value| - if value.include? letter - word_score += key - end - end - end + word = word.upcase + letter_array = word.split(//) + return letter_array + # letter_array.each do |letter| + # LETTER_SCORES.each do |key, value| + # if value.include? letter + # word_score += key + # end + # end + # end - word_score + # word_score end end diff --git a/specs/scoring-spec.rb b/specs/scoring-spec.rb index 6d252086..773fb2f1 100644 --- a/specs/scoring-spec.rb +++ b/specs/scoring-spec.rb @@ -6,4 +6,14 @@ Scoring.wont_be_nil end + describe "Scoring#score" do + # TEST_CASES = { + # "cat" => 5 + # } + + it "should return ['C', 'A', 'T'] for the word cat" do + Scoring.score("cat").must_equal(['C', 'A', 'T']) + end + end + end \ No newline at end of file From 208511d7ac00b4ad88df9a3686bd32d75d1fff01 Mon Sep 17 00:00:00 2001 From: Sarah Date: Tue, 8 Mar 2016 11:25:15 -0800 Subject: [PATCH 04/24] added 7 word length 50 point bonus --- lib/scoring.rb | 20 +++++++++++--------- specs/scoring-spec.rb | 19 ++++++++++++------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 6dbd535d..10198f2a 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -13,16 +13,18 @@ def self.score(word) word_score = 0 word = word.upcase letter_array = word.split(//) - return letter_array - # letter_array.each do |letter| - # LETTER_SCORES.each do |key, value| - # if value.include? letter - # word_score += key - # end - # end - # end - # word_score + letter_array.each do |letter| + LETTER_SCORES.each do |key, value| + if value.include? letter + word_score += key + end + end + end + if letter_array.length == 7 + word_score += 50 + end + word_score end end diff --git a/specs/scoring-spec.rb b/specs/scoring-spec.rb index 773fb2f1..eda7d6b8 100644 --- a/specs/scoring-spec.rb +++ b/specs/scoring-spec.rb @@ -7,13 +7,18 @@ end describe "Scoring#score" do - # TEST_CASES = { - # "cat" => 5 - # } + TEST_CASES = { + "cat" => 5, + "dog" => 5, + "zoo" => 12, + "AAAAAAA" => 57 + } - it "should return ['C', 'A', 'T'] for the word cat" do - Scoring.score("cat").must_equal(['C', 'A', 'T']) + + TEST_CASES.each do |word,score| + it "should return #{score} for the word #{word}" do + Scoring.score(word).must_equal(score) + end end end - -end \ No newline at end of file +end From 6d635d92eca1ba31fb7caf3b13d3f1b129a17939 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Tue, 8 Mar 2016 13:48:39 -0800 Subject: [PATCH 05/24] added self.highest_score_from method to Scoring class. Added test to check if highest scored word returned --- lib/scoring.rb | 19 +++++++++++++++++++ specs/scoring-spec.rb | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/lib/scoring.rb b/lib/scoring.rb index 10198f2a..c78e57cf 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -27,4 +27,23 @@ def self.score(word) word_score end + def self.highest_score_from(array_of_words) + word_hash = {} + array_of_words.each do |word| + word_hash[word] = score(word) + end + + word_list = [] + max_value = word_hash.max_by do |key, value| value + end + + word_hash.each do |key, value| + if value == max_value[1] + word_list << key + end + end + + word_list + end + end diff --git a/specs/scoring-spec.rb b/specs/scoring-spec.rb index eda7d6b8..eb95d695 100644 --- a/specs/scoring-spec.rb +++ b/specs/scoring-spec.rb @@ -21,4 +21,10 @@ end end end + + describe "Scoring#highest_score_from" do + it "should return ['zoo'] for the list ['cat', 'dog', 'zoo']" do + Scoring.highest_score_from(['cat', 'dog', 'zoo']).must_equal(['zoo']) + end + end end From ee276146e7affbdc6c981d1e954d76d5bbc577b3 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Tue, 8 Mar 2016 14:14:20 -0800 Subject: [PATCH 06/24] updated self.highest_score_from to include situations where multiple words have the same score. added more tests. --- lib/scoring.rb | 19 ++++++++++++------- specs/scoring-spec.rb | 13 +++++++++++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index c78e57cf..7e300996 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -15,16 +15,17 @@ def self.score(word) letter_array = word.split(//) letter_array.each do |letter| - LETTER_SCORES.each do |key, value| - if value.include? letter + LETTER_SCORES.each do |key, value| + if value.include? letter word_score += key - end - end - end + end + end + end if letter_array.length == 7 word_score += 50 end - word_score + + word_score end def self.highest_score_from(array_of_words) @@ -43,7 +44,11 @@ def self.highest_score_from(array_of_words) end end - word_list + word_list.find {|word| word.length == 7 + return word + } + + word_list.min_by {|words| words.length} end end diff --git a/specs/scoring-spec.rb b/specs/scoring-spec.rb index eb95d695..cbccf8f3 100644 --- a/specs/scoring-spec.rb +++ b/specs/scoring-spec.rb @@ -23,8 +23,17 @@ end describe "Scoring#highest_score_from" do - it "should return ['zoo'] for the list ['cat', 'dog', 'zoo']" do - Scoring.highest_score_from(['cat', 'dog', 'zoo']).must_equal(['zoo']) + TEST_ARRAYS = { + ['cat', 'dog', 'zoo'] => 'zoo', + ['zebra', 'program', 'candy'] => 'program', + ['cat', 'dog', 'sit'] => 'cat', + ['cat', 'AAAAAAA', 'EEEEEEE'] => 'AAAAAAA' + } + + TEST_ARRAYS.each do |list, word| + it "should return #{word} for the list #{list}" do + Scoring.highest_score_from(list).must_equal(word) + end end end end From ac171d69af1990c9e0ad4f9a4b57c40e7760a183 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Tue, 8 Mar 2016 15:03:00 -0800 Subject: [PATCH 07/24] added test cases. modified the self.highest_score_from method to fix .find and .min_by --- lib/scoring.rb | 6 +++--- scrabble.rb | 9 +++++---- specs/scoring-spec.rb | 7 ++++++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 7e300996..818dd30f 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,4 +1,5 @@ class Scoring + LETTER_SCORES = { 1 => ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'], 2 => ['D', 'G'], @@ -44,11 +45,10 @@ def self.highest_score_from(array_of_words) end end - word_list.find {|word| word.length == 7 - return word - } + word_list.find {|word| word.length == 7} word_list.min_by {|words| words.length} + end end diff --git a/scrabble.rb b/scrabble.rb index 5a0a8331..666b99d6 100644 --- a/scrabble.rb +++ b/scrabble.rb @@ -1,6 +1,7 @@ -require_relative '.lib/scoring' -require_relative '.lib/player' - module Scrabble - + include Scoring + end + +require_relative './lib/scoring' +require_relative './lib/player' diff --git a/specs/scoring-spec.rb b/specs/scoring-spec.rb index cbccf8f3..76dc3ff9 100644 --- a/specs/scoring-spec.rb +++ b/specs/scoring-spec.rb @@ -27,7 +27,12 @@ ['cat', 'dog', 'zoo'] => 'zoo', ['zebra', 'program', 'candy'] => 'program', ['cat', 'dog', 'sit'] => 'cat', - ['cat', 'AAAAAAA', 'EEEEEEE'] => 'AAAAAAA' + ['cat', 'AAAAAAA', 'EEEEEEE'] => 'AAAAAAA', + ['QZ', 'JX'] => 'QZ', + ['JX', 'QZ'] => 'QZ', + ['AAAAAAA', 'FFFFFFF'] => 'FFFFFFF', + ['AAAA', 'DG'] => 'DG', + ['BDG', 'AAAAAAA', 'EEEEEEE'] => 'AAAAAAA' } TEST_ARRAYS.each do |list, word| From 6e9a07b3bbfc30ef21f118e9458f6418c9010b48 Mon Sep 17 00:00:00 2001 From: Sarah Date: Tue, 8 Mar 2016 15:26:36 -0800 Subject: [PATCH 08/24] added methods and specs for player class --- lib/player.rb | 6 ++++++ specs/player-spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/player.rb b/lib/player.rb index 8e636451..98857598 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,2 +1,8 @@ class Player + attr_reader :name + def initialize(name) + @name = name + + end + end diff --git a/specs/player-spec.rb b/specs/player-spec.rb index e69de29b..a8636912 100644 --- a/specs/player-spec.rb +++ b/specs/player-spec.rb @@ -0,0 +1,25 @@ +require_relative './spec_helper' +require_relative '../lib/player' + +describe Player do + it "is an object we have access to" do + Player.wont_be_nil + + end + + describe "Player#name" do + it "returns sarah when initialized with sarah" do + Player.new("sarah").name.must_equal "sarah" + + end + end + + describe "Player#plays" do + it "returns an Array of words played by the player" do + Player.new. + + end + + end + +end From 5dbf07b9ba04e34e783184d2358eb96e9995e002 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Wed, 9 Mar 2016 09:40:00 -0800 Subject: [PATCH 09/24] added comments to the scoring class. --- lib/scoring.rb | 11 ++++++++--- specs/player-spec.rb | 10 +++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/scoring.rb b/lib/scoring.rb index 818dd30f..15975763 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -22,31 +22,36 @@ def self.score(word) end end end - if letter_array.length == 7 - word_score += 50 - end + + word_score += 50 if letter_array.length == 7 word_score end def self.highest_score_from(array_of_words) word_hash = {} + # create a hash where the key is the word and the value is its score array_of_words.each do |word| word_hash[word] = score(word) end word_list = [] + # find the highest score value max_value = word_hash.max_by do |key, value| value end + # put all the words with the highest score into an array word_hash.each do |key, value| if value == max_value[1] word_list << key end end + # if the word used all 7 letters, it wins word_list.find {|word| word.length == 7} + # if no words used 7 letters, the shortest one wins + # or if multiple are shortest, the first one in the array wins word_list.min_by {|words| words.length} end diff --git a/specs/player-spec.rb b/specs/player-spec.rb index a8636912..5858a1a7 100644 --- a/specs/player-spec.rb +++ b/specs/player-spec.rb @@ -14,12 +14,12 @@ end end - describe "Player#plays" do - it "returns an Array of words played by the player" do - Player.new. + # describe "Player#plays" do + # it "returns an Array of words played by the player" do + # Player.new. - end + # end - end + # end end From 0e09d559f558067542e9790062b903c881947f99 Mon Sep 17 00:00:00 2001 From: Sarah Date: Wed, 9 Mar 2016 10:30:56 -0800 Subject: [PATCH 10/24] added play(word) method, and total_score method --- lib/player.rb | 13 ++++++++++++- specs/player-spec.rb | 33 +++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 98857598..de78cca1 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,8 +1,19 @@ class Player - attr_reader :name + require_relative './scoring' + attr_reader :name, :plays ,:total_score def initialize(name) @name = name + @plays = [] + @total_score = 0 + end + + def play(word) + @plays << word + score = Scoring.score(word) + @total_score += score + score end + end diff --git a/specs/player-spec.rb b/specs/player-spec.rb index 5858a1a7..ad03b7e9 100644 --- a/specs/player-spec.rb +++ b/specs/player-spec.rb @@ -2,6 +2,11 @@ require_relative '../lib/player' describe Player do + before do + @sarah = Player.new("sarah") + end + + it "is an object we have access to" do Player.wont_be_nil @@ -9,17 +14,33 @@ describe "Player#name" do it "returns sarah when initialized with sarah" do - Player.new("sarah").name.must_equal "sarah" + @sarah.name.must_equal "sarah" end end - # describe "Player#plays" do - # it "returns an Array of words played by the player" do - # Player.new. +describe "words played by player" do + before do + @sarah.play("cat") + end + + + it "returns the score of the word for the play(word) method" do + @sarah.play("cat").must_equal 5 + end + + + it "returns an array of words gussed for the plays method" do + @sarah.plays.must_equal ["cat"] + + end + + - # end + it "returns the total score of all words played for the total_score method" do + @sarah.total_score.must_equal 5 + end - # end + end end From e868f28a129117f09026d2143b054d66d5503332 Mon Sep 17 00:00:00 2001 From: Sarah Date: Wed, 9 Mar 2016 11:16:58 -0800 Subject: [PATCH 11/24] added return false if player has already won and the won? method --- lib/player.rb | 9 +++++++++ specs/player-spec.rb | 28 +++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index de78cca1..25481cb1 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -5,15 +5,24 @@ def initialize(name) @name = name @plays = [] @total_score = 0 + @already_won = false end def play(word) + return false if @already_won == true @plays << word score = Scoring.score(word) @total_score += score + won? score end + def won? + return false unless total_score > 100 + @already_won = true + end + + end diff --git a/specs/player-spec.rb b/specs/player-spec.rb index ad03b7e9..ba56b3a2 100644 --- a/specs/player-spec.rb +++ b/specs/player-spec.rb @@ -4,6 +4,7 @@ describe Player do before do @sarah = Player.new("sarah") + @lisa = Player.new("lisa") end @@ -21,7 +22,16 @@ describe "words played by player" do before do - @sarah.play("cat") + ABOVE_100 = %w[qz jx qzjx zjkq qq zz jj zz xqj qqqqqq] + WORDS_PLAYED = %w[cat dog zebra elephant bird cow snake puma ] + WORDS_PLAYED.each do |word| + @sarah.play(word) + end + + ABOVE_100.each do |word| + @lisa.play(word) + end + end @@ -31,16 +41,28 @@ it "returns an array of words gussed for the plays method" do - @sarah.plays.must_equal ["cat"] + @sarah.plays.must_equal WORDS_PLAYED end it "returns the total score of all words played for the total_score method" do - @sarah.total_score.must_equal 5 + @sarah.total_score.must_equal 71 + end + + it "returns false if the player has under 100 points for the won? method" do + @sarah.won?.must_equal false end + it "it will return true if the player has over 100 points for the won? methdo" do + @lisa.won?.must_equal true + end + + it "will return false if the player has already won for the play word method" do + @lisa.play("cat").must_equal false + end + end end From 736a8744cc56c262f839bc808b299819ef7d4fae Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Wed, 9 Mar 2016 11:29:51 -0800 Subject: [PATCH 12/24] added highest_scoring_word and highest_word_score methods and new specs to test these methods --- lib/player.rb | 8 ++++++++ specs/player-spec.rb | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/player.rb b/lib/player.rb index 25481cb1..be1ad01a 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -24,5 +24,13 @@ def won? @already_won = true end + def highest_scoring_word + Scoring.highest_score_from(@plays) + end + + def highest_word_score + highest_word = Scoring.highest_score_from(@plays) + Scoring.score(highest_word) + end end diff --git a/specs/player-spec.rb b/specs/player-spec.rb index ba56b3a2..416b2bb0 100644 --- a/specs/player-spec.rb +++ b/specs/player-spec.rb @@ -45,6 +45,9 @@ end + it "returns array of words equal to ABOVE_100 for the plays method" do + @lisa.plays.must_equal ABOVE_100.first(4) + end it "returns the total score of all words played for the total_score method" do @@ -63,6 +66,13 @@ @lisa.play("cat").must_equal false end + it "will return the highest scoring word from all words played using the highest_scoring_word method" do + @lisa.highest_scoring_word.must_equal "qzjx" + end + + it "will return the score of the highest word in the list using the highest_word_score method" do + @lisa.highest_word_score.must_equal 36 + end end end From bdbe49a7fbbbbdf41745520c8069074a0bf03fda Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Wed, 9 Mar 2016 15:45:45 -0800 Subject: [PATCH 13/24] added comments --- lib/player.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index be1ad01a..3da6a75e 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -10,18 +10,18 @@ def initialize(name) def play(word) - return false if @already_won == true - @plays << word + return false if @already_won == true # can't play more words if already won + @plays << word # adds word to list of words played score = Scoring.score(word) @total_score += score - won? + won? # check to see if the game is won before giving back score score end def won? return false unless total_score > 100 - @already_won = true + @already_won = true # changes to true when score > 100 end def highest_scoring_word From 14f62e11b84c3b05215ce5722ac7e3fa3b48726b Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Wed, 9 Mar 2016 16:28:23 -0800 Subject: [PATCH 14/24] added TileBag class and spec file. added methods draw_tiles and tiles_remaining --- lib/tilebag.rb | 16 ++++++++++++++++ specs/tilebag-spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 lib/tilebag.rb create mode 100644 specs/tilebag-spec.rb diff --git a/lib/tilebag.rb b/lib/tilebag.rb new file mode 100644 index 00000000..e7028606 --- /dev/null +++ b/lib/tilebag.rb @@ -0,0 +1,16 @@ +class TileBag + + def initialize + @tile_bag = %w[a b c d e f] + end + + def draw_tiles(num) + @tile_bag.shift(num) + num + end + + def tiles_remaining + @tile_bag.length + end + +end \ No newline at end of file diff --git a/specs/tilebag-spec.rb b/specs/tilebag-spec.rb new file mode 100644 index 00000000..ac4af59f --- /dev/null +++ b/specs/tilebag-spec.rb @@ -0,0 +1,27 @@ +require_relative './spec_helper' +require_relative '../lib/tilebag' + +describe TileBag do + + it "is an object we have access to" do + TileBag.new.wont_be_nil + end + + describe "tiles drawn from tile bag" do + + before do + @tiles = TileBag.new + @tiles.draw_tiles(5) + end + + it "returns number of tiles from tile bag using draw_tiles method" do + @tiles.draw_tiles(5).must_equal 5 + end + + it "returns the remaining number of tiles in the tile bag using tiles_remaining method" do + @tiles.tiles_remaining.must_equal 1 # 327 + end + + end + +end \ No newline at end of file From 46609375f5b2da8440584c526c8e7162a0eb16c0 Mon Sep 17 00:00:00 2001 From: Sarah Date: Wed, 9 Mar 2016 16:57:53 -0800 Subject: [PATCH 15/24] added tile hash and fill tile bag method --- lib/tilebag.rb | 48 ++++++++++++++++++++++++++++++++++++++++--- specs/tilebag-spec.rb | 4 ++-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/lib/tilebag.rb b/lib/tilebag.rb index e7028606..6c4ad3fd 100644 --- a/lib/tilebag.rb +++ b/lib/tilebag.rb @@ -1,11 +1,53 @@ class TileBag + TILE_HASH = { + "A" => 9, + "B" => 2, + "C" => 2, + "D" => 4, + "E" => 12, + "F" => 2, + "G" => 2, + "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 + + } + + def fill_tile_bag + tile_array = [] + + TILE_HASH.each do |letter, number| + a = letter * number + a = a.split(//) + tile_array.push *a + end + tile_array + end + def initialize - @tile_bag = %w[a b c d e f] + @tile_bag = fill_tile_bag + end def draw_tiles(num) - @tile_bag.shift(num) + @tile_bag.shuffle.shift(num) num end @@ -13,4 +55,4 @@ def tiles_remaining @tile_bag.length end -end \ No newline at end of file +end diff --git a/specs/tilebag-spec.rb b/specs/tilebag-spec.rb index ac4af59f..54c9810d 100644 --- a/specs/tilebag-spec.rb +++ b/specs/tilebag-spec.rb @@ -19,9 +19,9 @@ end it "returns the remaining number of tiles in the tile bag using tiles_remaining method" do - @tiles.tiles_remaining.must_equal 1 # 327 + @tiles.tiles_remaining.must_equal 327 end end -end \ No newline at end of file +end From 013439a01f6ed9d21d41472861c8486f320881df Mon Sep 17 00:00:00 2001 From: Sarah Date: Wed, 9 Mar 2016 16:59:39 -0800 Subject: [PATCH 16/24] fixed our problem with the spec --- specs/tilebag-spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/tilebag-spec.rb b/specs/tilebag-spec.rb index 54c9810d..f3b626e0 100644 --- a/specs/tilebag-spec.rb +++ b/specs/tilebag-spec.rb @@ -19,7 +19,7 @@ end it "returns the remaining number of tiles in the tile bag using tiles_remaining method" do - @tiles.tiles_remaining.must_equal 327 + @tiles.tiles_remaining.must_equal 97 end end From d055b6258113e4da05d61443d97a5450e36bcd22 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 10 Mar 2016 09:21:52 -0800 Subject: [PATCH 17/24] Made better test for scoring. Fixed win condition for word.length == 7 in Scoring class. fixed indentation spaces. --- lib/player.rb | 3 +-- lib/scoring.rb | 7 +++--- lib/tilebag.rb | 2 -- specs/player-spec.rb | 53 +++++++++++++++++++------------------------ specs/scoring-spec.rb | 23 +++++++++++-------- specs/tilebag-spec.rb | 2 +- 6 files changed, 42 insertions(+), 48 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 3da6a75e..9203f054 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,6 +1,7 @@ class Player require_relative './scoring' attr_reader :name, :plays ,:total_score + def initialize(name) @name = name @plays = [] @@ -8,7 +9,6 @@ def initialize(name) @already_won = false end - def play(word) return false if @already_won == true # can't play more words if already won @plays << word # adds word to list of words played @@ -18,7 +18,6 @@ def play(word) score end - def won? return false unless total_score > 100 @already_won = true # changes to true when score > 100 diff --git a/lib/scoring.rb b/lib/scoring.rb index 15975763..6a1568d6 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -37,8 +37,7 @@ def self.highest_score_from(array_of_words) word_list = [] # find the highest score value - max_value = word_hash.max_by do |key, value| value - end + max_value = word_hash.max_by { |key, value| value } # put all the words with the highest score into an array word_hash.each do |key, value| @@ -48,7 +47,9 @@ def self.highest_score_from(array_of_words) end # if the word used all 7 letters, it wins - word_list.find {|word| word.length == 7} + seven_long = word_list.find {|word| word.length == 7} + + return seven_long unless seven_long.nil? # if no words used 7 letters, the shortest one wins # or if multiple are shortest, the first one in the array wins diff --git a/lib/tilebag.rb b/lib/tilebag.rb index 6c4ad3fd..a1191d99 100644 --- a/lib/tilebag.rb +++ b/lib/tilebag.rb @@ -43,12 +43,10 @@ def fill_tile_bag def initialize @tile_bag = fill_tile_bag - end def draw_tiles(num) @tile_bag.shuffle.shift(num) - num end def tiles_remaining diff --git a/specs/player-spec.rb b/specs/player-spec.rb index 416b2bb0..beeee89e 100644 --- a/specs/player-spec.rb +++ b/specs/player-spec.rb @@ -7,62 +7,55 @@ @lisa = Player.new("lisa") end - - it "is an object we have access to" do + it "is an object we have access to" do Player.wont_be_nil - end describe "Player#name" do it "returns sarah when initialized with sarah" do @sarah.name.must_equal "sarah" - end end -describe "words played by player" do - before do - ABOVE_100 = %w[qz jx qzjx zjkq qq zz jj zz xqj qqqqqq] - WORDS_PLAYED = %w[cat dog zebra elephant bird cow snake puma ] - WORDS_PLAYED.each do |word| - @sarah.play(word) - end + describe "words played by player" do + before do + ABOVE_100 = %w[qz jx qzjx zjkq qq zz jj zz xqj qqqqqq] + WORDS_PLAYED = %w[cat dog zebra elephant bird cow snake puma ] + WORDS_PLAYED.each do |word| + @sarah.play(word) + end ABOVE_100.each do |word| @lisa.play(word) end - - end + end it "returns the score of the word for the play(word) method" do @sarah.play("cat").must_equal 5 end - - it "returns an array of words gussed for the plays method" do - @sarah.plays.must_equal WORDS_PLAYED - - end + it "returns an array of words gussed for the plays method" do + @sarah.plays.must_equal WORDS_PLAYED + end it "returns array of words equal to ABOVE_100 for the plays method" do @lisa.plays.must_equal ABOVE_100.first(4) end + it "returns the total score of all words played for the total_score method" do + @sarah.total_score.must_equal 71 + end - it "returns the total score of all words played for the total_score method" do - @sarah.total_score.must_equal 71 - end - - it "returns false if the player has under 100 points for the won? method" do - @sarah.won?.must_equal false - end + it "returns false if the player has under 100 points for the won? method" do + @sarah.won?.must_equal false + end - it "it will return true if the player has over 100 points for the won? methdo" do - @lisa.won?.must_equal true - end + it "it will return true if the player has over 100 points for the won? methdo" do + @lisa.won?.must_equal true + end - it "will return false if the player has already won for the play word method" do + it "will return false if the player has already won for the play word method" do @lisa.play("cat").must_equal false end @@ -74,5 +67,5 @@ @lisa.highest_word_score.must_equal 36 end - end + end end diff --git a/specs/scoring-spec.rb b/specs/scoring-spec.rb index 76dc3ff9..52d7a4dd 100644 --- a/specs/scoring-spec.rb +++ b/specs/scoring-spec.rb @@ -7,17 +7,17 @@ end describe "Scoring#score" do - TEST_CASES = { - "cat" => 5, - "dog" => 5, - "zoo" => 12, - "AAAAAAA" => 57 - } + TEST_CASES = { + "cat" => 5, + "dog" => 5, + "zoo" => 12, + "AAAAAAA" => 57 + } - TEST_CASES.each do |word,score| - it "should return #{score} for the word #{word}" do - Scoring.score(word).must_equal(score) + TEST_CASES.each do |word,score| + it "should return #{score} for the word #{word}" do + Scoring.score(word).must_equal(score) end end end @@ -32,7 +32,10 @@ ['JX', 'QZ'] => 'QZ', ['AAAAAAA', 'FFFFFFF'] => 'FFFFFFF', ['AAAA', 'DG'] => 'DG', - ['BDG', 'AAAAAAA', 'EEEEEEE'] => 'AAAAAAA' + ['QQQQQQ', 'FAAAAAA', 'FEEEEEE'] => 'FAAAAAA' + # old test flawed: 'BDG' and 'AAAAAAA' both have scores of 7, + # but 'AAAAAAA' got 50 bonus points and that's why it won in + # the test (not because the word.length == 7 did anything--it didn't) } TEST_ARRAYS.each do |list, word| diff --git a/specs/tilebag-spec.rb b/specs/tilebag-spec.rb index f3b626e0..9e0b19b4 100644 --- a/specs/tilebag-spec.rb +++ b/specs/tilebag-spec.rb @@ -15,7 +15,7 @@ end it "returns number of tiles from tile bag using draw_tiles method" do - @tiles.draw_tiles(5).must_equal 5 + @tiles.draw_tiles(5).length.must_equal 5 end it "returns the remaining number of tiles in the tile bag using tiles_remaining method" do From 6f91aa44a6e77fce4370fb3753362a29a7ba208c Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 10 Mar 2016 12:50:35 -0800 Subject: [PATCH 18/24] added test to check that removing tiles changes the length of the remaining tile array. --- lib/player.rb | 1 + lib/tilebag.rb | 4 ++-- specs/tilebag-spec.rb | 15 ++++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 9203f054..5e9fe7a0 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -20,6 +20,7 @@ def play(word) def won? return false unless total_score > 100 + puts "You win!" @already_won = true # changes to true when score > 100 end diff --git a/lib/tilebag.rb b/lib/tilebag.rb index a1191d99..43b6f896 100644 --- a/lib/tilebag.rb +++ b/lib/tilebag.rb @@ -40,13 +40,13 @@ def fill_tile_bag tile_array end - def initialize @tile_bag = fill_tile_bag end def draw_tiles(num) - @tile_bag.shuffle.shift(num) + @tile_bag.shuffle! + @tile_bag.shift(num) end def tiles_remaining diff --git a/specs/tilebag-spec.rb b/specs/tilebag-spec.rb index 9e0b19b4..200eb545 100644 --- a/specs/tilebag-spec.rb +++ b/specs/tilebag-spec.rb @@ -11,15 +11,24 @@ before do @tiles = TileBag.new - @tiles.draw_tiles(5) + @tiles_five_drawn = @tiles.draw_tiles(5) end it "returns number of tiles from tile bag using draw_tiles method" do - @tiles.draw_tiles(5).length.must_equal 5 + @tiles_five_drawn.length.must_equal 5 + end + + it "returns an array of tiles from the tile bag using draw_tiles method" do + @tiles_five_drawn.must_be_instance_of Array end it "returns the remaining number of tiles in the tile bag using tiles_remaining method" do - @tiles.tiles_remaining.must_equal 97 + @tiles.tiles_remaining.must_equal 92 + end + + it "returns 92 if we draw 5 tiles using the tiles_remaining method" do + @tiles.draw_tiles(5) + @tiles.tiles_remaining.must_equal 87 end end From e8e1ea71deb285630f480e62c55bf6642bc3bf06 Mon Sep 17 00:00:00 2001 From: Sarah Date: Thu, 10 Mar 2016 13:22:52 -0800 Subject: [PATCH 19/24] added draw_tile method in splyer class and the tile collection instance variable to player --- lib/player.rb | 11 +++++++++-- specs/player-spec.rb | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 5e9fe7a0..e729a1b5 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,12 +1,14 @@ +require_relative './tilebag' +require_relative './scoring' class Player - require_relative './scoring' - attr_reader :name, :plays ,:total_score + attr_reader :name, :plays ,:total_score, :tiles def initialize(name) @name = name @plays = [] @total_score = 0 @already_won = false + @tiles = [] end def play(word) @@ -33,4 +35,9 @@ def highest_word_score Scoring.score(highest_word) end + def draw_tiles(tile_bag) + @tiles.push *tile_bag.draw_tiles(7 - @tiles.length) + @tiles + + end end diff --git a/specs/player-spec.rb b/specs/player-spec.rb index beeee89e..3870090d 100644 --- a/specs/player-spec.rb +++ b/specs/player-spec.rb @@ -1,5 +1,6 @@ require_relative './spec_helper' require_relative '../lib/player' +require_relative '../lib/tilebag' describe Player do before do @@ -67,5 +68,22 @@ @lisa.highest_word_score.must_equal 36 end + + it "will return an empty array" do + @sarah.tiles.must_equal [] + end + + it "will return an array using the draw tile method" do + tile_bag = TileBag.new + @sarah.draw_tiles(tile_bag).must_be_instance_of Array + + end + + it "will return an array of length 7" do + tile_bag = TileBag.new + @sarah.draw_tiles(tile_bag).length.must_equal 7 + + end + end end From f0cb0d355a2e1a701b62bcca69fa2b9a5d2ef1f3 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 11 Mar 2016 10:21:53 -0800 Subject: [PATCH 20/24] Created a Board class that makes a 15x15 board matrix. can_be_played method checks if spaces are free or contain the same letter needed. play_word method places letters in the correct spots in the matrix. --- lib/board.rb | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ scrabble.rb | 4 ++++ 2 files changed, 62 insertions(+) create mode 100644 lib/board.rb diff --git a/lib/board.rb b/lib/board.rb new file mode 100644 index 00000000..939f7015 --- /dev/null +++ b/lib/board.rb @@ -0,0 +1,58 @@ +class Board + attr_accessor :board + + def initialize + @board = make_board + end + + def make_board + a = [] + 15.times do + a << Array.new(15) + end + + return a + end + + def can_be_played(row, col, direction, word) + can_play = true + letter_array = word.split(//) + + letter_array.each do |letter| + if @board[row][col] != nil && @board[row][col] != letter + can_play = false + end + + row, col = increment_row_or_col(row, col, direction) + end + + can_play + end + + def play_word(row, col, direction, word) + return "can't play there" if can_be_played(row, col, direction, word) == false + + letter_array = word.split(//) + + i = 0 + while i < letter_array.length + @board[row][col] = letter_array[i] + i += 1 + + row, col = increment_row_or_col(row, col, direction) + end + end + + def increment_row_or_col(row, col, direction) + if direction.downcase.include? 'right' + col += 1 + else + row += 1 + end + + return [row, col] + end + + + +end \ No newline at end of file diff --git a/scrabble.rb b/scrabble.rb index 666b99d6..29c58e05 100644 --- a/scrabble.rb +++ b/scrabble.rb @@ -1,5 +1,9 @@ module Scrabble include Scoring + + include Player + + include TileBag end From 69bc6d8e47a2f6c0e024db73e66989521b606b28 Mon Sep 17 00:00:00 2001 From: Sarah Date: Fri, 11 Mar 2016 11:06:51 -0800 Subject: [PATCH 21/24] did 10 tests for the board class --- specs/board-spec.rb | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 specs/board-spec.rb diff --git a/specs/board-spec.rb b/specs/board-spec.rb new file mode 100644 index 00000000..1071d9a9 --- /dev/null +++ b/specs/board-spec.rb @@ -0,0 +1,63 @@ +require_relative '../lib/board' +require_relative './spec_helper' + +describe Board do + before do + @board = Board.new + end + + it "is an object we have access to" do + @board.wont_be_nil + end + + + it "should be an instance of Array" do + @board.board.must_be_instance_of Array + end + + it "should return an array with a length of 15" do + @board.board.length.must_equal 15 + end + + describe "letter in indexes" do + before do + @board.play_word(0,0,'down','cat') + end + + it "index [0][0] should be c" do + @board.board[0][0].must_equal "c" + end + + it "index [1][0] should be a" do + @board.board[1][0].must_equal "a" + + end + + it "index [2][0] should be t" do + @board.board[2][0].must_equal "t" + end + + end + + describe "overlapping words on board" do + before do + @board.play_word(0,0,'down','cat') + @board.play_word(1,0,"right","apple") + end + + it "index[1][1] should be p" do + @board.board[1][1].must_equal "p" + end + + it "should return can't play there if word overlaps" do + @board.play_word(1,3,"down","dog").must_equal "can't play there" + end + + it "index [1][3] should still be l" do + @board.play_word(1,3,"down","dog") + @board.board[1][3].must_equal "l" + + end + end + +end From 6f9538929e206f87659f555a6d553660e6cf0e1d Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 11 Mar 2016 11:08:39 -0800 Subject: [PATCH 22/24] changed from attr_accessor to attr_reader for @board --- lib/board.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/board.rb b/lib/board.rb index 939f7015..56a93942 100644 --- a/lib/board.rb +++ b/lib/board.rb @@ -1,5 +1,5 @@ class Board - attr_accessor :board + attr_reader :board def initialize @board = make_board From 973db9f1748f7073f66e694d5ef46063a1eccbee Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 11 Mar 2016 13:00:40 -0800 Subject: [PATCH 23/24] added comments to board.rb --- lib/board.rb | 7 +++++-- lib/tilebag.rb | 8 ++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/board.rb b/lib/board.rb index 56a93942..80894ab0 100644 --- a/lib/board.rb +++ b/lib/board.rb @@ -7,8 +7,8 @@ def initialize def make_board a = [] - 15.times do - a << Array.new(15) + 15.times do # fills array with 15 new arrays + a << Array.new(15) # each array has 15 elements, creating a 15 x 15 grid end return a @@ -19,10 +19,12 @@ def can_be_played(row, col, direction, word) letter_array = word.split(//) letter_array.each do |letter| + # if there's something in the specific index on the board that does not match the current letter if @board[row][col] != nil && @board[row][col] != letter can_play = false end + # increment either the row or column (depending on which direction specified by player) row, col = increment_row_or_col(row, col, direction) end @@ -30,6 +32,7 @@ def can_be_played(row, col, direction, word) end def play_word(row, col, direction, word) + # do not play the word if a conflicting word is already on the board return "can't play there" if can_be_played(row, col, direction, word) == false letter_array = word.split(//) diff --git a/lib/tilebag.rb b/lib/tilebag.rb index 43b6f896..c695f8e1 100644 --- a/lib/tilebag.rb +++ b/lib/tilebag.rb @@ -29,6 +29,10 @@ class TileBag } + def initialize + @tile_bag = fill_tile_bag + end + def fill_tile_bag tile_array = [] @@ -40,10 +44,6 @@ def fill_tile_bag tile_array end - def initialize - @tile_bag = fill_tile_bag - end - def draw_tiles(num) @tile_bag.shuffle! @tile_bag.shift(num) From 01cef02b0e86c863ef0e5653d2460b0cfb0ad189 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 11 Mar 2016 14:56:14 -0800 Subject: [PATCH 24/24] added classes to scrabble module, changed words in player-spec --- lib/player.rb | 1 + scrabble.rb | 3 +++ specs/player-spec.rb | 15 +++++++-------- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index e729a1b5..9cce2a4f 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,5 +1,6 @@ require_relative './tilebag' require_relative './scoring' + class Player attr_reader :name, :plays ,:total_score, :tiles diff --git a/scrabble.rb b/scrabble.rb index 29c58e05..30bcfab7 100644 --- a/scrabble.rb +++ b/scrabble.rb @@ -5,7 +5,10 @@ module Scrabble include TileBag + include Board end require_relative './lib/scoring' require_relative './lib/player' +require_relative './lib/tilebag' +require_relative './lib/board' \ No newline at end of file diff --git a/specs/player-spec.rb b/specs/player-spec.rb index 3870090d..3f97fdab 100644 --- a/specs/player-spec.rb +++ b/specs/player-spec.rb @@ -19,9 +19,10 @@ end describe "words played by player" do + ABOVE_100 = %w[quiz array scores lizard jokes lemon class stack cherry] + WORDS_PLAYED = %w[cat dog zebra elephant bird cow snake puma] + before do - ABOVE_100 = %w[qz jx qzjx zjkq qq zz jj zz xqj qqqqqq] - WORDS_PLAYED = %w[cat dog zebra elephant bird cow snake puma ] WORDS_PLAYED.each do |word| @sarah.play(word) end @@ -41,7 +42,7 @@ end it "returns array of words equal to ABOVE_100 for the plays method" do - @lisa.plays.must_equal ABOVE_100.first(4) + @lisa.plays.must_equal ABOVE_100 end it "returns the total score of all words played for the total_score method" do @@ -52,7 +53,7 @@ @sarah.won?.must_equal false end - it "it will return true if the player has over 100 points for the won? methdo" do + it "it will return true if the player has over 100 points for the won? method" do @lisa.won?.must_equal true end @@ -61,11 +62,11 @@ end it "will return the highest scoring word from all words played using the highest_scoring_word method" do - @lisa.highest_scoring_word.must_equal "qzjx" + @lisa.highest_scoring_word.must_equal "quiz" end it "will return the score of the highest word in the list using the highest_word_score method" do - @lisa.highest_word_score.must_equal 36 + @lisa.highest_word_score.must_equal 22 end @@ -76,13 +77,11 @@ it "will return an array using the draw tile method" do tile_bag = TileBag.new @sarah.draw_tiles(tile_bag).must_be_instance_of Array - end it "will return an array of length 7" do tile_bag = TileBag.new @sarah.draw_tiles(tile_bag).length.must_equal 7 - end end