Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Scrabble Wave 3 #26

Open
wants to merge 14 commits into
base: dfcp+raa/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.


<!--
## Wave 3

### Primary Requirements
Create a `TileBag` class with a minimum of 5 specs. It should have the following class and instance methods:

- `self.new` creates an instance with a collection of default tiles
- `#draw_tiles(n)` returns n number of random tiles, removes the tiles from the default set.
- `#initialize` Called when you use `TileBag.new`, sets up an instance with a collection of default tiles
- `#draw_tiles(num)` returns `num` number of random tiles, removes the tiles from the default set.
- `#tiles_remaining` returns the number of tiles remaining in the bag

Create specs for (minimum 2) and add to the `Player` class the following instance methods:
Expand Down Expand Up @@ -119,4 +118,3 @@ Beginning Tile Quantities:
- Create a `Dictionary` class that includes a method for searching a list of words to determine if a given word is a valid word.
- Create a `Board` class that has a matrix (array of arrays) of tile places. Check if a word can be played on a given tile place in a certain direction.
- Include a minimum of 20 specs between the `Dictionary` and `Board` classes.
-->
26 changes: 17 additions & 9 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for having @bag as an instance variable rather than bag as a local variable?

until @tiles.length == 7 do
drawn_tile = @bag.draw_tiles(1)
@tiles.push(drawn_tile)
end
return @tiles
end

end
Expand Down
22 changes: 22 additions & 0 deletions lib/tilebag.rb
Original file line number Diff line number Diff line change
@@ -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
27 changes: 22 additions & 5 deletions spec/player_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
@player2 = Scrabble::Player.new("andre")
@player3 = Scrabble::Player.new("batman")
@player4 = Scrabble::Player.new("spiderman")
@player5 = Scrabble::Player.new("superman")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand why there are five separate Player instances in this spec file. Because each it block test is run completely independent of all other tests, none of the code from the other ones will impact the @player1 instance.

In general you only need to write a spec with more than one instance of a class if at least one test requires that multiple instances of the class interact with each other. Otherwise, it's perfectly fine to use the same instance from a before block in every test.

@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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain the reason for this particular test, and how you've chosen to write it?

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

Expand Down
36 changes: 36 additions & 0 deletions spec/tilebag_spec.rb
Original file line number Diff line number Diff line change
@@ -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