From 4472cc95fca8f3a1e2a8e6134e3b630514e06467 Mon Sep 17 00:00:00 2001 From: jason perez Date: Fri, 31 Jan 2014 13:31:59 -0800 Subject: [PATCH 1/4] add feature to change a cards to_s --- blackjack.rb | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/blackjack.rb b/blackjack.rb index b6dcda9..3176e0d 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -14,7 +14,7 @@ def value end def to_s - "#{@value}-#{suit}" + "#{@value}#{suit.to_s[0].upcase}" end end @@ -69,7 +69,7 @@ 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 @@ -83,7 +83,7 @@ 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, @@ -135,7 +135,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 @@ -156,7 +156,7 @@ 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) @@ -164,10 +164,10 @@ def inspect 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]) @@ -176,22 +176,22 @@ def inspect 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) } @@ -227,16 +227,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 From 6761ac8a37fb6bc3a8837749c07cd0162fbdb762 Mon Sep 17 00:00:00 2001 From: jason perez Date: Fri, 31 Jan 2014 13:51:09 -0800 Subject: [PATCH 2/4] add ability to stand for the player if player busts --- blackjack.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/blackjack.rb b/blackjack.rb index 3176e0d..41bab44 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -75,6 +75,11 @@ def initialize def hit @player_hand.hit!(@deck) + if status[:player_value] > 21 + stand + else + player_hand.cards + end end def stand @@ -225,6 +230,12 @@ 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 + describe "#determine_winner" do it "should have dealer win when player busts" do Game.new.determine_winner(22, 15).should eq(:dealer) From baa9a526d3938cf09cfbfed0fa9afb7b51b53357 Mon Sep 17 00:00:00 2001 From: jason perez Date: Fri, 31 Jan 2014 22:50:10 -0800 Subject: [PATCH 3/4] add placeholder to hide a dealer card --- blackjack.rb | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/blackjack.rb b/blackjack.rb index 41bab44..ef94be8 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -42,7 +42,7 @@ def self.build_cards end class Hand - attr_reader :cards + attr_accessor :cards def initialize @cards = [] @@ -51,8 +51,12 @@ def hit!(deck) @cards << deck.cards.shift end + def placeholder + @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) @@ -70,7 +74,8 @@ def initialize @player_hand = Hand.new @dealer_hand = Hand.new 2.times { @player_hand.hit!(@deck) } - 2.times { @dealer_hand.hit!(@deck) } + @dealer_hand.placeholder + @dealer_hand.hit!(@deck) end def hit @@ -83,6 +88,8 @@ def hit 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 @@ -176,7 +183,13 @@ def inspect 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 @@ -236,6 +249,11 @@ def inspect 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) From f6715ae0cd6ad45194d212cd3dcd6363614d8cd4 Mon Sep 17 00:00:00 2001 From: jason perez Date: Mon, 3 Feb 2014 12:57:08 -0800 Subject: [PATCH 4/4] add dealer hand as a hand subclass that has a placeholder card --- blackjack.rb | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/blackjack.rb b/blackjack.rb index ef94be8..b45cd2d 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -51,10 +51,6 @@ def hit!(deck) @cards << deck.cards.shift end - def placeholder - @cards << Card.new("X", "X") - end - def value cards.inject(0) {|sum, card| sum += card.value.to_i } end @@ -67,12 +63,18 @@ def play_as_dealer(deck) end end +class DealerHand < Hand + def placeholder + @cards << Card.new("X", "X") + 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) } @dealer_hand.placeholder @dealer_hand.hit!(@deck) @@ -185,13 +187,6 @@ def inspect 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 = double(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 4), Card.new(:clubs, 2), Card.new(:hearts, 6)]) @@ -219,6 +214,16 @@ def inspect end end +describe DealerHand do + + it "should have a XX placeholder card for the dealer" do + dealer_hand = DealerHand.new + dealer_hand.placeholder + dealer_hand.cards.first.suit.should eq("X") + dealer_hand.cards.first.value.should eq("X") + end +end + describe Game do