Skip to content

Commit

Permalink
Merge branch 'aces_solution' into cucumber
Browse files Browse the repository at this point in the history
  • Loading branch information
Pat Maddox committed Aug 25, 2010
2 parents 1bd5f52 + 36b54cd commit f3a1d5f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .autotest
@@ -0,0 +1,8 @@
begin
require 'autotest/growl'
rescue LoadError
$stderr.puts "***************"
$stderr.puts "Couldn't load autotest/growl. If you want nifty growl notifications, gem install autotest-growl"
$stderr.puts "***************"
$stderr.puts ""
end
1 change: 1 addition & 0 deletions autotest/discover.rb
@@ -0,0 +1 @@
Autotest.add_discovery { "rspec2" }
41 changes: 41 additions & 0 deletions lib/hand.rb
@@ -1,3 +1,44 @@
class Hand
def initialize
@cards = []
end

def deal(card)
@cards << card
end

def score
score_of_non_aces + score_of_aces
end

def score_of_non_aces
@cards.reject {|c| c == :ace }.inject(0) do |sum, c|
sum += if [:king, :queen, :jack].include?(c)
10
else
c
end
end
end

def score_of_aces
num_aces = @cards.select {|c| c == :ace }.size
if num_aces == 0
0
elsif score_of_non_aces > 10 ||
(score_of_non_aces == 10 && num_aces > 1)
num_aces
else
11 + num_aces - 1
end
end

def blackjack?
score == 21 && @cards.size == 2
end

def bonus?
score == 21 &&
(@cards.uniq.size == 1 || @cards.max - @cards.min == 2)
end
end
5 changes: 5 additions & 0 deletions spec/hand_spec.rb
Expand Up @@ -53,6 +53,11 @@
@hand.deal 9
@hand.should_not be_bonus
end

it "should not be bonus when dealt 5, 5, 5" do
3.times { @hand.deal 5 }
@hand.should_not be_bonus
end

it "should be bonus when dealt 6, 7, 8" do
@hand.deal 6
Expand Down

0 comments on commit f3a1d5f

Please sign in to comment.