Permalink
Browse files

Merge branch 'aces_solution' into cucumber

  • Loading branch information...
2 parents 1bd5f52 + 36b54cd commit f3a1d5f3cbd8fa7e6a92760cc414a420a338d990 @patmaddox committed Aug 25, 2010
Showing with 55 additions and 0 deletions.
  1. +8 −0 .autotest
  2. +1 −0 autotest/discover.rb
  3. +41 −0 lib/hand.rb
  4. +5 −0 spec/hand_spec.rb
View
@@ -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
View
@@ -0,0 +1 @@
+Autotest.add_discovery { "rspec2" }
View
@@ -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
View
@@ -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

0 comments on commit f3a1d5f

Please sign in to comment.