[QUIZ SOLUTION] Euchre Hands (#55)

Discussion in 'Ruby' started by Daniel Sheppard, Nov 21, 2005.

  1. #
    # My Solution - unless a mail agent mangles it=20
    # (happens with great frequency)
    #

    class Card
    =20 attr_reader :suite, :value
    =20 def initialize(string)
    =20 @value =3D string[0..-2].upcase
    =20 @suite =3D string[-1..-1].downcase
    =20 end
    =20 def to_i
    =20 case @value
    =20 when 'J': 11
    =20 when 'Q': 12
    =20 when 'K': 13
    =20 when 'A': 1
    =20 else
    =20 @value.to_i
    =20 end
    =20 end
    =20 def to_s
    =20 @value + @suite
    =20 end
    end
    class EuchreHand
    =20 def initialize(trump, cards)
    =20 @trump_string =3D trump
    =20 @trump =3D trump[0..0].downcase
    =20 @cards =3D cards
    =20 end
    =20 def to_s
    =20 ([@trump_string] _by {|c| sort_value(c)}).join("\n"=
    )
    =20 end
    =20 private
    =20 #there are 6 sections of the sort: jack1, jack2, trump, opp1, same,=

    opp2
    =20 def sort_value(card)
    =20 return [0] if card.value =3D=3D 'J' && card.suite =3D=3D @trump=

    =20 same, opposite =3D [['h','d'],['c','s']].partition {|x|
    x.include?(@trump)}
    =20 same[0].delete(@trump)
    =20 same =3D same[0][0]
    =20 opposite =3D opposite[0]
    =20 return [1] if card.value =3D=3D 'J' && card.suite
    =20 suites =3D [@trump, opposite[0], same, opposite[1]]
    =20 return [2+suites.index(card.suite),-card.to_i]
    =20 end
    end

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




    #########################################################################=
    ############
    This email has been scanned by MailMarshal, an email content filter.
    #########################################################################=
    ############
    Daniel Sheppard, Nov 21, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Tran
    Replies:
    9
    Views:
    174
    David Tran
    Jan 21, 2005
  2. Ruby Quiz

    [QUIZ] Euchre Hands (#55)

    Ruby Quiz, Nov 18, 2005, in forum: Ruby
    Replies:
    5
    Views:
    144
    Timothy Goddard
    Nov 21, 2005
  3. Robin Stocker

    [QUIZ SOLUTION] Euchre Hands (#55)

    Robin Stocker, Nov 20, 2005, in forum: Ruby
    Replies:
    26
    Views:
    240
    Zed Lopez
    Nov 23, 2005
  4. Warren Brown

    [SOLUTION] Euchre Hands (#55)

    Warren Brown, Nov 21, 2005, in forum: Ruby
    Replies:
    2
    Views:
    92
    James Edward Gray II
    Nov 23, 2005
  5. Ruby Quiz

    [SUMMARY] Euchre Hands (#55)

    Ruby Quiz, Nov 24, 2005, in forum: Ruby
    Replies:
    2
    Views:
    124
    James Gray
    Nov 24, 2005
Loading...

Share This Page