Skip to content

Feature: Completed all examples #39

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
40 changes: 40 additions & 0 deletions .idea/Episode1-Summer2012.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/scopes/scope_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 50 additions & 24 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[0,1].capitalize}"
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 All @@ -55,39 +56,32 @@ def value
cards.inject(0) {|sum, card| sum += card.value }
end

def play_as_dealer(deck)
if value < 16
hit!(deck)
play_as_dealer(deck)
end
end
end

class Game
attr_reader :player_hand, :dealer_hand
def initialize
@deck = Deck.new
@player_hand = Hand.new
@dealer_hand = Hand.new
@dealer_hand = DealerHand.new
Copy link
Member

Choose a reason for hiding this comment

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

Nice :)

2.times { @player_hand.hit!(@deck) }
2.times { @dealer_hand.hit!(@deck) }
end

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

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

def status
{:player_cards=> @player_hand.cards,
:player_value => @player_hand.value,
:dealer_cards => @dealer_hand.cards,
:dealer_value => @dealer_hand.value,
:winner => @winner}
def stand
@dealer_hand.play_as_dealer(@deck)
@winner = determine_winner(@player_hand.value, @dealer_hand.value)
end

def determine_winner(player_value, dealer_value)
Expand All @@ -102,11 +96,32 @@ def determine_winner(player_value, dealer_value)
end
end

def inspect
status
def status
if @dealer_hand.played
@thing = @dealer_hand.cards
Copy link
Member

Choose a reason for hiding this comment

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

  1. @thing is generally a terrible name for a variable.
  2. I think it might be possible that the dealer-hand should be responsible for showing its cards
dealer_hand.show_cards(game)

or something

else
@thing = ['X','X']
end

{:player_cards=> @player_hand.cards,
:player_value => @player_hand.value,
:dealer_cards => @thing,
:dealer_value => @dealer_hand.value,
:winner => @winner}
end
end

class DealerHand < Hand
attr_accessor :played

def play_as_dealer(deck)
@played = 1
if value < 16
hit!(deck)
play_as_dealer(deck)
end
end
end

describe Card do

Expand Down Expand Up @@ -134,8 +149,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(:diamonds, "Q")
card.to_s.should eq("QD")
end
end

Expand All @@ -155,6 +170,17 @@ def inspect

describe Hand do

it "player should bust after 21" do
game = mock(:game)
deck = mock(:deck, :cards => [Card.new(:clubs, 5),
Card.new(:diamonds, 7),
Card.new(:clubs, "K"),
Card.new(:diamonds, 5)])
hand = Hand.new
4.times { hand.hit!(deck) }
game.should_receive(:bust)
Copy link
Member

Choose a reason for hiding this comment

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

You can't set a message_expectation at the end of a "it" block and expect it to do anything. It can only tell what happens after you set "should_receive"

Try this and see if it works:

it "player should bust after 21" do
  game = mock(:game)
  game.should_receive(:bust)
  deck = mock(:deck, :cards => [Card.new(:clubs, 5),
                                Card.new(:diamonds, 7),
                                Card.new(:clubs, "K"),
                                Card.new(:diamonds, 5)])
  hand = Hand.new
  4.times { hand.hit!(deck) }
end

The thing I'm noticing: the game doesn't know about the hand. So, maybe you need to use a real Game object and send in the mocked deck.

end

it "should calculate the value correctly" do
deck = mock(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 10)])
hand = Hand.new
Expand All @@ -175,16 +201,16 @@ def inspect
end

describe "#play_as_dealer" do
it "should hit blow 16" do
it "should hit below 16" do
deck = mock(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 4), Card.new(:clubs, 2), Card.new(:hearts, 6)])
hand = Hand.new
hand = DealerHand.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)])
hand = Hand.new
hand = DealerHand.new
2.times { hand.hit!(deck) }
hand.play_as_dealer(deck)
hand.value.should eq(17)
Expand All @@ -193,7 +219,7 @@ def inspect
deck = mock(:deck, :cards => [Card.new(:clubs, 4),
Card.new(:diamonds, 7),
Card.new(:clubs, "K")])
hand = Hand.new
hand = DealerHand.new
2.times { hand.hit!(deck) }
hand.play_as_dealer(deck)
hand.value.should eq(21)
Expand Down