The three rules of Ruby Q.:
-
Please do not post any solutions or spoiler discussion for this quiz
until
48 hours have passed from the time on this message. -
Support Ruby Q. by submitting ideas as often as you can:
- Enjoy!
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
The card game of Euchre has an unusual ordering of cards in the hand.
This
week’s Ruby Q. 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?