# Euchre Hands (#55)

**URL:** <https://www.ruby-forum.com/t/euchre-hands-55/93288>\
**Category:** Ruby\
**Created:** [November 21, 2005, 9:03am UTC](https://www.ruby-forum.com/t/euchre-hands-55/93288 "2005-11-21T09:03:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![warrenbrown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f9ae1b/32.png) [@warrenbrown](https://www.ruby-forum.com/u/warrenbrown)\
**Post date:** [November 21, 2005, 9:03am UTC](https://www.ruby-forum.com/t/euchre-hands-55/93288/1 "2005-11-21T09:03:32Z")

</div>

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 = ‘AKQJT9’  
SUITS = ‘SHCD’

def euchre\_sort(cards)

# Split out trump

trump = cards.shift

# Quick and dirty validation

trump\_index = 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 = “J#{SUITS[trump\_index,1]}”  
left\_bower = “J#{SUITS[trump\_index - 2,1]}”  
[trump] +  
cards.sort\_by do |card|  
upcase\_card = 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=‘AKQJT9’;S=‘shcd’;i=$\<.read.split;t=i.shift;x=S.index(t[0,1]  
.downcase);r=“J#{S[x,1]}”;l=“J#{S[x-2,1]}”;puts(t,i.sort\_by{|c|  
c==r ?-2:c==l ?-1:R.index(c[0,1])+10\*((S.index(c[1,1])-x)%4)})

```
- Warren B.
```

---

<div class="post-metadata">

**Author:** ![warrenbrown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f9ae1b/32.png) [@warrenbrown](https://www.ruby-forum.com/u/warrenbrown)\
**Post date:** [November 21, 2005, 7:49pm UTC](https://www.ruby-forum.com/t/euchre-hands-55/93288/2 "2005-11-21T19:49:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![warrenbrown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f9ae1b/32.png) [@warrenbrown](https://www.ruby-forum.com/u/warrenbrown)\
**Post date:** [November 23, 2005, 1:58am UTC](https://www.ruby-forum.com/t/euchre-hands-55/93288/3 "2005-11-23T01:58:04Z")

</div>

On Nov 21, 2005, at 2:00 AM, Warren B. 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 G. II
