[SOLUTION] Euchre Hands (#55)

Discussion in 'Ruby' started by Warren Brown, Nov 21, 2005.

  1. Warren Brown

    Warren Brown Guest

    All,

    This quiz looked so simple I just couldn't resist putting in the
    short amount of time needed to solve it. Also, I love to play Euchre.

    I see other solutions trying very hard to make sure that two suits
    of the same color do not appear next to each other. As a Euchre player,
    I would be quite upset if the order of the suits changed during play.
    For example, if hearts were trump, and the hand is (9h 9s 9d 9c Tc), and
    the 9s is played, the hand should stay (9h 9d 9c Tc), not be reordered
    to (9h 9c Tc 9d). I tend to always keep my suits in the same order (s h
    c d) and simply rotate the order so that the trump suit is first (e.g.
    (c d s h) if clubs are trump).

    I also made sure the routine works with fewer (or more) than five
    cards since cards are actually played from the hand during a game.


    ----------------------------------------------------------------
    # Constants
    RANKS =3D 'AKQJT9'
    SUITS =3D 'SHCD'
    =20
    def euchre_sort(cards)
    # Split out trump
    trump =3D cards.shift

    # Quick and dirty validation
    trump_index =3D SUITS.index(trump[0,1].upcase)
    unless trump_index
    puts "Invalid trump (#{trump})."
    exit
    end
    cards.each do |card|
    if card !~ /^[#{RANKS}][#{SUITS}]$/i
    puts "Invalid card (#{card})."
    exit
    end
    end

    # Return sorted hand
    right_bower =3D "J#{SUITS[trump_index,1]}"
    left_bower =3D "J#{SUITS[trump_index - 2,1]}"
    [trump] +
    cards.sort_by do |card|
    upcase_card =3D card.upcase
    case upcase_card
    when right_bower then -2
    when left_bower then -1
    else RANKS.index(upcase_card[0,1]) +
    10 * ((SUITS.index(upcase_card[1,1]) - trump_index) % 4)
    end
    end
    end

    puts euchre_sort(STDIN.read.split)
    ----------------------------------------------------------------


    This solution (minus error handling) is also very amenable to
    golfing at 187 characters:

    R=3D'AKQJT9';S=3D'shcd';i=3D$<.read.split;t=3Di.shift;x=3DS.index(t[0,1]
    downcase);r=3D"J#{S[x,1]}";l=3D"J#{S[x-2,1]}";puts(t,i.sort_by{|c|
    c=3D=3Dr ?-2:c=3D=3Dl ?-1:R.index(c[0,1])+10*((S.index(c[1,1])-x)%4)})


    - Warren Brown
    Warren Brown, Nov 21, 2005
    #1
    1. Advertising

  2. Warren Brown

    Rube Guest

    Re: Euchre Hands (#55)

    That is a clever way to sort by suit (trump first), but what happens
    when trump is spades and your hand consists of spades, clubs, and
    diamonds. Won't your program return spades next to clubs, instead of
    alternating spades, diamonds, clubs?
    Rube, Nov 21, 2005
    #2
    1. Advertising

  3. On Nov 21, 2005, at 2:00 AM, Warren Brown wrote:

    > I see other solutions trying very hard to make sure that two suits
    > of the same color do not appear next to each other. As a Euchre
    > player,
    > I would be quite upset if the order of the suits changed during play.


    Hold on there Tiger... I didn't say anything about changing the suit
    order. We're only working with the initial sort here.

    I believe a "correct" implementation caches the initial suit order
    for each player and holds that until the hand is played out (even if
    black/red suits eventually touch). Note that you will have to order
    the hand twice though, once after the deal and once after Trump is
    chosen.

    Of course, this is all just *MY* opinion of right. We've already
    heard of several others. This quiz just isn't about those... ;)

    James Edward Gray II
    James Edward Gray II, Nov 23, 2005
    #3
    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. xfairguy
    Replies:
    4
    Views:
    385
  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. Daniel Sheppard

    [QUIZ SOLUTION] Euchre Hands (#55)

    Daniel Sheppard, Nov 21, 2005, in forum: Ruby
    Replies:
    0
    Views:
    92
    Daniel Sheppard
    Nov 21, 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