Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
36 changes: 26 additions & 10 deletions blackjack.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'rspec'
class Card

class Card
attr_reader :suit, :value

def initialize(suit, value)
@suit = suit
@value = value
Expand All @@ -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'}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in Ruby, it's called a Hash (short for hash-map, I know, but people will know what you mean with hash). Map is typically used for map-reduce (as in over a collection)

Also, instead of suits_map[suit], I'd recommend

suits_map.fetch(:suit)

This way, fetch will will raise an exception if suit is somehow missing.

"#{suits_map[suit]}#{@value}"
end

end


class Deck
attr_reader :cards

Expand Down Expand Up @@ -47,6 +49,7 @@ class Hand
def initialize
@cards = []
end

def hit!(deck)
@cards << deck.cards.shift
end
Expand All @@ -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?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By naming this method "busted?" it gives a sense to the reader that it will return true/false. It might be better to have

def busted?
@player_hand.value > 21
end

That way, your command/flow can be separate from your logic

@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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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) }
Expand Down Expand Up @@ -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
Expand Down