From 80e6a6afaead006a8b8935e3f8aeddc55185e85f Mon Sep 17 00:00:00 2001 From: ThePomodoro Date: Sun, 9 Feb 2014 18:25:04 -0800 Subject: [PATCH 1/2] Feature: Completed all examples --- .idea/Episode1-Summer2012.iml | 40 +++++++++++++++++++++++++++++++++ .idea/encodings.xml | 5 +++++ .idea/misc.xml | 5 +++++ .idea/modules.xml | 9 ++++++++ .idea/scopes/scope_settings.xml | 5 +++++ .idea/vcs.xml | 7 ++++++ blackjack.rb | 35 ++++++++++++++++++++++------- 7 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 .idea/Episode1-Summer2012.iml create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/scopes/scope_settings.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/Episode1-Summer2012.iml b/.idea/Episode1-Summer2012.iml new file mode 100644 index 0000000..4402369 --- /dev/null +++ b/.idea/Episode1-Summer2012.iml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..e206d70 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..78d2e82 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e4f87f9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..c80f219 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/blackjack.rb b/blackjack.rb index b6dcda9..f06e280 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -14,7 +14,7 @@ def value end def to_s - "#{@value}-#{suit}" + "#{@value}#{@suit[0,1].capitalize}" end end @@ -75,17 +75,29 @@ def initialize def hit @player_hand.hit!(@deck) + if @player_hand.value > 21 + stand + else + status(:unstood) + end + end def stand @dealer_hand.play_as_dealer(@deck) @winner = determine_winner(@player_hand.value, @dealer_hand.value) + status(:stood) end - def status + def status (stood) + if stood == :stood + dlr_cards_to_show = @dealer_hand.cards + else + dlr_cards_to_show = ['X','X'] + end {:player_cards=> @player_hand.cards, :player_value => @player_hand.value, - :dealer_cards => @dealer_hand.cards, + :dealer_cards => dlr_cards_to_show, :dealer_value => @dealer_hand.value, :winner => @winner} end @@ -103,7 +115,7 @@ def determine_winner(player_value, dealer_value) end def inspect - status + status (:unstood) end end @@ -134,8 +146,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 @@ -211,7 +223,7 @@ def inspect Game.new.dealer_hand.cards.length.should eq(2) end it "should have a status" do - Game.new.status.should_not be_nil + Game.new.status(:unstood).should_not be_nil end it "should hit when I tell it to" do game = Game.new @@ -222,7 +234,14 @@ 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.status(:stood)[:winner].should_not be_nil + end + + it "should stand for the player when player hand is 21 or more" do + game = Game.new + let(game.player_hand.value) {21} + game.hit + game.should_receive(stand) end describe "#determine_winner" do From 0f181b694061a0f1d459a2ad53172b9c131c8a07 Mon Sep 17 00:00:00 2001 From: ThePomodoro Date: Wed, 19 Feb 2014 13:01:53 -0800 Subject: [PATCH 2/2] Please check my rpspec test - player should bust after 21 - why is it failing --- blackjack.rb | 85 ++++++++++++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 39 deletions(-) diff --git a/blackjack.rb b/blackjack.rb index f06e280..de9736f 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -47,6 +47,7 @@ class Hand def initialize @cards = [] end + def hit!(deck) @cards << deck.cards.shift end @@ -55,12 +56,6 @@ 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 @@ -68,7 +63,7 @@ class Game 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 @@ -76,30 +71,17 @@ def initialize def hit @player_hand.hit!(@deck) if @player_hand.value > 21 - stand - else - status(:unstood) + bust end + end + def bust + @winner = determine_winner(@player_hand.value, @dealer_hand.value) end def stand @dealer_hand.play_as_dealer(@deck) @winner = determine_winner(@player_hand.value, @dealer_hand.value) - status(:stood) - end - - def status (stood) - if stood == :stood - dlr_cards_to_show = @dealer_hand.cards - else - dlr_cards_to_show = ['X','X'] - end - {:player_cards=> @player_hand.cards, - :player_value => @player_hand.value, - :dealer_cards => dlr_cards_to_show, - :dealer_value => @dealer_hand.value, - :winner => @winner} end def determine_winner(player_value, dealer_value) @@ -114,11 +96,32 @@ def determine_winner(player_value, dealer_value) end end - def inspect - status (:unstood) + def status + if @dealer_hand.played + @thing = @dealer_hand.cards + 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 @@ -167,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) + end + it "should calculate the value correctly" do deck = mock(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 10)]) hand = Hand.new @@ -187,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) @@ -205,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) @@ -223,7 +237,7 @@ def inspect Game.new.dealer_hand.cards.length.should eq(2) end it "should have a status" do - Game.new.status(:unstood).should_not be_nil + Game.new.status.should_not be_nil end it "should hit when I tell it to" do game = Game.new @@ -234,14 +248,7 @@ def inspect it "should play the dealer hand when I stand" do game = Game.new game.stand - game.status(:stood)[:winner].should_not be_nil - end - - it "should stand for the player when player hand is 21 or more" do - game = Game.new - let(game.player_hand.value) {21} - game.hit - game.should_receive(stand) + game.status[:winner].should_not be_nil end describe "#determine_winner" do