7
\$\begingroup\$

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.

enter image description here

Examples:

$ ./jacks                               |
3C JC 5C QC 3H                          |<< input
3C 3H                                   |<< output
$ ./jacks                               |
3C 4C 5C 6C 7C                          |<< input
3C 4C 5C 6C 7C                          |<< output
\$\endgroup\$
  • \$\begingroup\$ Is this a single deck? It simplifies the logic because, for example, if a hand is 4 to a flush it can't have a 3 of a kind. \$\endgroup\$ – Keith Randall Jun 30 '11 at 4:33
  • 1
    \$\begingroup\$ @Keith: Single deck. \$\endgroup\$ – Alexandru Jun 30 '11 at 6:53
  • \$\begingroup\$ Can we use T instead of 10, it would simplify things greatly! \$\endgroup\$ – mellamokb Jun 30 '11 at 14:29
  • \$\begingroup\$ @mellamokb: My bad. Yes. I'm updating now. \$\endgroup\$ – Alexandru Jun 30 '11 at 14:56
2
\$\begingroup\$

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 < 4; ++a)
        for (int b = a+1; b < 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 < 3; ++a)
            if (num (tokens [a]) == num (tokens [a+1]))
                for (int b = 2; b < 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 < 5; i++)
        for (int j = 1; j < 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 <= 4; y++)
                    for (int x = 1; x <= 4; x++)
                        if ((i != y) && (j != x) && (y != x) && (i != j))
                            if (num (tokens [y]) == num (tokens [x]))
                                if ((tokens [i] != tokens [j]) && (tokens [i] != tokens [y]) && (tokens [j] != tokens [y]) &&
                                        (tokens [i] != tokens [x]) && (tokens [j] != tokens [x]) && (tokens [y] != tokens [x]))
                                    eblock (tokens);
    }

    static void checkHighFlush (String [] tokens)
    {
        if (suit (tokens [0]) == suit (tokens [1]) &&
            suit (tokens [0]) == suit (tokens [2]) && 
            suit (tokens [0]) == suit (tokens [3]) && 
            suit (tokens [0]) == suit (tokens [4]))
        {
            String outputString = "";
            for (int i = 0; i < tokens.length; i++)
                outputString  += tokens [i] + " ";
            System.out.println (outputString);
            System.exit (0);
        }
        for (int i = 0; i <= 4; i++)
            for (int j= 1; j<= 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 <= 4; y++)
                        for (int x = 1; x <= 4; x++)
                            if ((i != y) && (j != x) && (y != x) && (i != j))
                            {
                                if (suit (tokens [i]) == suit (tokens [x]) &&
                                        suit (tokens [i]) == suit (tokens [y]))
                                {
                                    if ((tokens [i] != tokens [j]) && (tokens [i] != tokens [y]) && (tokens [j] != tokens [y]) &&
                                            (tokens [i] != tokens [x]) && (tokens [j] != tokens [x]) && (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 <= 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 <= 3; i++)
        for (int j= 1; j<= 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 <= 4; y++)
                    for (int x = 1; x <= 4; x++)
                        if ((i != y) && (j != x) && (y != x) && (i != j))
                        {
                            if (ints[i] + 2 == ints[y] && ints[i] + 3 == ints [x])
                            {
                                if ((tokens [i] != tokens [j]) && (tokens [i] != tokens [y]) && (tokens [j] != tokens [y]) &&
                                        (tokens [i] != tokens [x]) && (tokens [j] != tokens [x]) && (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]) && (ints [i]+3 == ints [y]) && (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 <= 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] > ints [1]) marker = 1;
        if (ints [1] > ints [2]) marker = 2;
        if (ints [2] > ints [3]) marker = 3;
        if (ints [3] > ints [4]) marker = 4;
        System.out.println (tokens [marker]);
    }

    static void eblock (String[] tokens) 
    {
        for (int i = 0; i < 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 <= 4; i++)
        test [i] = tokens [i];
        for (int i = 0; i <=4; i++)
        {
        for (int j = 1; j <= 4; j++)
        {
            if (num (test [i]) == num (tokens [j]))
                counter++;
        }
        if (counter > 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);
    }   
}
\$\endgroup\$
  • \$\begingroup\$ shouldn't the answer be golfed? even if there are no competitors, it's still code-golf \$\endgroup\$ – Blazer Feb 12 '12 at 21:18
  • \$\begingroup\$ I made a first step towards golfing: Reduced from about 12k to about 6k. Roll it back if you don't like it. \$\endgroup\$ – user unknown Feb 13 '12 at 8:09
  • \$\begingroup\$ This was my first post. Thanks for editing, did I have it right? How does one get points!?! \$\endgroup\$ – Andrew Quaschnick Feb 14 '12 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.