Implement strategy for Jacks or Better Video You are dealt five cards from a single deck. Implement a strategy for Jacks or Better Video Poker that has an expected revenue of at least 99.46%. Output the cards you want to hold.

The shortest code wins.

You are not required to implement the suggested strategy. Optimum play is also acceptable.

Input is read from standard input, output is written to standard output. Cards are identified as pairs of number (2,...,9,T,J,Q,K,A) and suit (S,H,D,C - spades, hearts, diamonds, clubs): 3C - three of clubs, QH - queen of hearts.



Examples:

$ ./jacks                               |
3C JC 5C QC 3H                          |&lt;&lt; input
3C 3H                                   |&lt;&lt; output
$ ./jacks                               |
3C 4C 5C 6C 7C                          |&lt;&lt; input
3C 4C 5C 6C 7C                          |&lt;&lt; output
I did it!!!

import java.io.*;
// 427 lines 200
public class Base {

    static char suit (String tokens) {  return  tokens.charAt (1); }
    public static char num (String tokens) { return tokens.charAt (0);}

    public static void checkPair (String [] tokens)
    {
        for (int a = 0; a &lt; 4; ++a)
        for (int b = a+1; b &lt; 5; ++b) 
            if (num (tokens [a]) == num (tokens [b]))
                System.out.print (tokens [a] + " " + tokens [b]);
    }

    public static void checkThreeKind (String [] tokens)
    {
        for (int a = 0; a &lt; 3; ++a)
            if (num (tokens [a]) == num (tokens [a+1]))
                for (int b = 2; b &lt; 5; ++b) 
                    if (num (tokens [a]) == num (tokens [b]))
                        System.out.print (tokens [a] + " " + tokens [a+1] + " " + tokens [b]);
    }

    static void checkTwoPair (String [] tokens)
    {
        for (int i = 0; i &lt; 5; i++)
        for (int j = 1; j &lt; 5; j++)
            //First Increment is pair 1 2, followed by 1 3, 1 4, and 1, 5 then 2 3, 2 4, 2 5, then 3 4, 3 5 then 4 5
            if (num (tokens [i]) == num (tokens [j]))
                for (int y = 0; y &lt;= 4; y++)
                    for (int x = 1; x &lt;= 4; x++)
                        if ((i != y) &amp;&amp; (j != x) &amp;&amp; (y != x) &amp;&amp; (i != j))
                            if (num (tokens [y]) == num (tokens [x]))
                                if ((tokens [i] != tokens [j]) &amp;&amp; (tokens [i] != tokens [y]) &amp;&amp; (tokens [j] != tokens [y]) &amp;&amp;
                                        (tokens [i] != tokens [x]) &amp;&amp; (tokens [j] != tokens [x]) &amp;&amp; (tokens [y] != tokens [x]))
                                    eblock (tokens);
    }

    static void checkHighFlush (String [] tokens)
    {
        if (suit (tokens [0]) == suit (tokens [1]) &amp;&amp;
            suit (tokens [0]) == suit (tokens [2]) &amp;&amp; 
            suit (tokens [0]) == suit (tokens [3]) &amp;&amp; 
            suit (tokens [0]) == suit (tokens [4]))
        {
            String outputString = "";
            for (int i = 0; i &lt; tokens.length; i++)
                outputString  += tokens [i] + " ";
            System.out.println (outputString);
            System.exit (0);
        }
        for (int i = 0; i &lt;= 4; i++)
            for (int j= 1; j&lt;= 4; j++)
                //First Increment is pair 1 2, followed by 1 3, 1 4, and 1, 5 then 2 3, 2 4, 2 5, then 3 4, 3 5 then 4 5
                if (suit (tokens [i]) == suit (tokens [j]))
                    for (int y = 0; y &lt;= 4; y++)
                        for (int x = 1; x &lt;= 4; x++)
                            if ((i != y) &amp;&amp; (j != x) &amp;&amp; (y != x) &amp;&amp; (i != j))
                            {
                                if (suit (tokens [i]) == suit (tokens [x]) &amp;&amp;
                                        suit (tokens [i]) == suit (tokens [y]))
                                {
                                    if ((tokens [i] != tokens [j]) &amp;&amp; (tokens [i] != tokens [y]) &amp;&amp; (tokens [j] != tokens [y]) &amp;&amp;
                                            (tokens [i] != tokens [x]) &amp;&amp; (tokens [j] != tokens [x]) &amp;&amp; (tokens [y] != tokens [x]))
                                        eblock (tokens);
                                }
                            }
    }

     static void checkFourKind (String [] tokens)
     {
        if (num (tokens [0]) == num (tokens [1]))
        {
            if (num (tokens [0]) == num (tokens [2])) 
                if (num (tokens [0]) == num (tokens [3]) || num (tokens [0]) == num (tokens [4]))
                    eblock (tokens);
            if (num (tokens [0]) == num (tokens [3]))
                if (num (tokens [0]) == num (tokens [4]))
                    eblock (tokens);
        }
        if (num (tokens [1]) == num (tokens [2]))
            if (num (tokens [1]) == num (tokens [3])) 
                if (num (tokens [1]) == num (tokens [4])) 
                    eblock (tokens);
    }

    static void checkStraight (String [] tokens)
    {
        int [] ints = new int [5];
        for (int i = 0; i &lt;= 4; i++)
        {   
            switch (num (tokens [i]))        {
            case 'T': ints [i] = 10;
            case 'J': ints [i] = 11;
            case 'Q': ints [i] = 12;
            case 'K': ints [i] = 13;
            case 'A': ints [i] = 14;
            default:  ints [i] = num (tokens [i]) - '0';
        }
        }
        for (int i = 0; i &lt;= 3; i++)
        for (int j= 1; j&lt;= 4; j++)
            //First Increment is pair 1 2, followed by 1 3, 1 4, and 1, 5 then 2 3, 2 4, 2 5, then 3 4, 3 5 then 4 5
            if ((ints [i]+1) == ints [i+1])
                for (int y = 0; y &lt;= 4; y++)
                    for (int x = 1; x &lt;= 4; x++)
                        if ((i != y) &amp;&amp; (j != x) &amp;&amp; (y != x) &amp;&amp; (i != j))
                        {
                            if (ints[i] + 2 == ints[y] &amp;&amp; ints[i] + 3 == ints [x])
                            {
                                if ((tokens [i] != tokens [j]) &amp;&amp; (tokens [i] != tokens [y]) &amp;&amp; (tokens [j] != tokens [y]) &amp;&amp;
                                        (tokens [i] != tokens [x]) &amp;&amp; (tokens [j] != tokens [x]) &amp;&amp; (tokens [y] != tokens [x]))
                                {
                                    for (int a : new int[] {i, j, y, x})
                                        System.out.print (tokens [a] + " ");    
                                    System.out.println ();
                                    System.exit (0);
                                }
                            }
                            java.util.Arrays.sort (ints);
                            if (( (ints [i]+1 == ints [j]) &amp;&amp; (ints [i]+3 == ints [y]) &amp;&amp; (ints [i]+4 == ints [x])))
                            {
                                for (int a : new int[] {i, j, y, x})
                                    System.out.print (tokens [a] + " ");    
                                System.out.println ();
                                System.exit (0);
                            }
                        }
    }

    static void checkHighCard (String [] tokens)
    {
        int [] ints = new int [5];
        for (int i = 0; i &lt;= 4; i++)
        {   
            switch (num (tokens [i]))        {
            case 'T': ints [i] = 10;
            case 'J': ints [i] = 11;
            case 'Q': ints [i] = 12;
            case 'K': ints [i] = 13;
            case 'A': ints [i] = 14;
            default:  ints [i] = num (tokens [i]) - '0';
        }
        }
        int marker = 0;
        if (ints [0] &gt; ints [1]) marker = 1;
        if (ints [1] &gt; ints [2]) marker = 2;
        if (ints [2] &gt; ints [3]) marker = 3;
        if (ints [3] &gt; ints [4]) marker = 4;
        System.out.println (tokens [marker]);
    }

    static void eblock (String[] tokens) 
    {
        for (int i = 0; i &lt; 5; i++)
            System.out.print (tokens [i] + " ");
        System.out.println ();
        System.exit (0);
    }

    static void checkFullHouse (String [] tokens)
    {
        String [] test = new String [5];
        int counter = 0;
        int sum = 0;

        for (int i = 0; i &lt;= 4; i++)
        test [i] = tokens [i];
        for (int i = 0; i &lt;=4; i++)
        {
        for (int j = 1; j &lt;= 4; j++)
        {
            if (num (test [i]) == num (tokens [j]))
                counter++;
        }
        if (counter &gt; 1) 
            sum++;
        counter = 0;
        }
        if (sum == 5) eblock (tokens);
    }

    public static void main (String [] args) throws IOException
    {
        String inputString = new BufferedReader (new InputStreamReader (System.in)).readLine ();
        String [] tokens = inputString.split (" ");
        java.util.Arrays.sort (tokens);
        checkFourKind (tokens);
        checkHighFlush (tokens);
        checkFullHouse (tokens);
        checkStraight (tokens);
        checkTwoPair (tokens);
        checkThreeKind (tokens);
        checkPair (tokens);
        checkHighCard (tokens);
    }   
}