Skip to content
Open
Changes from 1 commit
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
74 changes: 59 additions & 15 deletions blackjack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ def value
end

def to_s
"#{@value}-#{suit}"
"#{@value}#{suit.to_s.chars.first.upcase}"
end

end


Expand Down Expand Up @@ -55,6 +54,16 @@ def value
cards.inject(0) {|sum, card| sum += card.value }
end

def dealer_cards
return "[XX, #{@cards[1]}]"
end

def bust?
if 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.

You should write this method as

def bust?
  value > 21
end

The reason --- if you're doing an "if" that returns true/false, your condition is already what you want

Copy link
Author

Choose a reason for hiding this comment

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

Ah! Thanks for pointing this out. There's no reason for the if. Exactly.

true
end
end

def play_as_dealer(deck)
if value < 16
hit!(deck)
Expand All @@ -69,12 +78,19 @@ 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 a player busts (goes over 21), the game should #standfor the player.
standfor if @player_hand.bust?
Copy link
Member

Choose a reason for hiding this comment

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

this looks great. I don't really like the method name "standfor" though

Copy link
Author

Choose a reason for hiding this comment

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

Thanks. That's an easy fix. I had named it standfor because that's how the Tiger level instruction reads.

end

def standfor
puts "Player Busts at #{@player_hand.value}"
@winner = :dealer
end

def stand
Expand All @@ -83,10 +99,18 @@ def stand
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,
:dealer_cards => if @winner.nil?
Copy link
Member

Choose a reason for hiding this comment

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

you should extract this if/else into another method. here it looks a bit tangled up and is hard to read. if you had a "dealer_cards" method that had much this same logic, you'd be golden

Copy link
Author

Choose a reason for hiding this comment

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

Ok. I knew that wasn't the best way to write that, but I wasn't sure where exactly to put that code. It makes sense to make it an additional method within the Hand class. Thanks!

@dealer_hand.dealer_cards
else
@dealer_hand.cards
end,
:dealer_value => if @winner.nil?
"?"
else
@dealer_hand.value
end,
:winner => @winner}
end

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

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

it "should show QH instead of queen-hearts" do
card = Card.new(:hearts, "Q")
card.to_s.should eq("QH")
end

it "should show 7C instead of 7-clubs" do
card = Card.new(:clubs, "7")
card.to_s.should eq("7C")
end
end

Expand All @@ -162,9 +196,19 @@ def inspect
hand.value.should eq(14)
end

it "should calculate a bust" do
deck = mock(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 10), Card.new(:hearts, 2), Card.new(:spades, "A")])
hand = Hand.new
2.times { hand.hit!(deck) }
hand.value.should eq(14)
2.times { hand.hit!(deck) }
hand.value.should eq(27)
hand.bust?.should eq(true)
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])
Expand All @@ -190,8 +234,8 @@ def inspect
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 = mock(: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 @@ -227,16 +271,16 @@ def inspect

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