Skip to content
Open
Changes from 2 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
31 changes: 28 additions & 3 deletions blackjack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ def value
end

def to_s
"#{@value}-#{suit}"
suit = "H" if @suit == :hearts
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The way this is specified is very close to a case statement. In ruby, that looks like:

suit = case @suit
when :hearts
  "H"
when :clubs
  "C"
when :spades
  "S"
when :diamonds"
 "D"
end

However, there's a slight problem here, that if the suit is something other than a :hearts, :clubs, :spades:, :diamonds, it will be nil and cause problems later down the line. So you might:

suit = case @suit
when :hearts
  "H"
when :clubs
  "C"
when :spades
  "S"
when :diamonds
 "D"
else
  raise ArgumentError.new "Unsupported suit: #{suit}"
end

suit = "C" if @suit == :clubs
suit = "S" if @suit == :spades
suit = "D" if @suit == :diamonds

"#{@value}#{suit}"
end

end
Expand Down Expand Up @@ -55,6 +60,10 @@ def value
cards.inject(0) {|sum, card| sum += card.value }
end

def bust?
value > 21
end

def play_as_dealer(deck)
if value < 16
hit!(deck)
Expand All @@ -75,6 +84,11 @@ def initialize

def hit
@player_hand.hit!(@deck)
if @player_hand.value > 21
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

did you mean to use the "bust" method here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Indeed I did, but I definitely forgot that I defined it. That what I get for stepping away from the assignment for a few hours.

stand
else
status
end
end

def stand
Expand Down Expand Up @@ -134,8 +148,8 @@ def inspect
end

it "should be formatted nicely" do
card = Card.new(:diamonds, "A")
card.to_s.should eq("A-diamonds")
card = Card.new(:hearts, "Q")
card.to_s.should eq("QH")
end
end

Expand Down Expand Up @@ -174,6 +188,17 @@ def inspect

end

it "should bust when a player hand value exceeds 21" do
deck = mock(:deck, :cards => [Card.new(:clubs, 10),
Card.new(:diamonds, 10),
Card.new(:spades, 10)])

hand = Hand.new
3.times { hand.hit!(deck) }
hand.bust?.should eq(true)

end

describe "#play_as_dealer" do
it "should hit blow 16" do
deck = mock(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 4), Card.new(:clubs, 2), Card.new(:hearts, 6)])
Expand Down