-
Notifications
You must be signed in to change notification settings - Fork 45
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ def value | |
end | ||
|
||
def to_s | ||
"#{@value}-#{suit}" | ||
"#{@value}#{@suit[0,1].capitalize}" | ||
end | ||
|
||
end | ||
|
@@ -47,6 +47,7 @@ class Hand | |
def initialize | ||
@cards = [] | ||
end | ||
|
||
def hit!(deck) | ||
@cards << deck.cards.shift | ||
end | ||
|
@@ -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 | ||
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) | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 | ||
|
||
|
@@ -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 | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice :)