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
23 changes: 21 additions & 2 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}"
"#{suit[0].upcase}#{@value}"
end

end
Expand Down Expand Up @@ -47,6 +47,7 @@ class Hand
def initialize
@cards = []
end

def hit!(deck)
@cards << deck.cards.shift
end
Expand Down Expand Up @@ -75,6 +76,11 @@ def initialize

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

def stand
Expand Down Expand Up @@ -135,7 +141,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("DA")
end
end

Expand All @@ -150,6 +156,12 @@ def inspect
Deck.new.cards.length.should eq(52)
end

# if shuffling is truly random, there is a 1 in 52! chance two
# new decks will be identical (8x10^57), hence this test
it "should return a shuffled deck" do
Deck.new.cards.should_not eq(Deck.new.cards)
end

end


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

it "should stand automatically when the player busts" do
game = Game.new
game.hit while game.status[:player_value] < 22
Copy link
Member

Choose a reason for hiding this comment

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

Nice use of the inline while here! Reads very nicely.

Copy link
Member

Choose a reason for hiding this comment

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

Do you think if the player's value was 21, that case would be handled appropriately? Or would it try to 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