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

Panda, Tiger, Eagle #38

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 3 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
65 changes: 47 additions & 18 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[0].upcase}"
end

end
Expand Down Expand Up @@ -42,7 +42,7 @@ def self.build_cards
end

class Hand
attr_reader :cards
attr_accessor :cards

def initialize
@cards = []
Expand All @@ -51,8 +51,12 @@ def hit!(deck)
@cards << deck.cards.shift
end

def placeholder
Copy link
Member

Choose a reason for hiding this comment

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

Should all Hand's have the ability to do a placeholder?

Or, should a DealerHand < Hand exist?

Copy link
Author

Choose a reason for hiding this comment

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

Good one, thanks!

@cards << Card.new("X", "X")
end

def value
cards.inject(0) {|sum, card| sum += card.value }
cards.inject(0) {|sum, card| sum += card.value.to_i }
end

def play_as_dealer(deck)
Expand All @@ -69,21 +73,29 @@ def initialize
@deck = Deck.new
@player_hand = Hand.new
@dealer_hand = Hand.new
2.times { @player_hand.hit!(@deck) }
2.times { @dealer_hand.hit!(@deck) }
2.times { @player_hand.hit!(@deck) }
@dealer_hand.placeholder
@dealer_hand.hit!(@deck)
end

def hit
@player_hand.hit!(@deck)
if status[:player_value] > 21
stand
else
player_hand.cards
end
end

def stand
@dealer_hand.cards.shift
@dealer_hand.hit!(@deck)
@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 @@ -135,7 +147,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 All @@ -156,42 +168,48 @@ 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 = double(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 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)
diamond7 = Card.new(:diamonds, 7)
clubK = Card.new(:clubs, "K")

deck = mock(:deck, :cards => [club4, diamond7, clubK])
deck = double(:deck, :cards => [club4, diamond7, clubK])
hand = Hand.new
2.times { hand.hit!(deck) }
hand.cards.should eq([club4, diamond7])
end

it "should have a XX placeholder card for the dealer" do
hand = Hand.new
hand.placeholder
hand.cards.first.suit.should eq("X")
hand.cards.first.value.should eq("X")
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)])
deck = double(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 4), Card.new(:clubs, 2), Card.new(:hearts, 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 = double(:deck, :cards => [Card.new(:clubs, 8), Card.new(:diamonds, 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),
deck = double(:deck, :cards => [Card.new(:clubs, 4),
Card.new(:diamonds, 7),
Card.new(:clubs, "K")])
hand = Hand.new
2.times { hand.hit!(deck) }
Expand Down Expand Up @@ -225,18 +243,29 @@ def inspect
game.status[:winner].should_not be_nil
end

it "should stand for the player if the player busts" do
game = Game.new
12.times {game.hit}
game.status[:winner].should_not be_nil
end

it "should not show the dealer's first card until the player stands" do
game = Game.new
game.status[:dealer_cards].first.to_s.should eq("XX")
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 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