Begin forwarded message:

> From: Nathan Morse <nathan / nathanmorse.com>
> Date: November 20, 2005 5:03:27 PM CST
> To: submission / rubyquiz.com
> Subject: Please Forward: Ruby Quiz Submission
>
> Ruby Quiz: Euchre Hands (#55) [SOLUTION]
>
> Hi James,
>
> This is my first shot at solving the Ruby Quiz and I found it quite  
> entertaining.  Please forward my solution to the Ruby Talk mailing  
> list as I am having trouble subscribing myself.
>
> Thanks!
>
> -Nathan
>
> ---------------------------------------------------------------------- 
> ---------
> #!/usr/local/bin/ruby
>
> class EuchreHand
>    attr_reader :trump, :cards
>    def initialize(trump, *cards)
>        @trump = trump
>        @cards = cards.inject([]) do |result, card|
>            card = Card.new(trump, card)
>            result.push(card)
>        end
>    end
>
> end
>
> class Card
>    include Comparable
>    attr_reader :trump, :card
>     @@sort_order_lookup = nil
>     def initialize(trump, card)
>        initialize_sort_order_lookup
>        @trump, @card = trump, card
>    end
>     def initialize_sort_order_lookup
>        if (@@sort_order_lookup.nil?)
>            @@sort_order_lookup = {}
>            suits = %w[h c d s]
>                     # build the lookup hash that will store the  
> sort order for all possible trumps
>            %w[Hearts Clubs Diamonds Spades].each do |suit_name|
>
>                #counter maintains the sort order of each card for  
> the current trump
>                counter = 0
>                @@sort_order_lookup[suit_name] = {}
>                faces = %w[9 T J Q K A]
>                             # index each 'regular' card
>                3.downto(0) do |suit_index|
>                    faces.each do |face|
>                        @@sort_order_lookup[suit_name]["#{face}# 
> {suits[suit_index]}"] = counter
>                        counter += 1
>                    end
>                end
>                             # reindex the left bower
>                @@sort_order_lookup[suit_name]["J#{suits[2]}"] =  
> counter
>                counter += 1
>                             # reindex the right bower
>                @@sort_order_lookup[suit_name]["J#{suits[0]}"] =  
> counter
>                             #shift the array of suits by 1
>                suits = suits[1..3].push(suits[0])
>                             end
>        end
>    end
>
>    def <=>(other)
>        @@sort_order_lookup[self.trump][self.card] <=>
>        @@sort_order_lookup[other.trump][other.card]
>    end
>
>    def to_s
>        @card
>    end
>
> end
>
> # parse the input
> $input = (1..6).inject([]) { |result, line| result.push 
> (STDIN.readline.chomp) }
> $euchre_hand = EuchreHand.new(*$input)
>
> # display our result
> puts $euchre_hand.trump
> $euchre_hand.cards.sort.reverse.each { |card| puts card }
>