0

I am writing a program that makes hands of five cards, and tests them against poker combinations. Wikipedia says you have a 1/254 chance in getting a straight, but I get an abysmally small mount of straights in each run. (around 50 in one million hands). Can anyone tell me why that is?

class Card {

    int suit, rank;

    public Card() 
    {
        this.suit = 0;  this.rank = 0;
    }
    public Card(int suit, int rank) 
    {
        this.suit = suit;  this.rank = rank;
    }
    public static void main(String[] args) 
    {
        String[] suits = { "...", "Clubs", "Diamonds", "Hearts", "Spades" };
        String[] ranks = { "...", "Ace", "2", "3", "4", "5", "6","7", "8", "9", "10", "Jack", "Queen", "King"};
        Card[] deck = makeDeck();
        int flushes = 0;
        int twoKind = 0;
        int threeKind = 0;
        int fourKind = 0;
        int straight = 0;
        int straightFlush = 0;
        int fullHouse = 0;
        for(int i = 0; i!=1000000; i++)
        {
            Card[] hand = makeHand(deck);
            if(isFlush(hand)) flushes++;
            if(isThreeKind(hand) == 2) twoKind++;
            else if(isThreeKind(hand) == 3) threeKind++;
            else if(isThreeKind(hand) == 4) fourKind++;
            if(isStraight(hand)) straight++;
            if(isStraight(hand) && isFlush(hand)) straightFlush++;
            if(isFullHouse(hand)) fullHouse++;
        }
        System.out.print(flushes + " flushes \n" + twoKind +" two of a kinds \n"+threeKind+" three of a kinds \n"+fourKind+" four of a kinds \n"+straight+" straights \n"+straightFlush+" straight flushes \n"+fullHouse+" full houses \n\nin 1,000,000 hands");
        /*Card[] hand = new Card[5];
        hand[0] = new Card(1,1);
        hand[1] = new Card(1,1);
        hand[2] = new Card(1,1);
        hand[3] = new Card(2,3);
        hand[4] = new Card(2,3);*/
        //printDeck(hand);      
    }
    public static Card[] makeDeck()
    {
        Card[] cards = new Card[52];
        int index = 0;
        for (int suit = 0; suit <= 3; suit++) 
        {
            for (int rank = 1; rank <= 13; rank++) 
            {
                cards[index] = new Card(suit, rank);
                index++;
            }
        }
        return cards;
    }
    public static void printDeck(Card[] cards)      {   
        for (int i = 0; i < cards.length; i++)
        {
            printCard(cards[i]);
        }
    }
    public static void printCard(Card c)
    {
        String[] suits = { "...", "Clubs", "Diamonds", "Hearts", "Spades" };
        String[] ranks = { "...", "Ace", "2", "3", "4", "5", "6","7", "8", "9", "10", "Jack", "Queen", "King"};
        System.out.println(ranks[c.rank] + " of " + suits[c.suit]);
    }
    public static Card[] makeHand(Card[] deck)
    {
        Card[] hand =  new Card[5];
        int index = 0;
        for(int num = 0; num < hand.length; num++)
        {
            int randRank = (int) (Math.random()*12) +1;
            int randSuit = (int) (Math.random()*4) +1;
            hand[index] = new Card(randSuit, randRank);
            index++;
        }
        return hand;
    }
    public static boolean isFlush(Card[] hand)
    {
        int clubs =0; int diamonds =0; int hearts =0; int spades =0;
        for(int i =0; i< hand.length; i++)
        {
            if(hand[i].suit == 1)
            {
                clubs++;
            }
            if(hand[i].suit == 2)
            {
                diamonds++;
            }
            if(hand[i].suit == 3)
            {
                hearts++;
            }
            if(hand[i].suit == 4)
            {
                spades++;
            }
        }
        if(clubs>4 || diamonds>4 || hearts>4 || spades>4)
        {
            return true;
        }
        else 
        {
            return false;   
        }
    }
    public static int isThreeKind(Card[] hand)
    {

        int count = 1;
        for (int i = 0; i < hand.length; i++) 
        {
            int counter = 1;
            for (int j = 0; j < hand.length; j++) 
            {
                if(hand[i].rank == hand[j].rank)
                {   
                    counter++;
                }
            }   
            if(counter>count)
            {
                count = counter; 
            }   
        }
        if(count == 3) return 2;
        if(count == 4) return 3;
        if(count == 5) return 4;
        if(count == 2) return 1;
        else return 0;
    }
    public static boolean isStraight(Card[] hand)
    {
        int temp1 = hand[0].rank - 1;
        int counter = 0;
        for(int i = 0; i< hand.length; i++)
        {
            int temp2 = hand[i].rank;
            if(temp2 == temp1+1)
            {
                counter++;
            }
            temp1 = temp2;
        }
        if(counter++ == hand.length) return true;
        else return false;
    }
    public static boolean isFullHouse(Card[] hand)
    {
        int count = 0;
        if(isThreeKind(hand) == 3)
        {
            for (int i = 0; i < hand.length; i++) 
            {
                int counter = 1;

                for (int j = 0; j < hand.length; j++) 
                {
                    if(hand[i].rank == hand[j].rank)
                    {   
                        counter++;
                    }
                }
                if(counter == 3)
                {
                    count = 2;
                }
            }
            if(count == 2) return true;
            else
            {
                return false;   
            }
        }
        else 
        {
            return false;
        }
    }

}
1
Contributor
1
Reply
11
Views
4 Years
Discussion Span
Last Post by jspence29
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.