1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Poker Hand Evaluator Java

Discussion in 'Java' started by hanleyhansen, Mar 30, 2009.

  1. hanleyhansen

    hanleyhansen New Member

    Joined:
    Jan 24, 2008
    Messages:
    336
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Drupal Developer/LAMP Developer
    Location:
    Clifton
    Home Page:
    I have another exercise I have to do that I really need help with. My logic is all messed up and I have no idea what to do. Here is the exercise:

    Exercise 12.5 The goal of this exercise is to write a program that generates
    random poker hands and classifies them, so that we can estimate the probability of
    the various poker hands. Don’t worry if you don’t play poker; I’ll tell you everything
    you need to know.
    a. As a warmup, write a program that uses shuffleDeck and subdeck to generate
    and print four random poker hands with five cards each. Did you get anything
    good? Here are the possible poker hands, in increasing order of value:
    pair: two cards with the same rank
    two pair: two pairs of cards with the same rank
    three of a kind: three cards with the same rank
    straight: five cards with ranks in sequence
    flush: five cards with the same suit
    full house: three cards with one rank, two cards with another
    four of a kind: four cards with the same rank
    straight flush: five cards in sequence and with the same suit
    b. Write a method called isFlush that takes a Deck as a parameter and returns a
    boolean indicating whether the hand contains a flush.
    c. Write a method called isThreeKind that takes a hand and returns a boolean
    indicating whether the hand contains Three of a Kind.
    d. Write a loop that generates a few thousand hands and checks whether they
    contain a flush or three of a kind. Estimate the probability of getting one of
    those hands.
    e. Write methods that test for the other poker hands. Some are easier than others.
    You might find it useful to write some general-purpose helper methods that can
    be used for more than one test.
    f. In some poker games, players get seven cards each, and they form a hand with
    the best five of the seven. Modify your program to generate seven-card hands
    and recompute the probabilities.

    And here is my code:
    Code:
    public class Main {
    
        public static void main(String[] args) {
            Deck deck = new Deck ();
            Deck.shuffleDeck (deck);
        }
    
    }
    
    class Card {
    
        int suit, rank;
        int index = 0;
        Card[] deck = new Card [52];
    
        public Card () {
            this.suit = 0; this.rank = 0;
        }
    
        public Card (int suit, int rank) {
            this.suit = suit; this.rank = rank;
        }
    
        public static void printCard (Card c) {
            String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
            String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
            "7", "8", "9", "10", "Jack", "Queen", "King" };
            System.out.println (ranks[c.rank] + " of " + suits[c.suit]);
        }
    }
    
    class Deck {
    
        Card[] cards;
        
        
        public Deck (int n) {
            cards = new Card[n];
        }
    
        public Deck () {
            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++;
                }
            }
        }
    
        public static Deck shuffleDeck(Deck deck){
            Deck hand1 = subDeck (deck, 0, 4);
            Deck hand2 = subDeck (deck, 5, 9);
            Deck hand3 = subDeck (deck, 10, 14);
            Deck hand4 = subDeck (deck, 15, 19);
            Deck pack = subDeck (deck, 20, 51);
            return deck;
        }
        
        public static Deck subDeck (Deck deck, int low, int high) {
            Deck sub = new Deck (high-low+1);
            for (int i = 0; i<sub.cards.length; i++) {
                sub.cards[i] = deck.cards[low+i];
            }
            return sub;
        }
    
        public static void printDeck (Deck hand) {
            for (int i = 0; i < hand.cards.length; i++) {
                Card.printCard (hand.cards[i]);
            }
        }
        
        public static boolean isFlush(Card[] x, int y) {
            int count = 0;
            for(int index = 0; index < y; index++){
                boolean comp = compareIfFlush(x[index], x[index + 1]);
                if (comp = true){
                    count++;
                }
            }
            if (count >= 5){
                System.out.println("Congratulations! You have a flush!");
                return true;
            }
            else{
                System.out.println("Sorry, you do not have a flush.");
                return false;
            }
        }
        
        public static boolean compareIfFlush(Card c1, Card c2){
            if (c1.suit != c2.suit){
                return false;
            }
            return true;
        }
    }
    I don't even know if my isFlush method is working because for seem reason my deck isnt shuffling and dealing a hand and I'm completely stuck. Can someone help me or point me in the right direction or something?
     
    Last edited: Mar 30, 2009
  2. hanleyhansen

    hanleyhansen New Member

    Joined:
    Jan 24, 2008
    Messages:
    336
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Drupal Developer/LAMP Developer
    Location:
    Clifton
    Home Page:
    I did some more thinking and I wrote a new shuffle method and I use subDeck to fill the hand with the already randomized array. However, I'm still having trouble in one part. Here is my code:
    Code:
    public class Poker {
    
        public static void main(String[] args) {
            Deck deck = new Deck ();
            Deck.dealDeck (deck);
        }
    
    }
    class Card {
    
        int suit, rank;
        int index = 0;
        //Card[] deck = new Card [52];
    
        public Card () {
            this.suit = 0; this.rank = 0;
        }
    
        public Card (int suit, int rank) {
            this.suit = suit; this.rank = rank;
        }
    
        public static void printCard (Card c) {
            String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
            String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
            "7", "8", "9", "10", "Jack", "Queen", "King" };
            System.out.println (ranks[c.rank] + " of " + suits[c.suit]);
        }
    }
    
    class Deck {
    
        Card[] cards;
        
        
        public Deck (int n) {
            cards = new Card[n];
        }
    
        public Deck () {
            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++;
                }
            }
        }
    
        public static Deck dealDeck(Deck deck){
            shuffle(deck);
            Deck hand1 = subDeck(deck, 0, 4);
            Deck hand2 = subDeck(deck, 5, 9);
            Deck hand3 = subDeck(deck, 10, 14);
            Deck hand4 = subDeck(deck, 15, 19);
            Deck pack = subDeck(deck, 20, 51);
            return deck;
        }
    
        public static Deck shuffle(Deck deck){
            for (int i = 0; i < 52; i++){
                int rand = (int)(Math.random()*(i + 1));
                [B]Deck[] temp = deck[i];
                deck[i] = deck[rand];
                deck[rand] = deck[temp];[/B]
            }
            return deck;
        }
        
        public static Deck subDeck(Deck deck, int low, int high) {
            Deck sub = new Deck (high-low+1);
            for (int i = 0; i < sub.cards.length; i++) {
                sub.cards[i] = deck.cards[low+i];
            }
            return sub;
        }
        
        public static void printDeck (Deck hand) {
            for (int i = 0; i < hand.cards.length; i++) {
                Card.printCard (hand.cards[i]);
            }
        }
        
        public static boolean isFlush(Card[] x, int y) {
            int count = 0;
            for(int index = 0; index < y; index++){
                boolean comp = compareIfFlush(x[index], x[index + 1]);
                if (comp = true){
                    count++;
                }
            }
            if (count >= 5){
                System.out.println("Congratulations! You have a flush!");
                return true;
            }
            else{
                System.out.println("Sorry, you do not have a flush.");
                return false;
            }
        }
        
        public static boolean compareIfFlush(Card c1, Card c2){
            if (c1.suit != c2.suit){
                return false;
            }
            return true;
        }
    }
    I put in bold the part I'm having trouble with. I get the error: "array required, but exercise125poker.Deck found". I need a work around for this error because I don't know how to fix it so I'm stuck. Can anyone help?
     
    Last edited by a moderator: Mar 31, 2009

Share This Page