From 5d0f448688fe8aed2edc260c917443519e6584b3 Mon Sep 17 00:00:00 2001 From: remi Date: Tue, 19 Jan 2010 22:08:36 -0700 Subject: [PATCH] added @card.color --- lib/playing-cards.rb | 11 +++++++++++ spec/card_spec.rb | 7 +++++++ spec/solitaire_spec.rb | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/lib/playing-cards.rb b/lib/playing-cards.rb index fecd2e2..ba48992 100644 --- a/lib/playing-cards.rb +++ b/lib/playing-cards.rb @@ -4,6 +4,8 @@ class Card + COLORS_FOR_SUITES = { 'club' => :black, 'spade' => :black, 'heart' => :red, 'diamond' => :red } + STANDARD_PLAYING_CARD_NAMES = %w( 2 3 4 5 6 7 8 9 10 Jack Queen King Ace ) STANDARD_PLAYING_CARD_SUITES = %w( Spades Hearts Clubs Diamonds ) @@ -20,6 +22,10 @@ def initialize name, suite = nil end end + def color + Card.color_for(suite) + end + def name= value @name = value.to_s.strip.sub(/^The\s*/i, '') end @@ -36,6 +42,10 @@ def short_name "#{ name[0..0].upcase }#{ suite[0..0].upcase }" end + def self.color_for suite + COLORS_FOR_SUITES[suite] + end + def self.[] name, suite = nil Card.new name, suite end @@ -66,6 +76,7 @@ def self.suite_from_appreviation abbreviated_suite def parse name_to_parse parts = name_to_parse.split('of') + parts = name_to_parse.split('Of') if parts.length == 1 # "Jack of Spades" or "The Jack of Spades" if parts.length == 2 diff --git a/spec/card_spec.rb b/spec/card_spec.rb index 0c4bd28..bf58f7f 100644 --- a/spec/card_spec.rb +++ b/spec/card_spec.rb @@ -2,6 +2,13 @@ describe Card do + it 'has a color (based on suite)' do + TheJackOfSpades.color.should == :black + TheJackOfClubs.color.should == :black + TheJackOfHearts.color.should == :red + TheJackOfDiamonds.color.should == :red + end + it 'has a name, suite, and full_name (eg. "4 of Clubs")' do card = Card.new 4, :club card.name.should == '4' diff --git a/spec/solitaire_spec.rb b/spec/solitaire_spec.rb index 1d894a1..f57f2fe 100644 --- a/spec/solitaire_spec.rb +++ b/spec/solitaire_spec.rb @@ -54,4 +54,8 @@ game.waste.length.should == 3 end + it 'should be able to move the top waste card to a pile (should yell if not allowed)' do + pending 'cards need colors' + end + end