[QUIZ] Euchre Hands (#55)

Discussion in 'Ruby' started by Ruby Quiz, Nov 18, 2005.

  1. Ruby Quiz

    Ruby Quiz Guest

    The three rules of Ruby Quiz:

    1. Please do not post any solutions or spoiler discussion for this quiz until
    48 hours have passed from the time on this message.

    2. Support Ruby Quiz by submitting ideas as often as you can:

    http://www.rubyquiz.com/

    3. Enjoy!

    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

    The card game of Euchre has an unusual ordering of cards in the hand. This
    week's Ruby Quiz is to take a random Euchre hand and sort it.

    The first thing you need to know is that Euchre is played with a small deck of
    cards. Four suits are used Diamonds (d), Clubs (c), Spades (s), and Hearts (h),
    but each suit has only the cards Nine (9), Ten (T), Jack (J), Queen (Q), King
    (K), and Ace (A). The cards are generally ordered as I just listed them, Nine
    being the low card and Ace the high card. The exception is the "Bowers".

    When a Euchre hand is started, the first task is to select a Trump suit. How
    that's done is not important, just know that one suit is always different from
    the rest. Trump is the best suit, valued higher than the other three suits
    (which are basically equal). In the Trump suit, the card order changes.

    The first oddity of Trump is that the Jack of the selected suit becomes the
    Right Bower, the highest ranked Trump card. The second oddity is that the other
    Jack of the same color (Diamonds and Hearts are red while Clubs and Spades are
    black) becomes the Left Bower, the second highest Trump card. This card is
    considered to be of the Trump suit for the rest of the hand. For example, if
    Spades is selected as Trump, the order of Spades becomes (lowest to highest):
    9s, Ts, Qs, Ks, As, Jc, and Js. All other suits run Nine to Ace, save that
    Clubs will be short a Jack.

    The three non-Trump suits are equal, but it is good interface to sort the by
    suit alternating red, black, red, and black, I think. Especially with a GUI,
    this makes it easier to understand the hand.

    Input (on STDIN) will be a line containing the Trump suit, followed by five
    lines containing a Euchre hand. For example:

    Diamonds
    Kc
    Jh
    Kd
    Td
    Ah

    Your script should output (to STDOUT) the Trump suit, followed by the cards in
    sorted order (highest card first):

    Diamonds
    Jh
    Kd
    Td
    Kc
    Ah

    Here's a script that will feed your program random hands:

    #!/usr/local/bin/ruby -w

    # build a Euchre deck
    cards = Array.new
    %w{9 T J Q K A}.each do |face|
    %w{d c s h}.each do |suit|
    cards << face + suit
    end
    end

    # choose trump
    puts %w{Diamonds Clubs Spades Hearts}[rand(4)]

    # deal a hand
    cards = cards.sort_by { rand }
    puts cards[0..4]

    One last thought: If accuracy is our ultimate goal here, how will you know your
    output is correct?
    Ruby Quiz, Nov 18, 2005
    #1
    1. Advertising

  2. On 18/11/05, Ruby Quiz <> wrote:
    > The three rules of Ruby Quiz:
    >
    > 1. Please do not post any solutions or spoiler discussion for this quiz =

    until
    > 48 hours have passed from the time on this message.
    >
    > 2. Support Ruby Quiz by submitting ideas as often as you can:
    >
    > http://www.rubyquiz.com/
    >
    > 3. Enjoy!
    >
    > -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=

    =3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D=
    -=3D-=3D-=3D
    >
    > The card game of Euchre has an unusual ordering of cards in the hand. Th=

    is
    > week's Ruby Quiz is to take a random Euchre hand and sort it.
    >
    > The first thing you need to know is that Euchre is played with a small de=

    ck of
    > cards. Four suits are used Diamonds (d), Clubs (c), Spades (s), and Hear=

    ts (h),
    > but each suit has only the cards Nine (9), Ten (T), Jack (J), Queen (Q), =

    King
    > (K), and Ace (A). The cards are generally ordered as I just listed them,=

    Nine
    > being the low card and Ace the high card. The exception is the "Bowers".
    >
    > When a Euchre hand is started, the first task is to select a Trump suit. =

    How
    > that's done is not important, just know that one suit is always different=

    from
    > the rest. Trump is the best suit, valued higher than the other three sui=

    ts
    > (which are basically equal). In the Trump suit, the card order changes.
    >
    > The first oddity of Trump is that the Jack of the selected suit becomes t=

    he
    > Right Bower, the highest ranked Trump card. The second oddity is that th=

    e other
    > Jack of the same color (Diamonds and Hearts are red while Clubs and Spade=

    s are
    > black) becomes the Left Bower, the second highest Trump card. This card =

    is
    > considered to be of the Trump suit for the rest of the hand. For example=

    , if
    > Spades is selected as Trump, the order of Spades becomes (lowest to highe=

    st):
    > 9s, Ts, Qs, Ks, As, Jc, and Js. All other suits run Nine to Ace, save th=

    at
    > Clubs will be short a Jack.
    >
    > The three non-Trump suits are equal, but it is good interface to sort the=

    by
    > suit alternating red, black, red, and black, I think. Especially with a =

    GUI,
    > this makes it easier to understand the hand.
    >
    > Input (on STDIN) will be a line containing the Trump suit, followed by fi=

    ve
    > lines containing a Euchre hand. For example:
    >
    > Diamonds
    > Kc
    > Jh
    > Kd
    > Td
    > Ah
    >
    > Your script should output (to STDOUT) the Trump suit, followed by the car=

    ds in
    > sorted order (highest card first):
    >
    > Diamonds
    > Jh
    > Kd
    > Td
    > Kc
    > Ah
    >
    > Here's a script that will feed your program random hands:
    >
    > #!/usr/local/bin/ruby -w
    >
    > # build a Euchre deck
    > cards =3D Array.new
    > %w{9 T J Q K A}.each do |face|
    > %w{d c s h}.each do |suit|
    > cards << face + suit
    > end
    > end
    >
    > # choose trump
    > puts %w{Diamonds Clubs Spades Hearts}[rand(4)]
    >
    > # deal a hand
    > cards =3D cards.sort_by { rand }
    > puts cards[0..4]
    >
    > One last thought: If accuracy is our ultimate goal here, how will you kn=

    ow your
    > output is correct?
    >
    >


    Hello James,

    I have to admit that I do not totally understand. Why is in the
    example the King of Clubs higher than the Ace of hearts. I thought an
    Ace would be higher than a King and Clubs and Hearts are equal except
    for the Jack if the trump is Diamond.

    And secondly I do not understand the paragraph about the red-black sort ord=
    er.

    cheers,

    Brian
    --
    http://ruby.brian-schroeder.de/

    Stringed instrument chords: http://chordlist.brian-schroeder.de/
    Brian Schröder, Nov 19, 2005
    #2
    1. Advertising

  3. On Nov 19, 2005, at 1:31 PM, Brian Schr=F6der wrote:

    >> Your script should output (to STDOUT) the Trump suit, followed by =20
    >> the cards in
    >> sorted order (highest card first):
    >>
    >> Diamonds
    >> Jh
    >> Kd
    >> Td
    >> Kc
    >> Ah


    > Hello James,


    Hello Brian.

    > I have to admit that I do not totally understand.


    That's probably just my terrible explination. Let's see if I can =20
    clear it up...

    > Why is in the example the King of Clubs higher than the Ace of hearts.


    Cards are sorted first buy suit, then by value. Trump is a better =20
    suit then the rest, but the others are equal and can come in any =20
    order. The suit order above is Diamonds (Trump), Clubs, then =20
    Hearts. Inside those suits, cards are sorted by face value.

    > And secondly I do not understand the paragraph about the red-black =20
    > sort order.


    Technically, this is another (correct) answer to the above:

    Diamonds
    Jh
    Kd
    Td
    Ah
    Kc

    The cards are still sorted in order of value. However, now we have =20
    put the red suits together. I think it's better to break them up, =20
    when we can. That's why my example shows Clubs between the two suits.

    Does that help?

    James Edward Gray II
    James Edward Gray II, Nov 19, 2005
    #3
  4. On 19/11/05, James Edward Gray II <> wrote:
    > On Nov 19, 2005, at 1:31 PM, Brian Schr=F6der wrote:
    >
    > >> Your script should output (to STDOUT) the Trump suit, followed by
    > >> the cards in
    > >> sorted order (highest card first):
    > >>
    > >> Diamonds
    > >> Jh
    > >> Kd
    > >> Td
    > >> Kc
    > >> Ah

    >
    > > Hello James,

    >
    > Hello Brian.
    >
    > > I have to admit that I do not totally understand.

    >
    > That's probably just my terrible explination. Let's see if I can
    > clear it up...
    >
    > > Why is in the example the King of Clubs higher than the Ace of hearts.

    >
    > Cards are sorted first buy suit, then by value. Trump is a better
    > suit then the rest, but the others are equal and can come in any
    > order. The suit order above is Diamonds (Trump), Clubs, then
    > Hearts. Inside those suits, cards are sorted by face value.
    >


    Now I understand. I always sort my cards first by value then by suite.
    That was the problem.

    cheers,

    Brian

    > > [snip]

    >
    > Does that help?


    Yes

    >
    > James Edward Gray II
    >
    >
    >



    --
    http://ruby.brian-schroeder.de/

    Stringed instrument chords: http://chordlist.brian-schroeder.de/
    Brian Schröder, Nov 19, 2005
    #4
  5. Ruby Quiz

    Sam Gentle Guest

    On 11/18/05, Ruby Quiz <> wrote:
    > The card game of Euchre has an unusual ordering of cards in the hand. Th=

    is
    > week's Ruby Quiz is to take a random Euchre hand and sort it.


    Hmm. My original solution was to assign integer values to each card
    and suit, and sort_by that mapping.

    #!/usr/bin/ruby -w

    NUMS =3D %w(9 T J Q K A)
    SUITS =3D %w(h c d s)
    COLOURS =3D Hash[*%w(s B d R c B h R)]

    def card_value(card, trump)
    =09num, suit =3D card.split('')

    =09value =3D NUMS.index(num)
    =09value +=3D 10 * SUITS.index(suit)
    =09if num =3D=3D 'J'
    =09=09value +=3D 150 if COLOURS[suit] =3D=3D COLOURS[trump]
    =09=09value +=3D 10 if suit =3D=3D trump
    =09elsif suit =3D=3D trump
    =09=09value +=3D 100
    =09end
    =09
    =09return value
    end

    trump =3D gets.chomp
    tsuit =3D trump[0,1].downcase

    puts trump
    puts $stdin.readlines.sort_by { |card| -card_value(card,tsuit) }


    When I started to think about it though, that seemed a little silly to
    me. If you ever use more than ten numbers, or more than 5 cards,
    things start to get messy and sneaky bugs pop up. I realised that what
    I was really doing was creating an array of base-10 numbers to
    represent the cards. Why not just create an actual array and be done
    with it?

    So, here's my version 2:

    #!/usr/bin/ruby -w

    NUMS =3D %w(9 T J Q K A)
    SUITS =3D %w(h c d s)
    COLOURS =3D Hash[*%w(s B d R c B h R)]

    def card_value(card, trump)
    =09num, suit =3D card.split('')
    =09
    =09value =3D Array.new
    =09
    =09value << (num =3D=3D 'J' && value[3] =3D=3D 1 ? 1 : 0)
    =09value << (num =3D=3D 'J' && COLOURS[suit] =3D=3D COLOURS[trump] ? 1 : 0)
    =09value << (suit =3D=3D trump ? 1 : 0)
    =09value << SUITS.index(suit)
    =09value << NUMS.index(num)
    end

    trump =3D gets.chomp
    tsuit =3D trump[0,1].downcase

    puts trump
    puts $stdin.readlines.sort_by { |card| card_value(card,tsuit) }.reverse


    Though, incidentally, I'm not sure how the performance of the two
    would compare. I also wish I could think of a neater way to include
    those boolean values than the ? 1 : 0 thing. I considered defining <=3D>
    for TrueClass and FalseClass, but thought that might be a little icky.

    Sam
    Sam Gentle, Nov 20, 2005
    #5
  6. Re: Euchre Hands (#55)

    I seriously debated with myself whether it was worth submitting my
    solution. It takes a whole 100 lines of code! I decided in the end that
    I would post it anyway.

    I made the opposite decision to Robin Stocker and decided to stick with
    the <=> approach. The cards do have to know about the trump suit, but
    the deck class automatically handles that.

    Here it is:

    class Card
    @@values = {'A' => :ace, 'K' => :king, 'Q' => :queen, 'J' => :jack,
    'T' => :ten, '9' => :nine}
    @@value_values = {:ace => 6, :king => 5, :queen => 4, :jack => 3, :ten
    => 2, :nine => 1}
    @@suits = {'h' => :hearts, 'd' => :diamonds, 'c' => :clubs, 's' =>
    :spades}
    @@suit_values = {:hearts => 2, :diamonds => -2, :clubs => 1, :spades
    => -1}

    # The suit_values is used to compactly calculate the order suits
    should be sorted in

    def initialize(string = "Jh", trump = :hearts)
    @suit = :hearts
    @value = :ace
    @trump = trump
    if string
    self.parse(string)
    end
    end

    def parse(string)
    cardarray = string.upcase.scan(/[AKQJT9][HDCS]/)
    if cardarray.length > 0
    @value = @@values[cardarray[0][0,1]]
    @suit = @@suits[(cardarray[0][1,1]).downcase]
    end
    end

    def rank_value
    case @suit
    when @trump
    return @value == :jack ? 40 : (@@value_values[@value] + 30)
    when @@suit_values.index(@@suit_values[@trump] * -1)
    # True when card is of the trump's suit's colour companion
    return @value == :jack ? 39 : (@@value_values[@value] + 10)
    when @@suit_values.index((@@suit_values[@trump] % 2) + 1)
    # Triggers for clubs if trump is red or hearts if trump is black
    return @@value_values[@value] + 20
    else
    # The last remaining suit comes last
    return @@value_values[@value]
    end
    end

    def <=>(other)
    self.rank_value <=> other.rank_value
    end

    def printable
    @@values.index(@value) + @@suits.index(@suit)
    end

    include Comparable

    attr_accessor :trump, :value, :suit
    end

    class Hand
    def initialize(trump = :hearts, *cards)
    @cards = Array.new
    @trump = trump
    cards.each do |cs|
    @cards << Card.new(cs, trump)
    end
    @cards.sort!
    end

    def add_card(card)
    @cards << Card.new(card, @trump)
    end

    def display
    @cards.sort!
    @cards.reverse.each do |card|
    puts card.printable
    end
    end

    def inspect
    handstring = String.new
    @cards.each do |card|
    handstring << card.printable << ', '
    end
    handstring.chop!
    handstring.chop!
    end

    def trump=(newtrump)
    @trump = newtrump
    @cards.each {|c| c.trump = newtrump}
    end

    attr_accessor :cards
    attr_reader :trump
    end

    if __FILE__ == $0

    until (trumpline = readline()) != ""
    end

    case trumpline.downcase[0,1]
    when 'h'
    trump = :hearts
    when 'd'
    trump = :diamonds
    when 'c'
    trump = :clubs
    when 's'
    trump = :spades
    else
    raise StandardError, "Bad Input"
    end

    h = Hand.new(trump)

    5.times do |t|
    cardline = readline()
    h.add_card(cardline)
    end

    puts trump.to_s.capitalize
    h.display

    end
    Timothy Goddard, Nov 21, 2005
    #6
    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. Ruby Quiz

    [QUIZ] Animal Quiz (#15)

    Ruby Quiz, Jan 14, 2005, in forum: Ruby
    Replies:
    11
    Views:
    364
    James Edward Gray II
    Jan 18, 2005
  2. Robin Stocker

    [QUIZ SOLUTION] Euchre Hands (#55)

    Robin Stocker, Nov 20, 2005, in forum: Ruby
    Replies:
    26
    Views:
    244
    Zed Lopez
    Nov 23, 2005
  3. Daniel Sheppard

    [QUIZ SOLUTION] Euchre Hands (#55)

    Daniel Sheppard, Nov 21, 2005, in forum: Ruby
    Replies:
    0
    Views:
    101
    Daniel Sheppard
    Nov 21, 2005
  4. Warren Brown

    [SOLUTION] Euchre Hands (#55)

    Warren Brown, Nov 21, 2005, in forum: Ruby
    Replies:
    2
    Views:
    94
    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:
    136
    James Gray
    Nov 24, 2005
Loading...

Share This Page