diff --git a/RakeFile b/RakeFile new file mode 100644 index 00000000..f45f7fcf --- /dev/null +++ b/RakeFile @@ -0,0 +1,10 @@ +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/lib/.DS_Store b/lib/.DS_Store new file mode 100644 index 00000000..34c7db24 Binary files /dev/null and b/lib/.DS_Store differ diff --git a/lib/player.rb b/lib/player.rb new file mode 100644 index 00000000..66161c10 --- /dev/null +++ b/lib/player.rb @@ -0,0 +1,91 @@ +class Player + attr_accessor :name + + def initialize (name) + @name = name + end + + def hash_players + hash_players = { + "Suzanne" => ["cramps", "develop", "mixup", "doggies"], + "Jeremy" => ["zebra", "naysays", "pigsty"], + "Nadine" => ["mice"] + } + end + + def plays + played_words = [] + hash_players.each do |name, words| + if @name == name + played_words += words + end + end + return played_words + end + + def self.score(word) + super + end + + def play(word) + if won? == true + return false + else + hash_players.each do |name, words| + if @name == name + words << word + end + end + return Scoring.score(word) + end + end + + def total_score + total_array_values = 0 + hash_players.each do |name, array| + if @name == name + array.each do |word| + total_array_values += Scoring.score(word) + end + end + end + return total_array_values + end + + def won? + if total_score >100 + return true + else + return false + end + end + + def highest_scoring_word + win_word = "" + hash_players.each do |name,array| + if @name == name + win_word = Scoring.highest_score_from(array) + end + end + return win_word + end + + def highest_word_score + highest_score = 0 + highest_score = Scoring.score(highest_scoring_word) + end + + tiles = [] + + def tiles + self.draw_tiles + return tiles + end + + def draw_tiles + tiles = self.tiles + tiles_needed = 7 - tiles.length + tiles = Tilebag.draw_tiles(tiles_needed) + end + +end diff --git a/lib/scoring.rb b/lib/scoring.rb new file mode 100644 index 00000000..cda748b5 --- /dev/null +++ b/lib/scoring.rb @@ -0,0 +1,50 @@ +class Scoring + #some sort of data structure to store the individual letter scores + SCORE_CHART = { + "A"=>1, "B"=>3, "C"=>3, "D"=>2, + "E"=>1, "F"=>4, "G"=>2, "H"=>4, + "I"=>1, "J"=>8, "K"=>5, "L"=>1, + "M"=>3, "N"=>1, "O"=>1, "P"=>3, + "Q"=>10, "R"=>1, "S"=>1, "T"=>1, + "U"=>1, "V"=>4, "W"=>4, "X"=>8, + "Y"=>4, "Z"=>10 + } + + def self.score(word) + word_array = word.upcase.split("") + + word_score = 0 + + if word.length <= 7 + word_array.each do |letter| + word_score += SCORE_CHART[letter] + #returns score value for given word. word input as string. + end + #seven letter word get 50point bonus + word.length == 7 ? word_score += 50 : word_score + end + return word_score + end + + def self.highest_score_from(array_of_words) + win_score = 0 + win_word = array_of_words[0] + + + array_of_words.each do |word| + score_inst = self.score(word) + #checks if current winning score is less than or equal + #to the current word's score, then uses the tiebreaker + #conditions to determine winning word and score + if score_inst > win_score + win_score = score_inst + win_word = word + elsif score_inst == win_score && word.length < win_word.length + win_word = word + win_score = score_inst + end + end + return win_word + + end +end diff --git a/lib/tilebag.rb b/lib/tilebag.rb new file mode 100644 index 00000000..117b5ecb --- /dev/null +++ b/lib/tilebag.rb @@ -0,0 +1,42 @@ +class Tilebag + attr_reader :tilebag + + def initialize + #@tilebag = {} + @tilebag = { + "a"=>9, "b"=>2, "c"=>2, "d"=>4, "e"=>12, "f"=>2, "g"=>3, "h"=>2, "i"=>9, + "j"=>1, "k"=>1, "l"=>4, "m"=>2, "n"=>6, "o"=>8, "p"=>2, "q"=>1, "r"=>6, + "s"=>4, "t"=>6, "u"=>4, "v"=>2, "w"=>2, "x"=>1, "y"=>2, "z"=>1} + + end + + def draw_tiles(num) + keys = [] + letters = [] + count = 0 + + if self.tiles_remaining >= num + while count < num + keys = (@tilebag.keys).shuffle + letter = keys[0] + if @tilebag[letter] > 0 + @tilebag[letter] = @tilebag[letter] - 1 + letters << letter + count = count + 1 + end + end + return letters + else + return "Out of tiles" + end + + + end + + def tiles_remaining + values = @tilebag.values + remaining = values.inject(0) { |sum, item| sum + item } + return remaining + end + +end diff --git a/scrabble.rb b/scrabble.rb new file mode 100644 index 00000000..e88cf6c2 --- /dev/null +++ b/scrabble.rb @@ -0,0 +1,7 @@ + + +module Scrabble + require_relative "./lib/scoring.rb" + require_relative "./lib/player.rb" + require_relative "./lib/tilebag.rb" +end diff --git a/specs/.DS_Store b/specs/.DS_Store new file mode 100644 index 00000000..1d8b7d6f Binary files /dev/null and b/specs/.DS_Store differ diff --git a/specs/player_spec.rb b/specs/player_spec.rb new file mode 100644 index 00000000..e7966cac --- /dev/null +++ b/specs/player_spec.rb @@ -0,0 +1,85 @@ +require_relative "./spec_helper" +require_relative "../scrabble" + + +describe Player do + it "it is an object we have acccess to" do + Player.wont_be_nil + end +end + + describe "Player#name" do + suzanne = Player.new("Suzanne") + it "should return Suzanne" do + suzanne.name.must_equal("Suzanne") + end + end + + describe "Player#plays" do + suzanne = Player.new("Suzanne") + it "should return words:" do + suzanne.plays.must_equal(["cramps", "develop", "mixup", "doggies"]) + end + end + + describe "Player#play(word)" do + suzanne = Player.new("Suzanne") + nadine = Player.new("Nadine") + it "adds input word to the array" do + suzanne.play("spider").must_equal(false) + end + it "adds input word to the array" do + nadine.play("cat").must_equal(5) + end + end + + describe "Player#total_score" do + suzanne = Player.new("Suzanne") + it "should return 151 for Suzanne" do + suzanne.total_score.must_equal(151) + end + end + + describe "Player#won?" do + suzanne = Player.new("Suzanne") + it "return true for player with >100 points" do + suzanne.won?.must_equal(true) + end + end + + describe "Player#highest_scoring_word" do + suzanne = Player.new("Suzanne") + nadine = Player.new("Nadine") + jeremy = Player.new("Jeremy") + it "should return highest scoring played word" do + suzanne.highest_scoring_word.must_equal("develop") + end + it "should return highest scoring played word" do + nadine.highest_scoring_word.must_equal("mice") + end + it "should return highest scoring played word" do + jeremy.highest_scoring_word.must_equal("naysays") + end + end + + describe "Player#highest_word_score" do + suzanne = Player.new("Suzanne") + it "should return highest word score" do + suzanne.highest_word_score.must_equal(63) + end + end + + describe "Player#tiles" do + suzanne = Player.new("Suzanne") + it "should return a collection of letters can play (max 7)" do + suzanne.tiles.must_equal.Array + end + end + + describe "Player#draw_tiles(tile_bag)" do + suzanne = Player.new("Suzanne") + it "should fill tiles array until it has 7 letters" do + suzanne.draw_tiles + suzanne.tiles.length.must_equal(7) + end + end diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb new file mode 100644 index 00000000..d0ce5613 --- /dev/null +++ b/specs/scoring_spec.rb @@ -0,0 +1,46 @@ +require_relative "./spec_helper" +require_relative "../scrabble" + + +describe Scoring do + it "it is an object we have acccess to" do + Scoring.wont_be_nil + end + + TEST_SCORE = { + "jogged" => 16, + "zebra" => 16, + "xylophone" => 0, + "develop" => 63, + "pigsty" => 12, + "doggies" => 60, + "zenith" => 18, + "cramps" => 12, + "naysays" => 63, + "mixup" => 16 + } + + HASH_WORDS = { + "jogged" => ["jogged"], + "mixup" => ["mixup", "jogged", "pigsty", "zebra", "cramps"], + "zebra" => ["jogged", "zebra", "pigsty", "mixup", "cramps"], + "zenith" => ["jogged", "zebra", "pigsty", "mixup", "cramps", "zenith"], + "develop" => ["jogged", "zebra", "develop", "pigsty", "doggies", "naysays", "mixup", "cramps", "zenith"] + } + + describe "Scoring#score" do + TEST_SCORE.each do |word, score| + it "should return #{ score } for '#{ word }'" do + Scoring.score(word).must_equal(score) + end + end + end + + describe "Scoring#highest_score_from" do + HASH_WORDS.each do |word,array| + it "should return #{word} with the highest score and tiebreaker" do + Scoring.highest_score_from(array).must_equal(word) + end + end + end +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..3ccb41dd --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,11 @@ +require "simplecov" +SimpleCov.start + +require "minitest" +require "minitest/spec" +require "minitest/autorun" +require "minitest/reporters" + +#give us some really pretty output +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + diff --git a/specs/tilebag_spec.rb b/specs/tilebag_spec.rb new file mode 100644 index 00000000..dcf468ab --- /dev/null +++ b/specs/tilebag_spec.rb @@ -0,0 +1,54 @@ +require_relative "./spec_helper" +require_relative "../scrabble" + +describe Tilebag do + it "is an object we have access to" do + Tilebag.wont_be_nil + end +end + +describe "Tilebag#tdraw_tiles" do + nadine_bag = Tilebag.new + it "should return out of tiles" do + nadine_bag.draw_tiles(100).must_equal("Out of tiles") + end +end + +describe "Tilebag#tiles_remaining" do + nadine_bag = Tilebag.new + it "should return the 98 tiles remaining in the game" do + nadine_bag.tiles_remaining.must_equal(98) + end +end + +describe "Tilebag#tiles_remaining2" do + suzanne_bag= Tilebag.new + it "should return the 91 tiles remaining in the game" do + suzanne_bag.draw_tiles(7) + suzanne_bag.tiles_remaining.must_equal(91) + end +end + +describe "Tilebag#tiles_remaining3" do + jeremy_bag=Tilebag.new + it "should return the 0 tiles remaining in the game" do + jeremy_bag.draw_tiles(98) + jeremy_bag.tiles_remaining.must_equal(0) + end +end + + + + + + # TILEBAG = { + # "a"=>9, "b"=>2, "c"=>2, "d"=>4, "e"=>12, "f"=>2, "g"=>3, "h"=>2, "i"=>9, + # "j"=>1, "k"=>1, "l"=>4, "m"=>2, "n"=>6, "o"=>8, "p"=>2, "q"=>1, "r"=>6, + # "s"=>4, "t"=>6, "u"=>4, "v"=>2, "w"=>2, "x"=>1, "y"=>2, "z"=>1 + # } + # + # describe "Tilebag#tilebag" do + # bag = Tilebag.new + # it "should give an array with all tiles" do + # bag.must_equal(TILEBAG) + # end