-
Notifications
You must be signed in to change notification settings - Fork 45
BlackJack Homework #9
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
Open
jamlevi
wants to merge
4
commits into
RubyoffRails:master
Choose a base branch
from
jamlevi:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
require 'rspec' | ||
class Card | ||
|
||
# @@FORMATS = (1 => "#{@value}-#{suit}", 2 => "#{suit}#{@value}") | ||
|
||
attr_reader :suit, :value | ||
def initialize(suit, value) | ||
@suit = suit | ||
|
@@ -13,8 +15,33 @@ def value | |
return @value | ||
end | ||
|
||
def suit | ||
return_value = '' | ||
if @suit.eql?(:clubs) | ||
return_value = 'C' | ||
end | ||
if @suit.eql?(:spades) | ||
return_value = 'S' | ||
end | ||
if @suit.eql?(:hearts) | ||
return_value = 'H' | ||
end | ||
if @suit.eql?(:diamonds) | ||
return_value = 'D' | ||
end | ||
return_value | ||
end | ||
|
||
# "D5" instead of "5-diamonds" | ||
def to_s | ||
"#{@value}-#{suit}" | ||
### cant do this beacause it's a Symbol | ||
### suit_label = suit.chr.capitalize | ||
### "{suit_label}#{@value}" | ||
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. you can just call :symbol.to_s to convert a symbol to a string |
||
|
||
### orig | ||
# "#{@value}-#{suit}" | ||
|
||
"#{suit}#{@value}" # I changed the symbols, but the tests will fail now? | ||
end | ||
|
||
end | ||
|
@@ -61,6 +88,10 @@ def play_as_dealer(deck) | |
play_as_dealer(deck) | ||
end | ||
end | ||
|
||
def inspect | ||
@cards | ||
end | ||
end | ||
|
||
class Game | ||
|
@@ -75,24 +106,38 @@ def initialize | |
|
||
def hit | ||
@player_hand.hit!(@deck) | ||
if @player_hand.value > 21 | ||
puts "Player Busts! at #{@player_hand.value}" | ||
self.stand | ||
end | ||
puts "Player is Holding #{@player_hand.value}: #{@player_hand.inspect}" | ||
end | ||
|
||
def stand | ||
@dealer_hand.play_as_dealer(@deck) | ||
unless @player_hand.value > 21 | ||
@dealer_hand.play_as_dealer(@deck) | ||
end | ||
puts "Dealer is Holding #{@dealer_hand.value}: #{@dealer_hand.inspect}" | ||
@winner = determine_winner(@player_hand.value, @dealer_hand.value) | ||
puts "#{@winner} wins!" | ||
end | ||
|
||
def status | ||
{:player_cards=> @player_hand.cards, | ||
:player_value => @player_hand.value, | ||
:dealer_cards => @dealer_hand.cards, | ||
:dealer_value => @dealer_hand.value, | ||
:dealer_cards => '$$', | ||
:dealer_value => '$$', | ||
:winner => @winner} | ||
end | ||
|
||
def determine_winner(player_value, dealer_value) | ||
return :dealer if player_value > 21 | ||
return :player if dealer_value > 21 | ||
#if player_value > 21 | ||
# stand | ||
# :dealer | ||
#end | ||
#return :player if dealer_value > 21 | ||
if player_value == dealer_value | ||
:push | ||
elsif player_value > dealer_value | ||
|
@@ -107,17 +152,17 @@ def inspect | |
end | ||
end | ||
|
||
|
||
#============================================================================== | ||
describe Card do | ||
|
||
it "should accept suit and value when building" do | ||
card = Card.new(:clubs, 10) | ||
card.suit.should eq(:clubs) | ||
card.suit.should eq(:clubs.to_s.capitalize.each_char.first) | ||
card.value.should eq(10) | ||
end | ||
|
||
it "should have a value of 10 for facecards" do | ||
facecards = ["J", "Q", "K"] | ||
facecards = %w( J Q K ) | ||
facecards.each do |facecard| | ||
card = Card.new(:hearts, facecard) | ||
card.value.should eq(10) | ||
|
@@ -135,7 +180,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 eq("DA") | ||
end | ||
end | ||
|
||
|
@@ -225,6 +271,18 @@ def inspect | |
game.status[:winner].should_not be_nil | ||
end | ||
|
||
it "should stand if player busts" do | ||
game = Game.new | ||
deck = mock(:deck, :cards => [Card.new(:clubs, 8), | ||
Card.new(:diamonds, 7), | ||
Card.new(:clubs, "K")]) | ||
hand = Hand.new | ||
2.times { hand.hit!(deck) } | ||
hand.hit!(deck) | ||
|
||
|
||
end | ||
|
||
describe "#determine_winner" do | ||
it "should have dealer win when player busts" do | ||
Game.new.determine_winner(22, 15).should eq(:dealer) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I recommend not leaving commented code in your code.
Also, the use of a class level variable here doesn't make sense (@@). Just use a CONSTANT instead.