From faa32381811d4a16bb3a54a4d70d769ad2f7f131 Mon Sep 17 00:00:00 2001 From: ardder Date: Thu, 27 Jun 2013 11:35:31 +0800 Subject: [PATCH 1/4] finish panda level --- Gemfile.lock | 18 +++++++++--------- blackjack.rb | 12 +++++++++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 986a95b..1f9ee0e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/blackjack.rb b/blackjack.rb index b6dcda9..979e211 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -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 @@ -133,9 +139,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 From b7dc9f04803052eba38595e51683260ab9f59250 Mon Sep 17 00:00:00 2001 From: ardder Date: Thu, 27 Jun 2013 12:25:35 +0800 Subject: [PATCH 2/4] Tiger level finished --- blackjack.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/blackjack.rb b/blackjack.rb index 979e211..81ed5d6 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -81,6 +81,7 @@ def initialize def hit @player_hand.hit!(@deck) + stand if @player_hand.value > 21 end def stand @@ -231,6 +232,15 @@ def inspect game.status[:winner].should_not be_nil 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.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 3b55387daeba4605d41752ce8915d9333c288059 Mon Sep 17 00:00:00 2001 From: ardder Date: Sun, 30 Jun 2013 16:50:55 +0800 Subject: [PATCH 3/4] refactoring for tiger level --- blackjack.rb | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/blackjack.rb b/blackjack.rb index 81ed5d6..c98f45d 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -77,11 +77,14 @@ def initialize @dealer_hand = Hand.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 + stand if @player_hand.value >= 21 + status end def stand @@ -112,6 +115,10 @@ def determine_winner(player_value, dealer_value) def inspect status end + + def finished? + @winner != nil + end end @@ -229,7 +236,7 @@ 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 @@ -238,7 +245,17 @@ def inspect while game.player_hand.value <= 21 game.hit end - game.status[:winner].should_not be_nil + 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 + end + game.finished?.should be_true end describe "#determine_winner" do From 48d77075a4c5f7a882f89980565b2d1560ce9dd9 Mon Sep 17 00:00:00 2001 From: ardder Date: Sun, 30 Jun 2013 18:23:35 +0800 Subject: [PATCH 4/4] Eagle level finished --- blackjack.rb | 85 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 70 insertions(+), 15 deletions(-) diff --git a/blackjack.rb b/blackjack.rb index c98f45d..6ce5f6c 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -48,33 +48,63 @@ 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 } + 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) } @@ -88,7 +118,7 @@ def hit end def stand - @dealer_hand.play_as_dealer(@deck) + @dealer_hand.play(@deck) @winner = determine_winner(@player_hand.value, @dealer_hand.value) end @@ -184,32 +214,57 @@ def finished? 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 + 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