diff --git a/README.md b/README.md index 07088686..a23da7e9 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ Create a `Scrabble` class with a minimum of 8 specs. The class should have the f ### Primary Requirements Create a `Player` class with a minimum of 11 specs. The class should have the following class and instance methods: -- `self.new(name)`: creates a new instance with the instance variable `name` assigned +- `#initialize(name)`: Called when you use `Player.new`, sets up an instance with the instance variable `name` assigned - `#name`: returns the `@name` instance variable - `#plays`: returns an Array of the words played by the player - `#play(word)`: Adds the input word to the `plays` Array @@ -71,14 +71,13 @@ Create a `Player` class with a minimum of 11 specs. The class should have the fo - `#highest_word_score`: Returns the `highest_scoring_word` score. - diff --git a/lib/player.rb b/lib/player.rb index 424077f8..ad0b9c83 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,13 +1,15 @@ module Scrabble class Player - attr_reader :name, :plays - attr_accessor :total + attr_reader :name, :plays, :tiles, :bag + def initialize(name) @name = name @plays = [] - @total = 0 + @tiles = [] + @bag = nil end + def play(word) if won? == true return false @@ -17,10 +19,11 @@ def play(word) end def total_score + total = 0 @plays.each do |word| - @total += Scrabble.score(word) + total += Scrabble.score(word) end - return @total + return total end def highest_scoring_word @@ -32,11 +35,16 @@ def highest_word_score end def won? - if @total > 100 - true - else - false + total_score > 100 + end + + def draw_tiles(tile_bag) + @bag = tile_bag + until @tiles.length == 7 do + drawn_tile = @bag.draw_tiles(1) + @tiles.push(drawn_tile) end + return @tiles end end diff --git a/lib/tilebag.rb b/lib/tilebag.rb new file mode 100644 index 00000000..ddb41e18 --- /dev/null +++ b/lib/tilebag.rb @@ -0,0 +1,22 @@ +module Scrabble + class TileBag + + attr_reader :tile_quantities + def initialize + starting_tiles = %w{A A A A A A A A A B B C C D D D D + E E E E E E E E E E E E F F G G G H H I I I I I I I I I J K + L L L L M M N N N N N N O O O O O O O O P P Q R R R R R R S S + S S T T T T T T U U U U V V W W X Y Y Z} + @tile_quantities = starting_tiles.shuffle! + end + + def draw_tiles(num) + @tile_quantities.pop(num) + end + + def tiles_remaining + @tile_quantities.length + end + + end +end diff --git a/spec/player_spec.rb b/spec/player_spec.rb index ab453dfc..5fe97380 100644 --- a/spec/player_spec.rb +++ b/spec/player_spec.rb @@ -6,11 +6,11 @@ @player2 = Scrabble::Player.new("andre") @player3 = Scrabble::Player.new("batman") @player4 = Scrabble::Player.new("spiderman") + @player5 = Scrabble::Player.new("superman") @player2.play("love") # love = 7 @player2.play("toast") # toast = 5 @player2.play("hand") # hand = 8 - @player3.total = 101 - + @player3.play("zzzzzzzzzzz") # = 110 end describe ".new" do @@ -39,7 +39,7 @@ it "adds the input word to the plays array" do expect(@player2.plays).to eq ["love", "toast", "hand"] end - it "returns false is player has already won" do + it "returns false if player has already won" do expect(@player3.play("word")).to eq false end end @@ -59,7 +59,7 @@ expect(@player2.won?).to eq false end it "returns false if player does not have over 100 points" do - @player4.total = 100 + @player4.play("zzzzzzzzzz") #word = 100 points exactly expect(@player4.won?).to eq false end end @@ -72,7 +72,24 @@ describe "#highest_word_score" do it "returns the highest scoring word score" do - expect(@player2.highest_word_score).to eq 8 + expect(@player2.highest_word_score).to eq 8 + end + end + + describe "#tiles" do + it "returns the collection of letters that the player can play" do + expect(@player5.tiles).to be_an_instance_of Array + end + it "contains a maximum of 7 tiles" do + expect(@player5.tiles.length).to be <= 7 + end + end + + describe "#draw_tiles(tile_bag)" do + it "fills tiles array until it has 7 letters from the given tile bag" do + @tilebag = Scrabble::TileBag.new + @player5.draw_tiles(@tilebag) + expect(@player5.tiles.length).to eq 7 end end diff --git a/spec/tilebag_spec.rb b/spec/tilebag_spec.rb new file mode 100644 index 00000000..92f79606 --- /dev/null +++ b/spec/tilebag_spec.rb @@ -0,0 +1,36 @@ +require "./lib/tilebag" + +describe Scrabble::TileBag do + before :each do + @tilebag1 = Scrabble::TileBag.new + @tilebag2 = Scrabble::TileBag.new + @tilebag2.draw_tiles(4) + end + + describe "#initialize" do + it "sets up an instance of the TileBag class" do + expect(@tilebag1).to be_an_instance_of Scrabble::TileBag + end + it "sets up an instance with a collection of default tiles" do + expect(@tilebag1.tile_quantities).to_not be_empty + expect(@tilebag1.tile_quantities.length).to eq 98 + end + end + + describe "#draw_tiles" do + it "returns the specifed number of random tiles" do + expect(@tilebag2.draw_tiles(4).length).to eq 4 + end + it "Removes the tiles from the default set" do + expect(@tilebag2.tile_quantities.length).to eq 94 + end + end + + describe "#tiles_remaining" do + it " returns the number of tiles remaining in the bag" do + expect(@tilebag2.tiles_remaining).to eq 94 + end + end + + +end