Tag Archives: poker

Euler Problem 54

Loading the data is pretty simple, even parsing it. But once I tried solving for all possible outcomes, what a mess! So in keeping with the principles of Java, I searched for a pre-made class that can evaluate a hand.

I found this one: http://spaz.ca/poker/

And it works. Too well. Got the answer on the first try. Still, it was fun instantiating everything.

    import java.util.*;
    import java.io.*;

    public class Euler54
    {
        public static void main(String[] args)
        {
            Euler54 e = new Euler54();
            System.out.format(“Problem: %s\n”, e.getClass().getName());
            System.out.format(“Answer = %s\n”, e.Problem());
        }
        public String Problem ()
        {
            ArrayList P1 = new ArrayList();
            ArrayList P2 = new ArrayList();
            ArrayList list = new ArrayList();
            String Faces [] = {“2″,”3″,”4″,”5″,”6″,”7″,”8″,”9″,”T”,”J”,”Q”,”K”,”A”};
            int win = 0;
            try
            {
                String line = “”;
                FileInputStream stream = new FileInputStream(“F:/Development/Java/EULER/src/euler/poker.txt”);
                DataInputStream in = new DataInputStream(stream);
                BufferedReader buffer = new BufferedReader(new InputStreamReader(in));
                while ((line = buffer.readLine()) != null)
                {
                    list.add(line);
                }
                in.close();
            }
            catch (Exception e)
            {
                System.out.format(“%s\n”, e);
            }
            for (String l: list)
            {
                String [] cardcodes = l.split(” “);
                ArrayList Card = new ArrayList();
                for (int i = 0; i <= 9; i++)
                {
                    String cardValue = String.valueOf(cardcodes[i].charAt(0));
                    String cardSuit = String.valueOf(cardcodes[i].charAt(1));
                    int rank = 0, suit = 0;
                    for (int f = 0; f <= 12; f++)
                    {
                        if (cardValue.equals(Faces[f]))
                        {
                            rank = f;
                            break;
                        }
                    }
                    if (cardSuit.equals(“C”))
                        suit = 0;
                    else if (cardSuit.equals(“D”))
                        suit = 1;
                    else if (cardSuit.equals(“H”))
                        suit = 2;
                    else if (cardSuit.equals(“S”))
                        suit = 3;
                    poker.Card c = new poker.Card();
                    c.setCard(rank, suit);
                    Card.add(c);
                }
                poker.Hand hand1 = new poker.Hand();
                poker.Hand hand2 = new poker.Hand();
                for (int i = 0; i <= 4; i++)
                    hand1.addCard(Card.get(i));
                for (int i = 5; i <= 9; i++)
                    hand2.addCard(Card.get(i));
                P1.add(hand1);
                P2.add(hand2);
            }
            for (int i = 0; i <= 999; i++)
            {
                poker.Hand hand1 = new poker.Hand();
                poker.Hand hand2 = new poker.Hand();
                hand1 = P1.get(i);
                hand2 = P2.get(i);
                System.out.format(“Hand1:%s Hand2:%s\n”, hand1, hand2);
                poker.HandEvaluator Eval = new poker.HandEvaluator();
                if (Eval.compareHands(hand1, hand2) == 1)
                    win++;
            }
            return String.valueOf(win);
        }   
    }