From 089d731a1c0ed5540e14f72f29201de11feb2c49 Mon Sep 17 00:00:00 2001 From: Pat Maddox Date: Fri, 14 Nov 2008 12:46:38 -0800 Subject: [PATCH 1/4] pat's hand.rb --- lib/hand.rb | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/lib/hand.rb b/lib/hand.rb index 9e22b20..5c641bb 100644 --- a/lib/hand.rb +++ b/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 From 17412496d0cc4fcf4ad1f8d400ea2a9be98bc1f4 Mon Sep 17 00:00:00 2001 From: Pat Maddox Date: Tue, 24 Aug 2010 18:43:02 -0700 Subject: [PATCH 2/4] work for rspec 2 --- spec/spec_helper.rb | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e4ec7d0..0a8e71e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,28 +1,26 @@ require 'rubygems' -require 'spec' +require 'rspec' Dir[File.dirname(__FILE__) + '/../lib/**/*.rb'].each do |ruby_file| require ruby_file end -class OneAtATimeReporter < Spec::Runner::Reporter +class OneAtATimeReporter < RSpec::Core::Reporter def example_started(example) super unless @has_failed end - def example_failed(example, error) + def example_failed(example) unless @has_failed @has_failed = true super end end +end - def example_finished(example, error=nil) - super unless @has_failed +class RSpec::Core::Configuration + def reporter + @reporter ||= OneAtATimeReporter.new(formatter) end end -Spec::Runner.options.reporter = OneAtATimeReporter.new(Spec::Runner.options) - - - From d3dbabbc2680271f72187beda155c40f4650fa62 Mon Sep 17 00:00:00 2001 From: Pat Maddox Date: Wed, 25 Aug 2010 14:53:05 -0700 Subject: [PATCH 3/4] beef up specs --- spec/hand_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/hand_spec.rb b/spec/hand_spec.rb index 107d0bc..22a836f 100644 --- a/spec/hand_spec.rb +++ b/spec/hand_spec.rb @@ -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 From 36b54cda4a4aae3461875f764e00f6d2b35157bd Mon Sep 17 00:00:00 2001 From: Pat Maddox Date: Wed, 25 Aug 2010 14:53:17 -0700 Subject: [PATCH 4/4] autotest stuff --- .autotest | 8 ++++++++ autotest/discover.rb | 1 + 2 files changed, 9 insertions(+) create mode 100644 .autotest create mode 100644 autotest/discover.rb diff --git a/.autotest b/.autotest new file mode 100644 index 0000000..d6058e0 --- /dev/null +++ b/.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 diff --git a/autotest/discover.rb b/autotest/discover.rb new file mode 100644 index 0000000..cd6892c --- /dev/null +++ b/autotest/discover.rb @@ -0,0 +1 @@ +Autotest.add_discovery { "rspec2" }