Forum: Ruby Euchre Hands (#55)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1c1e3bdfe006a22214102fcd6434a012?d=identicon&s=25 daniels (Guest)
on 2005-11-21 07:59
(Received via mailing list)
#
# My Solution - unless a mail agent mangles it
# (happens with great frequency)
#

class Card
    attr_reader :suite, :value
    def initialize(string)
        @value = string[0..-2].upcase
        @suite = string[-1..-1].downcase
    end
    def to_i
        case @value
            when 'J': 11
            when 'Q': 12
            when 'K': 13
            when 'A': 1
            else
                @value.to_i
        end
    end
    def to_s
        @value + @suite
    end
end
class EuchreHand
    def initialize(trump, cards)
        @trump_string = trump
        @trump = trump[0..0].downcase
        @cards = cards
    end
    def to_s
        ([@trump_string] +@cards.sort_by {|c| sort_value(c)}).join("\n")
    end
    private
    #there are 6 sections of the sort: jack1, jack2, trump, opp1, same,
opp2
    def sort_value(card)
        return [0] if card.value == 'J' && card.suite == @trump
        same, opposite = [['h','d'],['c','s']].partition {|x|
x.include?(@trump)}
        same[0].delete(@trump)
        same = same[0][0]
        opposite = opposite[0]
        return [1] if card.value == 'J' && card.suite
        suites = [@trump, opposite[0], same, opposite[1]]
        return [2+suites.index(card.suite),-card.to_i]
    end
end

cards = ARGF.readlines
puts EuchreHand.new(cards.shift, cards.map {|s| Card.new(s)})




#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################
This topic is locked and can not be replied to.