Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

fixed typos + tiger level completed #17

Open
wants to merge 3 commits into from

2 participants

@ellesuzuki

No description provided.

@jwo jwo commented on the diff
blackjack.rb
@@ -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'}
@jwo Owner
jwo added a note

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jwo jwo commented on the diff
blackjack.rb
((18 lines not shown))
end
+ def busted?
@jwo Owner
jwo added a note

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jwo
Owner

Looks good! One more suggestion: write a test for the busted? method --- that's a part of the game's workflow and would be best served under test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 29 additions and 13 deletions.
  1. +3 −3 README.md
  2. +26 −10 blackjack.rb
View
6 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)
View
36 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'}
@jwo Owner
jwo added a note

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ "#{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?
@jwo Owner
jwo added a note

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ @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
Something went wrong with that request. Please try again.