-
Notifications
You must be signed in to change notification settings - Fork 45
I've finished panda level! #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,13 @@ def value | |
end | ||
|
||
def to_s | ||
"#{@value}-#{suit}" | ||
abbrev = { | ||
clubs: "C", | ||
diamonds: "D", | ||
spades: "S", | ||
hearts: "H" | ||
} | ||
"#{@value}#{abbrev[@suit]}" | ||
end | ||
|
||
end | ||
|
@@ -42,43 +48,77 @@ def self.build_cards | |
end | ||
|
||
class Hand | ||
attr_reader :cards | ||
|
||
def initialize | ||
@cards = [] | ||
end | ||
|
||
def hit!(deck) | ||
@cards << deck.cards.shift | ||
end | ||
|
||
def cards | ||
@cards.map {|card| card.to_s } | ||
end | ||
|
||
def value | ||
@cards.inject(0) {|sum, card| sum += card.value } | ||
end | ||
end | ||
|
||
class DealerHand < Hand | ||
def initialize | ||
super | ||
@played = false | ||
end | ||
|
||
def cards | ||
if @played | ||
super | ||
else | ||
#hide first card | ||
["XX"] + @cards[1..-1].map {|card| card.to_s} | ||
end | ||
end | ||
|
||
def value | ||
cards.inject(0) {|sum, card| sum += card.value } | ||
if @played | ||
super | ||
else | ||
nil | ||
end | ||
end | ||
|
||
def play_as_dealer(deck) | ||
def play(deck) | ||
@played = true | ||
if value < 16 | ||
hit!(deck) | ||
play_as_dealer(deck) | ||
play(deck) | ||
end | ||
end | ||
end | ||
|
||
|
||
|
||
class Game | ||
attr_reader :player_hand, :dealer_hand | ||
def initialize | ||
@deck = Deck.new | ||
@player_hand = Hand.new | ||
@dealer_hand = Hand.new | ||
@dealer_hand = DealerHand.new | ||
2.times { @player_hand.hit!(@deck) } | ||
2.times { @dealer_hand.hit!(@deck) } | ||
|
||
stand if @player_hand.value == 21 | ||
end | ||
|
||
def hit | ||
@player_hand.hit!(@deck) | ||
stand if @player_hand.value >= 21 | ||
status | ||
end | ||
|
||
def stand | ||
@dealer_hand.play_as_dealer(@deck) | ||
@dealer_hand.play(@deck) | ||
@winner = determine_winner(@player_hand.value, @dealer_hand.value) | ||
end | ||
|
||
|
@@ -105,6 +145,10 @@ def determine_winner(player_value, dealer_value) | |
def inspect | ||
status | ||
end | ||
|
||
def finished? | ||
@winner != nil | ||
end | ||
end | ||
|
||
|
||
|
@@ -133,9 +177,9 @@ def inspect | |
card.value.should eq(11) | ||
end | ||
|
||
it "should be formatted nicely" do | ||
it "should be formatted with abbreviation" do | ||
card = Card.new(:diamonds, "A") | ||
card.to_s.should eq("A-diamonds") | ||
card.to_s.should eq("AD") | ||
end | ||
end | ||
|
||
|
@@ -170,32 +214,57 @@ def inspect | |
deck = mock(:deck, :cards => [club4, diamond7, clubK]) | ||
hand = Hand.new | ||
2.times { hand.hit!(deck) } | ||
hand.cards.should eq([club4, diamond7]) | ||
hand.cards.should eq([club4.to_s, diamond7.to_s]) | ||
|
||
end | ||
end | ||
|
||
describe DealerHand do | ||
|
||
it "should hide the dealer's first card and value until dealer has played" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, sticking to a 1 assertion per specifications (1 should per 'it') is a good practice. In a workflow like this, I'm cool with what you did, although I'd want to move the setup of the hand to a before block. |
||
club4 = Card.new(:clubs, 4) | ||
diamond7 = Card.new(:diamonds, 7) | ||
clubK = Card.new(:clubs, "K") | ||
|
||
deck = mock(:deck, :cards => [club4, diamond7, clubK]) | ||
dealer_hand = DealerHand.new | ||
|
||
2.times { dealer_hand.hit!(deck) } | ||
|
||
dealer_hand.cards[0].should eq("XX") | ||
dealer_hand.value.should be_nil | ||
|
||
dealer_hand.play(deck) | ||
|
||
dealer_hand.cards[0].should eq(club4.to_s) | ||
dealer_hand.value.should eq(21) | ||
end | ||
|
||
describe "#play" do | ||
|
||
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)]) | ||
hand = Hand.new | ||
hand = DealerHand.new | ||
2.times { hand.hit!(deck) } | ||
hand.play_as_dealer(deck) | ||
hand.play(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)]) | ||
hand = Hand.new | ||
hand = DealerHand.new | ||
2.times { hand.hit!(deck) } | ||
hand.play_as_dealer(deck) | ||
hand.play(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")]) | ||
hand = Hand.new | ||
hand = DealerHand.new | ||
2.times { hand.hit!(deck) } | ||
hand.play_as_dealer(deck) | ||
hand.play(deck) | ||
hand.value.should eq(21) | ||
end | ||
end | ||
|
@@ -222,7 +291,26 @@ def inspect | |
it "should play the dealer hand when I stand" do | ||
game = Game.new | ||
game.stand | ||
game.status[:winner].should_not be_nil | ||
game.finished?.should be_true | ||
end | ||
|
||
|
||
it "should stand for the player if a player busts" do | ||
game = Game.new | ||
while game.player_hand.value <= 21 | ||
game.hit | ||
end | ||
game.finished?.should be_true | ||
end | ||
|
||
it "should stand for the player if a player gets 21 exactly" do | ||
game = Game.new | ||
while game.player_hand.value < 21 | ||
game.hit | ||
#recreate a game if the player busts | ||
game = Game.new if game.player_hand.value > 21 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice touch! |
||
end | ||
game.finished?.should be_true | ||
end | ||
|
||
describe "#determine_winner" do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job with inject here.