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
63 changes: 38 additions & 25 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}"
end

end
Expand All @@ -29,7 +29,7 @@ def initialize

def self.build_cards
cards = []
[:clubs, :diamonds, :spades, :hearts].each do |suit|
[:C, :D, :S, :H].each do |suit|
Copy link
Member

Choose a reason for hiding this comment

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

That's a cool wait to solve the 6H instead of 6-hearts problem. I hadn't considered it

I want to make sure you know how to turn :clubs into C though:

suit = :clubs
suit.to_s.upcase.chars.first
=> "C"

(2..10).each do |number|
cards << Card.new(suit, number)
end
Expand Down Expand Up @@ -69,21 +69,25 @@ def initialize
@deck = Deck.new
@player_hand = Hand.new
@dealer_hand = Hand.new
2.times { @player_hand.hit!(@deck) }
2.times { @player_hand.hit!(@deck) }
2.times { @dealer_hand.hit!(@deck) }
end

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


def stand
@dealer_hand.play_as_dealer(@deck)
@winner = determine_winner(@player_hand.value, @dealer_hand.value)
end

def status
{:player_cards=> @player_hand.cards,
{:player_cards=> @player_hand.cards,
:player_value => @player_hand.value,
:dealer_cards => @dealer_hand.cards,
:dealer_value => @dealer_hand.value,
Expand Down Expand Up @@ -111,31 +115,31 @@ def inspect
describe Card do

it "should accept suit and value when building" do
card = Card.new(:clubs, 10)
card.suit.should eq(:clubs)
card = Card.new(:C, 10)
card.suit.should eq(:C)
card.value.should eq(10)
end

it "should have a value of 10 for facecards" do
facecards = ["J", "Q", "K"]
facecards.each do |facecard|
card = Card.new(:hearts, facecard)
card = Card.new(:H, facecard)
card.value.should eq(10)
end
end
it "should have a value of 4 for the 4-clubs" do
card = Card.new(:clubs, 4)
card = Card.new(:C, 4)
card.value.should eq(4)
end

it "should return 11 for Ace" do
card = Card.new(:diamonds, "A")
card = Card.new(:D, "A")
card.value.should eq(11)
end

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

Expand All @@ -156,16 +160,16 @@ def inspect
describe Hand do

it "should calculate the value correctly" do
deck = mock(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 10)])
deck = mock(:deck, :cards => [Card.new(:C, 4), Card.new(:D, 10)])
hand = Hand.new
2.times { hand.hit!(deck) }
hand.value.should eq(14)
end

it "should take from the top of the deck" do
club4 = Card.new(:clubs, 4)
diamond7 = Card.new(:diamonds, 7)
clubK = Card.new(:clubs, "K")
club4 = Card.new(:C, 4)
diamond7 = Card.new(:D, 7)
clubK = Card.new(:C, "K")

deck = mock(:deck, :cards => [club4, diamond7, clubK])
hand = Hand.new
Expand All @@ -175,24 +179,24 @@ def inspect
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)])
it "should hit below 16" do
deck = mock(:deck, :cards => [Card.new(:C, 4), Card.new(:D, 4), Card.new(:C, 2), Card.new(:H, 6)])
hand = Hand.new
2.times { hand.hit!(deck) }
hand.play_as_dealer(deck)
hand.value.should eq(16)
end
it "should not hit above" do
deck = mock(:deck, :cards => [Card.new(:clubs, 8), Card.new(:diamonds, 9)])
deck = mock(:deck, :cards => [Card.new(:C, 8), Card.new(:D, 9)])
hand = Hand.new
2.times { hand.hit!(deck) }
hand.play_as_dealer(deck)
hand.value.should eq(17)
end
it "should stop on 21" do
deck = mock(:deck, :cards => [Card.new(:clubs, 4),
Card.new(:diamonds, 7),
Card.new(:clubs, "K")])
deck = mock(:deck, :cards => [Card.new(:C, 4),
Card.new(:D, 7),
Card.new(:C, "K")])
hand = Hand.new
2.times { hand.hit!(deck) }
hand.play_as_dealer(deck)
Expand Down Expand Up @@ -225,18 +229,27 @@ def inspect
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)
Game.new.determine_winner(22, 15).should eq(:dealer)
end
it "should #stand for player when busts" do
game = Game.new
while game.status[:player_value] < 21
game.hit
end
game.stand.should eq(:dealer)
end
it "should player win if dealer busts" do
Game.new.determine_winner(18, 22).should eq(:player)
Game.new.determine_winner(18, 22).should eq(:player)
end
it "should have player win if player > dealer" do
Game.new.determine_winner(18, 16).should eq(:player)
Game.new.determine_winner(18, 16).should eq(:player)
end
it "should have push if tie" do
Game.new.determine_winner(16, 16).should eq(:push)
Game.new.determine_winner(16, 16).should eq(:push)
end
end
end