Play Best Card in Euchre

13

1

Input:

Firstly
An array of three cards representing the cards played by each player formatted like

[JD][TH][9S]

Representing

Jack of Diamonds, 10 of Hearts, and Nine of Spades.

As you always sit across from your team-mate in Euchre, the second element represents your team mate's play. Here, the TH.

Secondly
A Single Char, String, etc representing the trump suit formatted like

S, D, C, H

representing

Spades, Diamonds, Clubs, Hearts

Thirdly an array of four cards representing your hand formatted like

[KD][JC][QH][AS]

representing

King of Diamonds, Jack of Clubs, Queen of Hearts, Ace of Spades

Objective:

Given the three inputs, output the best possible card to add to the cards that have been played such that it meets the following criteria:

  1. It takes the hand if it can, if not output the least valuable card
  2. It takes the hand if it can, however it will not trump your team mate unless it is unavoidable
  3. If it can take the hand, it does so by using the least valuable card. (If you have an ace and a queen that can win the hand, you play the queen).
  4. Any plays must follow suit as required by the rules at the bottom.

Output format like [JD]

Formatting

A - Ace
K - King
Q - Queen
J - Jack
T - Ten
9 - Nine

H - Hearts
D - Diamonds
S - Spades
C - Clubs

TH, 9D, JD, QH, AD, 9C, TC

Examples

In: [QD][KD][9C], "C", [AH][JH][QH][9H]

Out: 9H

Reason: As clubs are trump, the 9C is winning the hand, we are unable to take the hand so we should discard our lowest card, here the 9H

In: [QD][KD][AD], "H", [AH][JH][QH][9H]

Out: 9H

Reason: As Hearts are trump, the Ace of Diamonds is currently winning the hand, we are able to trump the hand so we should use our lowest card, here the 9H

In: [QD][KD][TD], "D", [AD][JH][QH][9D]

Out: 9D

Reason: As diamonds are trump and we are currently winning the hand, we should play the 9D because our partner currently is winning the hand, so we want to play the 9D over the AD

In: [QH][KH][JH], "D", [AD][JD][QH][9D]

Out: QH

Reason: As Diamonds are trump our opponents are winning with the left Bower JH We have the right bower but cannot trump him because QH was led and we must follow suit, the QH

In: [QH][KH][JH], "D", [AD][JD][QC][9D]

Out: JD

Reason: As Diamonds are trump our opponents are winning with the left Bower JH We have the right bower and since we have no Diamonds we can trump over him with the JD

Euchre Card Strengths

If Hearts is trump:

JH
JD
AH
KH
QH
TH
9H

See the Euchre Rules if you are unfamiliar with the strength of different cards in Euchre

Since this is Code-Golf the shortest code wins!

Good Luck and have fun!

jacksonecac

Posted 2016-11-15T12:19:04.383

Reputation: 2 584

2I think the play should be described here not with a link. – Jonathan Allan – 2016-11-15T12:49:33.667

@JonathanAllan I thought about it, but it is better described in the link. Probably not the best challenge for those who are unfamiliar with the game. Unless they want to learn on the fly. – jacksonecac – 2016-11-15T12:51:10.240

The example with 9D as the result has hearts set as trumps (although I don't think it affects this result). – Jonathan Allan – 2016-11-15T13:02:18.217

@JonathanAllan nice catch, thank you. – jacksonecac – 2016-11-15T13:04:50.397

Would it maybe be better if 10 was denoted as T? Would make the card parsing less of a headache. – Kade – 2016-11-15T16:56:28.673

Good idea, I will make the change – jacksonecac – 2016-11-15T16:57:09.110

Why take a trick that your partner has already won? – Sparr – 2018-12-11T04:31:36.970

@Sparr the 2nd-to-last test case addresses this: In: [QH][KH][JH], "D", [AD][JD][QH][9D] Out: QH. We play the QH because our partner already won. Although, this hand is not possible in euchre because it has the QH both in-play and in-hand. – Dave – 2018-12-11T07:11:12.177

I'm more asking from a strategy standpoint. Seems like bad euchre play. – Sparr – 2018-12-11T08:32:15.427

@Sparr because the first card played was [QH] meaning the rest of the play is hearts by default. So you would only be able to play [QH] because you have a hearts card so you have to play it. I think that's how this game works, at least a similar Chinese card game works this way. – HyperNeutrino – 2018-12-11T14:29:50.497

I just read the examples and it seems like they match my expectations and violate the objective. I think whoever wrote this used "trump your team mate" when they meant any play over them, not just trumps? – Sparr – 2018-12-11T22:33:37.807

Would the rules be better stated as this? 1) If your team mate is winning, play the lowest ranked legal card. 2) If your team mate is losing, play the lowest ranked legal card that wins the hand if you can, otherwise play the lowest ranked legal card. – Sparr – 2018-12-11T22:35:34.213

Yes, that is a clearer and more concise description of the objective. 'Legal card' is defined as cards matching the lead suit (the suit of the first card played), or all cards if no cards in-hand match the lead suit. 'Trump' is considered a suit, and the Jack of the same color as trump is considered trump (not its actual suit). 'Lowest ranked' is defined as: Jack of trump > Jack of same color as trump > (A > K > Q > T > 9) of trump > (A > K > Q > J > T > 9 of lead suit) > all other cards. – Dave – 2018-12-12T01:24:43.950

Agreed but its been 2 years go golf something else :) – jacksonecac – 2018-12-12T01:25:52.717

1@jacksonecac thanks for the problem, I had fun with it. – Dave – 2018-12-12T01:28:30.050

Answers

2

Perl - 557 532 511 490 482 384 363

($p,$t,$h)=@ARGV;%L=(H=>D,D=>H,C=>S,S=>C);$B=%L{$t};$_="\]$p$h\[";s/(.$t)/0$1/g;s/J$B/01$B/;s/0J/00/;s/J/R/g;s/9/Z/g;($T,@C)=split/\]\[/,$_;$s=$C[0];$s=~s/.+(.)/$1/;$s=~s/$t/0/;$X=(sort@C[0..2])[0];@M=@C[3..6];@F=(grep{/$s/}@M);@M=@F if@F;$w=(sort@M)[@M-1];push@M,$X;foreach$g(sort@M){$g ne$X?$b=$g:last}$_=$C[1]eq$X||!$b?$w:$b;s/.(..)/$1/;s/Z/9/;s/0|1|R/J/;print

How it works

Try it online!

First it reads args and finds the left-bauer's suit (jack of the same color as trump) with a hash lookup:

($p,$t,$h)=@ARGV;
%L=(H=>D,D=>H,C=>S,S=>C);
$B=%L{$t};

Then it combines all cards into one string and does some replacements, making the cards naturally sort in the correct order for euchre:

$_="\]$p$h\[";
s/(.$t)/0$1/g; # trump cards start with '0'
s/J$B/01$B/;   # left-bauer's 'J' changes to '01', making it trump
s/0J/00/;      # right-bauer's 'J' changes to '0'
s/J/R/g;       # all other jacks have 'J' changed to 'R'
s/9/Z/g;       # all 9s change to Z
($T,@C)=split/\]\[/,$_;

At the end of this block, the card string is then split on the brackets, which builds an array of all cards where:

  • index 0 = opponent's lead card

  • index 1 = teammate's card

  • index 2 = other opponent's card

  • indices 3-6 represent the hand

The lead suit is parsed from the first card, but the suit changes to '0' to represent trump if trump was led:

$s=$C[0];
$s=~s/.+(.)/$1/;
$s=~s/$t/0/;

The best card in-play is found by sorting the first three cards and retrieving the first card:

$X=(sort@C[0..2])[0];

The playable cards are found. If any cards in-hand match the lead suit, then only those cards remain. Otherwise all cards are considered playable:

@M=@C[3..6];
@F=(grep{/$s/}@M);
@M=@F if@F;

The 'throwaway' card is found by returning the last card in the sorted array of playable cards:

$w=(sort@M)[@M-1];

The lowest 'winning' card that can win the trick is found by adding the highest card in-play to the array of playable cards, sorting the array, and iterating over it until the highest card in-play is found. The 'winning' card is the card from the previous iteration:

push@M,$X;
foreach$g(sort@M){$g ne$X?$b=$g:last}

The correct play is then evaluated. The 'throwaway' card is chosen if any of the following are true:

  1. The best card on the table has an index of 1 in the unsorted array of all cards, meaning it belongs to our teammate
  2. The search for the lowest 'winning' card came up null, meaning our opponent's best card is higher than all cards in the hand

Otherwise the 'winning' card is returned:

$_=$C[1]eq$X||!$b?$w:$b;
s/.(..)/$1/; # remove the '0' that indicated trump
s/Z/9/;      # get those 9s back
s/0|1|R/J/;  # fix the jacks
print

Dave

Posted 2016-11-15T12:19:04.383

Reputation: 325