Play Best Card in Euchre 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:


It takes the hand if it can, if not output the least valuable card
It takes the hand if it can, however it will not trump your team mate unless it is unavoidable  
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).
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!
Perl - 557 532 511 490 482 384 363

($p,$t,$h)=@ARGV;%L=(H=&gt;D,D=&gt;H,C=&gt;S,S=&gt;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=&gt;D,D=&gt;H,C=&gt;S,S=&gt;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:


The best card on the table has an index of 1 in the unsorted array of all cards, meaning it belongs to our teammate
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