diff --git a/blackjack.rb b/blackjack.rb index b6dcda9..182514c 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -14,7 +14,7 @@ def value end def to_s - "#{@value}-#{suit}" + "#{@value}#{suit}" end end @@ -29,7 +29,7 @@ def initialize def self.build_cards cards = [] - [:clubs, :diamonds, :spades, :hearts].each do |suit| + [:C, :D, :S, :H].each do |suit| (2..10).each do |number| cards << Card.new(suit, number) end @@ -69,21 +69,25 @@ 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 def hit @player_hand.hit!(@deck) + if @player_hand.value > 21 + stand + end end + def stand @dealer_hand.play_as_dealer(@deck) @winner = determine_winner(@player_hand.value, @dealer_hand.value) 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, @@ -111,31 +115,31 @@ def inspect describe Card do it "should accept suit and value when building" do - card = Card.new(:clubs, 10) - card.suit.should eq(:clubs) + card = Card.new(:C, 10) + card.suit.should eq(:C) card.value.should eq(10) end it "should have a value of 10 for facecards" do facecards = ["J", "Q", "K"] facecards.each do |facecard| - card = Card.new(:hearts, facecard) + card = Card.new(:H, facecard) card.value.should eq(10) end end it "should have a value of 4 for the 4-clubs" do - card = Card.new(:clubs, 4) + card = Card.new(:C, 4) card.value.should eq(4) end it "should return 11 for Ace" do - card = Card.new(:diamonds, "A") + card = Card.new(:D, "A") card.value.should eq(11) end it "should be formatted nicely" do - card = Card.new(:diamonds, "A") - card.to_s.should eq("A-diamonds") + card = Card.new(:D, "A") + card.to_s.should eq("AD") end end @@ -156,16 +160,16 @@ 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 = mock(:deck, :cards => [Card.new(:C, 4), Card.new(:D, 10)]) hand = Hand.new 2.times { hand.hit!(deck) } hand.value.should eq(14) end it "should take from the top of the deck" do - club4 = Card.new(:clubs, 4) - diamond7 = Card.new(:diamonds, 7) - clubK = Card.new(:clubs, "K") + club4 = Card.new(:C, 4) + diamond7 = Card.new(:D, 7) + clubK = Card.new(:C, "K") deck = mock(:deck, :cards => [club4, diamond7, clubK]) hand = Hand.new @@ -175,24 +179,24 @@ def inspect end 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)]) + it "should hit below 16" do + deck = mock(:deck, :cards => [Card.new(:C, 4), Card.new(:D, 4), Card.new(:C, 2), Card.new(:H, 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 = mock(:deck, :cards => [Card.new(:C, 8), Card.new(:D, 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), - Card.new(:clubs, "K")]) + deck = mock(:deck, :cards => [Card.new(:C, 4), + Card.new(:D, 7), + Card.new(:C, "K")]) hand = Hand.new 2.times { hand.hit!(deck) } hand.play_as_dealer(deck) @@ -225,18 +229,27 @@ def inspect 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) + Game.new.determine_winner(22, 15).should eq(:dealer) + end + it "should #stand for player when busts" do + game = Game.new + while game.status[:player_value] < 21 + game.hit + end + game.stand.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