From 58c5723172d9d805f28e72f961018e2ebd1c034e Mon Sep 17 00:00:00 2001 From: Elle Yoko Suzuki Date: Fri, 23 Nov 2012 23:51:30 -0800 Subject: [PATCH 1/2] fixed tyops + tiger level completed --- README.md | 6 +++--- blackjack.rb | 36 ++++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 5aca256..3077479 100644 --- a/README.md +++ b/README.md @@ -16,19 +16,19 @@ game.hit game.stand ``` -2. Change the Card's to_s to show "Q5" instead of 5-queen" +2. Change the Card's to_s to show "H5" instead of 5-hearts" Tiger Level ----------- 1. Complete the Panda assignment -2. If a player busts (goes over 21), the game should #standfor the player +2. If a player busts (goes over 21), the game should #stand for the player Eagle Level ------------ -1. The dealer hand should not not show both cards until the player has stood (It should be like "XX", "Q5") +1. The dealer hand should not show both cards until the player has stood (It should be like "XX", "Q5") Copyright: Jesse Wolgamott, MIT License (See LICENSE) diff --git a/blackjack.rb b/blackjack.rb index b6dcda9..ae420ca 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -1,7 +1,8 @@ require 'rspec' -class Card +class Card attr_reader :suit, :value + def initialize(suit, value) @suit = suit @value = value @@ -14,12 +15,13 @@ def value end def to_s - "#{@value}-#{suit}" + #"#{@value}-#{suit}" # 5-hearts + # want form 'H5' + suits_map = {:clubs => 'C', :diamonds => 'D', :spades => 'S', :hearts => 'H'} + "#{suits_map[suit]}#{@value}" end - end - class Deck attr_reader :cards @@ -47,6 +49,7 @@ class Hand def initialize @cards = [] end + def hit!(deck) @cards << deck.cards.shift end @@ -65,25 +68,32 @@ def play_as_dealer(deck) class Game attr_reader :player_hand, :dealer_hand + def initialize @deck = Deck.new @player_hand = Hand.new @dealer_hand = Hand.new 2.times { @player_hand.hit!(@deck) } 2.times { @dealer_hand.hit!(@deck) } + busted? # might get 2 Aces = 22 by def end def hit - @player_hand.hit!(@deck) + @player_hand.hit!(@deck) + busted? end + def busted? + @player_hand.value > 21 ? stand : @player_hand.cards + 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, @@ -135,7 +145,8 @@ 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("A-diamonds") + card.to_s.should == 'DA' end end @@ -171,11 +182,10 @@ def inspect hand = Hand.new 2.times { hand.hit!(deck) } hand.cards.should eq([club4, diamond7]) - 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 2.times { hand.hit!(deck) } @@ -225,11 +235,17 @@ def inspect game.status[:winner].should_not be_nil end + it 'should stand when I bust' do + game = Game.new + 5.times {game.hit} + game.status[:winner].should eq(:dealer) + end + describe "#determine_winner" do it "should have dealer win when player busts" do Game.new.determine_winner(22, 15).should eq(:dealer) end - it "should player win if dealer busts" do + it "should have player win if dealer busts" do Game.new.determine_winner(18, 22).should eq(:player) end it "should have player win if player > dealer" do From e7f8504c38d646e078c5b46fca7040783d31562d Mon Sep 17 00:00:00 2001 From: Elle Yoko Suzuki Date: Fri, 23 Nov 2012 23:51:30 -0800 Subject: [PATCH 2/2] fixed typos + tiger level completed --- README.md | 6 +++--- blackjack.rb | 36 ++++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 5aca256..3077479 100644 --- a/README.md +++ b/README.md @@ -16,19 +16,19 @@ game.hit game.stand ``` -2. Change the Card's to_s to show "Q5" instead of 5-queen" +2. Change the Card's to_s to show "H5" instead of 5-hearts" Tiger Level ----------- 1. Complete the Panda assignment -2. If a player busts (goes over 21), the game should #standfor the player +2. If a player busts (goes over 21), the game should #stand for the player Eagle Level ------------ -1. The dealer hand should not not show both cards until the player has stood (It should be like "XX", "Q5") +1. The dealer hand should not show both cards until the player has stood (It should be like "XX", "Q5") Copyright: Jesse Wolgamott, MIT License (See LICENSE) diff --git a/blackjack.rb b/blackjack.rb index b6dcda9..ae420ca 100644 --- a/blackjack.rb +++ b/blackjack.rb @@ -1,7 +1,8 @@ require 'rspec' -class Card +class Card attr_reader :suit, :value + def initialize(suit, value) @suit = suit @value = value @@ -14,12 +15,13 @@ def value end def to_s - "#{@value}-#{suit}" + #"#{@value}-#{suit}" # 5-hearts + # want form 'H5' + suits_map = {:clubs => 'C', :diamonds => 'D', :spades => 'S', :hearts => 'H'} + "#{suits_map[suit]}#{@value}" end - end - class Deck attr_reader :cards @@ -47,6 +49,7 @@ class Hand def initialize @cards = [] end + def hit!(deck) @cards << deck.cards.shift end @@ -65,25 +68,32 @@ def play_as_dealer(deck) class Game attr_reader :player_hand, :dealer_hand + def initialize @deck = Deck.new @player_hand = Hand.new @dealer_hand = Hand.new 2.times { @player_hand.hit!(@deck) } 2.times { @dealer_hand.hit!(@deck) } + busted? # might get 2 Aces = 22 by def end def hit - @player_hand.hit!(@deck) + @player_hand.hit!(@deck) + busted? end + def busted? + @player_hand.value > 21 ? stand : @player_hand.cards + 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, @@ -135,7 +145,8 @@ 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("A-diamonds") + card.to_s.should == 'DA' end end @@ -171,11 +182,10 @@ def inspect hand = Hand.new 2.times { hand.hit!(deck) } hand.cards.should eq([club4, diamond7]) - 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 2.times { hand.hit!(deck) } @@ -225,11 +235,17 @@ def inspect game.status[:winner].should_not be_nil end + it 'should stand when I bust' do + game = Game.new + 5.times {game.hit} + game.status[:winner].should eq(:dealer) + end + describe "#determine_winner" do it "should have dealer win when player busts" do Game.new.determine_winner(22, 15).should eq(:dealer) end - it "should player win if dealer busts" do + it "should have player win if dealer busts" do Game.new.determine_winner(18, 22).should eq(:player) end it "should have player win if player > dealer" do