Skip to content
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
18 changes: 9 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
rspec (2.11.0)
rspec-core (~> 2.11.0)
rspec-expectations (~> 2.11.0)
rspec-mocks (~> 2.11.0)
rspec-core (2.11.1)
rspec-expectations (2.11.2)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.1)
diff-lcs (1.2.4)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
rspec-mocks (~> 2.13.0)
rspec-core (2.13.1)
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.1)

PLATFORMS
ruby
Expand Down
126 changes: 107 additions & 19 deletions blackjack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ def value
end

def to_s
"#{@value}-#{suit}"
abbrev = {
clubs: "C",
diamonds: "D",
spades: "S",
hearts: "H"
}
"#{@value}#{abbrev[@suit]}"
end

end
Expand Down Expand Up @@ -42,43 +48,77 @@ def self.build_cards
end

class Hand
attr_reader :cards

def initialize
@cards = []
end

def hit!(deck)
@cards << deck.cards.shift
end

def cards
@cards.map {|card| card.to_s }
end

def value
@cards.inject(0) {|sum, card| sum += card.value }
Copy link
Member

Choose a reason for hiding this comment

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

Good job with inject here.

end
end

class DealerHand < Hand
def initialize
super
@played = false
end

def cards
if @played
super
else
#hide first card
["XX"] + @cards[1..-1].map {|card| card.to_s}
end
end

def value
cards.inject(0) {|sum, card| sum += card.value }
if @played
super
else
nil
end
end

def play_as_dealer(deck)
def play(deck)
@played = true
if value < 16
hit!(deck)
play_as_dealer(deck)
play(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) }

stand if @player_hand.value == 21
end

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

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

Expand All @@ -105,6 +145,10 @@ def determine_winner(player_value, dealer_value)
def inspect
status
end

def finished?
@winner != nil
end
end


Expand Down Expand Up @@ -133,9 +177,9 @@ def inspect
card.value.should eq(11)
end

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

Expand Down Expand Up @@ -170,32 +214,57 @@ def inspect
deck = mock(:deck, :cards => [club4, diamond7, clubK])
hand = Hand.new
2.times { hand.hit!(deck) }
hand.cards.should eq([club4, diamond7])
hand.cards.should eq([club4.to_s, diamond7.to_s])

end
end

describe DealerHand do

it "should hide the dealer's first card and value until dealer has played" do
Copy link
Member

Choose a reason for hiding this comment

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

In general, sticking to a 1 assertion per specifications (1 should per 'it') is a good practice. In a workflow like this, I'm cool with what you did, although I'd want to move the setup of the hand to a before block.

club4 = Card.new(:clubs, 4)
diamond7 = Card.new(:diamonds, 7)
clubK = Card.new(:clubs, "K")

deck = mock(:deck, :cards => [club4, diamond7, clubK])
dealer_hand = DealerHand.new

2.times { dealer_hand.hit!(deck) }

dealer_hand.cards[0].should eq("XX")
dealer_hand.value.should be_nil

dealer_hand.play(deck)

dealer_hand.cards[0].should eq(club4.to_s)
dealer_hand.value.should eq(21)
end

describe "#play" do

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)])
hand = Hand.new
hand = DealerHand.new
2.times { hand.hit!(deck) }
hand.play_as_dealer(deck)
hand.play(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.play(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")])
hand = Hand.new
hand = DealerHand.new
2.times { hand.hit!(deck) }
hand.play_as_dealer(deck)
hand.play(deck)
hand.value.should eq(21)
end
end
Expand All @@ -222,7 +291,26 @@ def inspect
it "should play the dealer hand when I stand" do
game = Game.new
game.stand
game.status[:winner].should_not be_nil
game.finished?.should be_true
end


it "should stand for the player if a player busts" do
game = Game.new
while game.player_hand.value <= 21
game.hit
end
game.finished?.should be_true
end

it "should stand for the player if a player gets 21 exactly" do
game = Game.new
while game.player_hand.value < 21
game.hit
#recreate a game if the player busts
game = Game.new if game.player_hand.value > 21
Copy link
Member

Choose a reason for hiding this comment

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

nice touch!

end
game.finished?.should be_true
end

describe "#determine_winner" do
Expand Down