-
Notifications
You must be signed in to change notification settings - Fork 44
Modified #to_s to format 'Q Hearts' to 'QH' #36
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 2 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,12 @@ def value | |
| end | ||
|
|
||
| def to_s | ||
| "#{@value}-#{suit}" | ||
| suit = "H" if @suit == :hearts | ||
| suit = "C" if @suit == :clubs | ||
| suit = "S" if @suit == :spades | ||
| suit = "D" if @suit == :diamonds | ||
|
|
||
| "#{@value}#{suit}" | ||
| end | ||
|
|
||
| end | ||
|
|
@@ -55,6 +60,10 @@ def value | |
| cards.inject(0) {|sum, card| sum += card.value } | ||
| end | ||
|
|
||
| def bust? | ||
| value > 21 | ||
| end | ||
|
|
||
| def play_as_dealer(deck) | ||
| if value < 16 | ||
| hit!(deck) | ||
|
|
@@ -75,6 +84,11 @@ def initialize | |
|
|
||
| def hit | ||
| @player_hand.hit!(@deck) | ||
| if @player_hand.value > 21 | ||
|
Member
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. did you mean to use the "bust" method here?
Author
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. Indeed I did, but I definitely forgot that I defined it. That what I get for stepping away from the assignment for a few hours. |
||
| stand | ||
| else | ||
| status | ||
| end | ||
| end | ||
|
|
||
| def stand | ||
|
|
@@ -134,8 +148,8 @@ def inspect | |
| end | ||
|
|
||
| it "should be formatted nicely" do | ||
| card = Card.new(:diamonds, "A") | ||
| card.to_s.should eq("A-diamonds") | ||
| card = Card.new(:hearts, "Q") | ||
| card.to_s.should eq("QH") | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -174,6 +188,17 @@ def inspect | |
|
|
||
| end | ||
|
|
||
| it "should bust when a player hand value exceeds 21" do | ||
| deck = mock(:deck, :cards => [Card.new(:clubs, 10), | ||
| Card.new(:diamonds, 10), | ||
| Card.new(:spades, 10)]) | ||
|
|
||
| hand = Hand.new | ||
| 3.times { hand.hit!(deck) } | ||
| hand.bust?.should eq(true) | ||
|
|
||
| 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)]) | ||
|
|
||
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.
The way this is specified is very close to a case statement. In ruby, that looks like:
However, there's a slight problem here, that if the suit is something other than a
:hearts, :clubs, :spades:, :diamonds, it will be nil and cause problems later down the line. So you might: