Skip to content
Open
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
44 changes: 40 additions & 4 deletions blackjack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def value
end

def to_s
"#{@value}-#{suit}"
"#{@value}#{(suit.to_s.chars.first.upcase)}"
Copy link
Member

Choose a reason for hiding this comment

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

This is getting a little unwieldy. Could be better to either:

  1. Have a Suit class with its own to_s method (lots of work)
  2. Offload this logic to another method, like display_suit
def to_s
  [@value, display_suit].join('-')
end

def display_suit
  suit.to_s.chars.first.upcase
end

end

end
Expand Down Expand Up @@ -75,6 +75,9 @@ def initialize

def hit
@player_hand.hit!(@deck)
if @player_hand.value > 21
stand
end
end

def stand
Expand All @@ -85,11 +88,31 @@ def stand
def status
{:player_cards=> @player_hand.cards,
:player_value => @player_hand.value,
:dealer_cards => @dealer_hand.cards,
:dealer_value => @dealer_hand.value,
:dealer_cards => dealer_card_display,
:dealer_value => dealer_value_display,
:winner => @winner}
end

def dealer_card_display
if @winner.nil?
hidden_dealer_cards = Array.new
@dealer_hand.cards.each {
hidden_dealer_cards << 'X'
}
hidden_dealer_cards
else
@dealer_hand.cards
end
end

def dealer_value_display
if @winner.nil?
"can't tell you"
else
@dealer_hand.value
end
end

def determine_winner(player_value, dealer_value)
return :dealer if player_value > 21
return :player if dealer_value > 21
Expand Down Expand Up @@ -135,7 +158,7 @@ def inspect

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

Expand Down Expand Up @@ -225,6 +248,19 @@ def inspect
game.status[:winner].should_not be_nil
end

it "should stand for player if player busts (goes over 21)" do
deck = mock(:deck, :cards => [Card.new(:clubs, "Q"),
Card.new(:diamonds, "J"),
Card.new(:spades, 3),
Card.new(:diamonds, 10),
Card.new(:clubs, "K")])
game = Game.new
game.hit
game.status[:winner].should_not be_nil


end

describe "#determine_winner" do
it "should have dealer win when player busts" do
Game.new.determine_winner(22, 15).should eq(:dealer)
Expand Down