From 97590d7d9a0b39c927c8acfccac495441bd07784 Mon Sep 17 00:00:00 2001 From: Meshed Date: Thu, 15 Nov 2012 00:47:01 -0500 Subject: [PATCH 1/4] Modify card to_s display format. Stand when player busts --- blackjack.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/blackjack.rb b/blackjack.rb index b6dcda9..5cbf0ca 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -14,7 +14,11 @@ def value end def to_s - "#{@value}-#{suit}" + suit = "C" if @suit == :clubs + suit = "D" if @suit == :diamonds + suit = "H" if @suit == :hearts + suit = "S" if @suit == :spaids + "#{suit}#{@value}" end end @@ -29,7 +33,7 @@ def initialize def self.build_cards cards = [] - [:clubs, :diamonds, :spades, :hearts].each do |suit| + [:clubs, :diamonds, :spaids, :hearts].each do |suit| (2..10).each do |number| cards << Card.new(suit, number) end @@ -75,6 +79,7 @@ def initialize def hit @player_hand.hit!(@deck) + stand if @player_hand.value > 21 end def stand @@ -135,7 +140,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("DA") end end @@ -218,7 +223,13 @@ def inspect game.hit game.player_hand.cards.length.should eq(3) end - + it "should stand when player busts" do + game = Game.new + until game.status[:player_value] > 21 + game.hit + end + game.status[:winner].should_not be_nil + end it "should play the dealer hand when I stand" do game = Game.new game.stand From a86279c15ccb5f18907a5097aba2037be83f6c45 Mon Sep 17 00:00:00 2001 From: Meshed Date: Thu, 15 Nov 2012 01:05:53 -0500 Subject: [PATCH 2/4] Hide first dealer card until player stands. Hide dealer score until player stands --- blackjack.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/blackjack.rb b/blackjack.rb index 5cbf0ca..a9daedc 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -88,10 +88,17 @@ def stand end def status + if @winner == nil + dealer_hand = ["XX", @dealer_hand.cards[1]] + dealer_value = "XX" + else + dealer_hand = @dealer_hand.cards + dealer_value = @dealer_hand.value + end {:player_cards=> @player_hand.cards, :player_value => @player_hand.value, - :dealer_cards => @dealer_hand.cards, - :dealer_value => @dealer_hand.value, + :dealer_cards => dealer_hand, + :dealer_value => dealer_value, :winner => @winner} end @@ -218,6 +225,12 @@ def inspect it "should have a status" do Game.new.status.should_not be_nil end + it "should not display dealers full hand until player stands" do + Game.new.status[:dealer_cards][0].should eq("XX") + end + it "should not display dealers score until player stands" do + Game.new.status[:dealer_value].should eq("XX") + end it "should hit when I tell it to" do game = Game.new game.hit From 651157979f8bc0316d6000589210992a47db78ca Mon Sep 17 00:00:00 2001 From: Meshed Date: Wed, 28 Nov 2012 22:17:33 -0500 Subject: [PATCH 3/4] Make code more readable --- blackjack.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/blackjack.rb b/blackjack.rb index a9daedc..016838b 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -14,11 +14,11 @@ def value end def to_s - suit = "C" if @suit == :clubs - suit = "D" if @suit == :diamonds - suit = "H" if @suit == :hearts - suit = "S" if @suit == :spaids - "#{suit}#{@value}" + suit-abbreviation = "C" if @suit == :clubs + suit-abbreviation = "D" if @suit == :diamonds + suit-abbreviation = "H" if @suit == :hearts + suit-abbreviation = "S" if @suit == :spaids + "#{suit-abbreviation}#{@value}" end end @@ -88,7 +88,7 @@ def stand end def status - if @winner == nil + if @winner.nil? dealer_hand = ["XX", @dealer_hand.cards[1]] dealer_value = "XX" else From 954ffe11d8ba71e6ea8df859427d757ca4344ab1 Mon Sep 17 00:00:00 2001 From: Meshed Date: Wed, 28 Nov 2012 22:36:32 -0500 Subject: [PATCH 4/4] Update suit-abbreviation to suit_abbreviation to prevent math being performed --- blackjack.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/blackjack.rb b/blackjack.rb index 016838b..6958aca 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -14,11 +14,11 @@ def value end def to_s - suit-abbreviation = "C" if @suit == :clubs - suit-abbreviation = "D" if @suit == :diamonds - suit-abbreviation = "H" if @suit == :hearts - suit-abbreviation = "S" if @suit == :spaids - "#{suit-abbreviation}#{@value}" + suit_abbreviation = "C" if @suit == :clubs + suit_abbreviation = "D" if @suit == :diamonds + suit_abbreviation = "H" if @suit == :hearts + suit_abbreviation = "S" if @suit == :spaids + "#{suit_abbreviation}#{@value}" end end