"Hal E. Fulton" <hal9000 / hypermetrics.com> writes:

> I've implemented this for now as a class method taking a parameter
> which is an array of four cards. Each card is an object with
> accessors 'suit' and 'rank'.

Hal:

I've played with the idea of recording the winner in a Round
object. It seems fairly natural:

     class Card
       attr_reader :suit, :rank

       def initialize(suit, rank)
         @suit = suit
         @rank = rank
       end

       # return the better of this card and another
       def best_of(other)
         if @suit == other.suit
           @rank > other.rank ? self : other
         elsif other.suit == :spade
           other
         else
           self
         end
       end
     end

     # Record the best of a series of cards

     class Round
       attr_reader :winner
       def initialize
         @winner = nil
       end

       def add(card)
         if @winner
           @winner = @winner.best_of(card)
         else
           @winner = card
         end
       end
     end


Having said that, I'm not familiar with the game, so here are some
testcases which capture my assumptions of what I thought you meant :)


     require "test/unit"

     class TestRound < Test::Unit::TestCase

       # Generate cards C2..C14, D2..D14, etc
       for suit in [:club, :diamond, :heart, :spade]
         for rank in 2..14
           name = "#{suit.to_s[0,1].upcase}#{rank}"
           const_set(name, Card.new(suit, rank))
         end
       end

       def testSimple
         r = Round.new
         r.add(H2)
         assert_equal(H2, r.winner)
         r.add(H8)
         assert_equal(H8, r.winner)
         r.add(H4)
         assert_equal(H8, r.winner)
       end

       def testMixed
         r = Round.new
         r.add(H2)
         assert_equal(H2, r.winner)
         r.add(C8)
         assert_equal(H2, r.winner)
         r.add(D4)
         assert_equal(H2, r.winner)
         r.add(H3)
         assert_equal(H3, r.winner)
       end

       def testSpade
         r = Round.new
         r.add(H8)
         assert_equal(H8, r.winner)
         r.add(C8)
         assert_equal(H8, r.winner)
         r.add(S4)
         assert_equal(S4, r.winner)
         r.add(H10)
         assert_equal(S4, r.winner)
         r.add(S5)
         assert_equal(S5, r.winner)
       end

       def testInitialSpade
         r = Round.new
         r.add(S2)
         assert_equal(S2, r.winner)
         r.add(C8)
         assert_equal(S2, r.winner)
         r.add(S4)
         assert_equal(S4, r.winner)
       end
     end