From f96fe1fc5935a98e7bace538c4d3c964230fea64 Mon Sep 17 00:00:00 2001 From: Loc Pru Date: Tue, 16 Jul 2013 20:20:25 -0500 Subject: [PATCH 1/4] Panda requirement --- blackjack.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/blackjack.rb b/blackjack.rb index b6dcda9..668c48d 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -14,7 +14,7 @@ def value end def to_s - "#{@value}-#{suit}" + "#{@value}#{suit[0].upcase}" end end @@ -133,9 +133,14 @@ def inspect card.value.should eq(11) end - it "should be formatted nicely" do - card = Card.new(:diamonds, "A") - card.to_s.should eq("A-diamonds") + it "should be formatted number first and suit later: 5H" do + card = Card.new(:hearts, 5) + card.to_s.should eq("5H") + end + + it "should be formatted facecard first and suit later QH" do + card = Card.new(:hearts, 'Q') + card.to_s.should eq("QH") end end From eefcdabe86bc73b0eaab415aedfc73ef36f6618f Mon Sep 17 00:00:00 2001 From: Loc Pru Date: Wed, 17 Jul 2013 14:33:10 -0500 Subject: [PATCH 2/4] Tiger level --- blackjack.rb | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/blackjack.rb b/blackjack.rb index 668c48d..a3c2f07 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -80,6 +80,29 @@ def hit def stand @dealer_hand.play_as_dealer(@deck) @winner = determine_winner(@player_hand.value, @dealer_hand.value) + puts "The winner is #{@winner}" if @winner == :player or @winner == :dealer + puts "It's a push." if @winner == :push + continue? ? new_game : stop + end + + def continue? + return true if @player_hand.value <= 21 + return false if @player_hand.value >= 22 + end + + def new_game + puts "Beginning new game" + @player_hand.cards.clear + @dealer_hand.cards.clear + @deck = Deck.new + 2.times { @player_hand.hit!(@deck) } + 2.times { @dealer_hand.hit!(@deck) } + status + end + + def stop + puts "You went over 21, you lost" + exit end def status @@ -244,4 +267,16 @@ def inspect Game.new.determine_winner(16, 16).should eq(:push) end end -end + + it "should continue playing if player gets 21 or less" do + game = Game.new + game.player_hand.stub(:value).and_return(14) + game.continue?.should eq(true) + end + + it "should stop playing if player goes over 21" do + game = Game.new + game.player_hand.stub(:value).and_return(22) + game.continue?.should eq(false) + end +end \ No newline at end of file From 0bd1f3e2f9b6fcf493b2ac020084bfde723cde9b Mon Sep 17 00:00:00 2001 From: Loc Pru Date: Thu, 18 Jul 2013 12:28:34 -0500 Subject: [PATCH 3/4] Tiger level again --- blackjack.rb | 59 ++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/blackjack.rb b/blackjack.rb index a3c2f07..8203db5 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -74,35 +74,28 @@ def initialize end def hit - @player_hand.hit!(@deck) + puts "Have other card" + hit = @player_hand.hit!(@deck) + return next_move(@player_hand.value) if @player_hand.value >= 21 + return hit end - def stand - @dealer_hand.play_as_dealer(@deck) - @winner = determine_winner(@player_hand.value, @dealer_hand.value) - puts "The winner is #{@winner}" if @winner == :player or @winner == :dealer - puts "It's a push." if @winner == :push - continue? ? new_game : stop - end - - def continue? - return true if @player_hand.value <= 21 - return false if @player_hand.value >= 22 - end - - def new_game - puts "Beginning new game" - @player_hand.cards.clear - @dealer_hand.cards.clear - @deck = Deck.new - 2.times { @player_hand.hit!(@deck) } - 2.times { @dealer_hand.hit!(@deck) } - status + def next_move( value ) + case value + when (21) then stand + when (22..52) then stop + end end def stop puts "You went over 21, you lost" - exit + end + + def stand + @dealer_hand.play_as_dealer(@deck) + @winner = determine_winner(@player_hand.value, @dealer_hand.value) + puts "The winner is #{@winner}" + @winner end def status @@ -268,15 +261,23 @@ def inspect end end - it "should continue playing if player gets 21 or less" do + it "should stop if the player goes over 21" do + game = Game.new + game.player_hand.stub(:value).and_return(22) + game.hit.should be_nil + end + + it "should hit if the player have less than 21" do game = Game.new - game.player_hand.stub(:value).and_return(14) - game.continue?.should eq(true) + game.player_hand.stub(:value).and_return(15) + game.hit.length.should eq(3) end - it "should stop playing if player goes over 21" do + it "should stand if the player or the dealer have 21" do game = Game.new - game.player_hand.stub(:value).and_return(22) - game.continue?.should eq(false) + game.player_hand.stub(:value).and_return(21) + [:player, :dealer, :push].each do | winner | + game.hit.should eq(winner) if game.hit == winner + end end end \ No newline at end of file From 811bc130def081a734902d5ba6ab590b6ba61641 Mon Sep 17 00:00:00 2001 From: Loc Pru Date: Thu, 18 Jul 2013 14:28:53 -0500 Subject: [PATCH 4/4] Eagle Level --- blackjack.rb | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/blackjack.rb b/blackjack.rb index 8203db5..041f510 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -16,7 +16,6 @@ def value def to_s "#{@value}#{suit[0].upcase}" end - end @@ -75,11 +74,25 @@ def initialize def hit puts "Have other card" + @dealer_hand.hit!(@deck) hit = @player_hand.hit!(@deck) return next_move(@player_hand.value) if @player_hand.value >= 21 return hit end + def format_cards( cards ) + return hide_cards( cards ) if @winner.nil? + return cards + end + + def hide_cards( cards ) + hidden_cards = [] + cards.each do | card | + hidden_cards << "XX" + end + hidden_cards + end + def next_move( value ) case value when (21) then stand @@ -89,6 +102,7 @@ def next_move( value ) def stop puts "You went over 21, you lost" + stand end def stand @@ -99,9 +113,9 @@ 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_cards => format_cards( @dealer_hand.cards ), :dealer_value => @dealer_hand.value, :winner => @winner} end @@ -264,7 +278,8 @@ def inspect it "should stop if the player goes over 21" do game = Game.new game.player_hand.stub(:value).and_return(22) - game.hit.should be_nil + game.hit + game.stand.should eq(:dealer) end it "should hit if the player have less than 21" do @@ -280,4 +295,20 @@ def inspect game.hit.should eq(winner) if game.hit == winner end end + + it "should hide the dealer cards" do + game = Game.new + game.hide_cards( game.dealer_hand.cards ) + game.status[:dealer_cards].each do | card | + card.should eq("XX") + end + end + + it "should hide the dealer cards until the player has stood" do + game = Game.new + game.stand + game.status[:dealer_cards].each do | card | + card.should_not eq("XX") + end + end end \ No newline at end of file