Card.java

package poker;

public class Card {
    int suit;
    int rank;

    public Card () {
        suit = 0;
        rank = 0;
    }

    public Card (int s, int r) {
        suit = s;
        rank = r;
    }

    public int getSuit () {
        return suit;
    }

    public int getRank () {
        return rank;
    }

    public static int handScore (Card[] card) {
        int total = 0;
        for (int i = 0; i < card.length; i++) {
            int score = card[i].getRank ();
            if (score > 10)
                score = 10;
            total += score;
            }
        return total;
        }

    public static int StringToSuit (String str) {
        if (str.equals ("Clubs"))
            return 0;
        if (str.equals ("Diamonds"))
            return 1;
        if (str.equals ("Hearts"))
            return 2;
        if (str.equals ("Spades"))
            return 3;
        else
            return -99;
    }

    public static int StringToRank (String str) {
        int result = -99;
        try {
            result = Integer.parseInt (str);
            if (result >= 2 && result <= 10)
                return result;
            else
                return -99;
        } catch (NumberFormatException e) {
            str = str.toLowerCase ();
            if (str.equals ("ace"))
                return 1;
            if (str.equals ("jack"))
                return 11;
            if (str.equals ("queen"))
                return 12;
            if (str.equals ("king"))
                return 13;
            else
                return -99;
        }
    }

    public static Card parseCard (String str) {
        String[] token = str.split (" of ");
        int rank = StringToRank (token[0]);
        int suit = StringToSuit (token[1]);
        System.out.println (suit + " " + rank);
        if (rank == -99 || suit == -99)
            return null;
        else
            return new Card (suit, rank);
    }

    public static int[] suitHist (Card[] card) {
        int[] hist = new int[4];
        for (int i = 0; i < card.length; i++)
            hist[card[i].getSuit()]++;
        return hist;
    }

    public int compareCard (Card card) {
        int rank = card.getRank ();
        int suit = card.getSuit ();
        if (rank == this.rank) {
            if (suit == this.suit) {
                return 0;
            } else {
                if (suit < this.suit) {
                    return 1;
                } else {
                    return -1;
                }
            }
            } else {
                if (rank < this.rank) {
                    return 1;
                } else {
                    return -1;
                }
        }
    }
}

Deck.java

package poker;

public 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 Card[] buildDeck () {
        Card deck[] = new Card[52];
        int index = 0;
        for (int suit = 0; suit <= 3; suit++) {
            for (int rank = 1; rank <= 13; rank++) {
                deck[index] = new Card (suit, rank);
                index++;
            }
        }
        return deck;
    }

    public static boolean isFlush (Card[] card) {
        int[] hist = Card.suitHist (card);
        for (int i = 0; i < hist.length; i++) {
            if (hist[i] >= 5)
                return true;
            else
                continue;
        }
        return false;
    }

    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 swapCards (Card[] c, int i, int j) {
        Card temp = c[i];
        c[i] = c[j];
        c[j] = temp;
    }

    public static void shuffleDeck (Card[] c) {
        for (int i = 0; i < c.length; i++) {
            int random = i + (int) (Math.random () * (c.length - 1));
            if (random <= 51 && random >= 0)
                swapCards (c, i, random);
        }
    }

    public static void shuffleDeck (Deck d) {
        for (int i = 0; i < d.cards.length; i++) {
            int random = i + (int) (Math.random () * (d.cards.length - 1));
            if (random <= 51 && random >= 0)
                swapCards (d.cards, i, random);
        }
    }

    public static int findLowestCard (Card[] c, int low, int high) {
        //if (low < 0 || high >= c.length
        //return -99;
        int min = low;
        for (int i = low + 1; i <= high; i++) {
            if (c[min].compareCard (c[i]) == 1)
                min = i;
        }
        return min;
    }

    public static void sort (Card[] c) {
        for (int i = 0; i < c.length; i++) {
            int swap = findLowestCard (c, i, c.length - 1);
            if (swap != -99)
                swapCards (c, i, swap);
        }
    }

    public static void incrementalShuffle (Deck d, Card c) {
        int random = 0 + (int) (Math.random () * (d.cards.length - 1));
        d.cards[random] = c;
    }

    public static Deck merge (Deck d1, Deck d2) {
        Deck result = new Deck (d1.cards.length + d2.cards.length);
        int i = 0;
        int j = 0;
        for (int k = 0; k < result.cards.length; k++) {
            //d1 empty
            if (i == d1.cards.length) {
                result.cards[k] = d2.cards[j];
                j++;
            //d2 empty
            } else if (j == d2.cards.length) {
                result.cards[k] = d1.cards[i];
                i++;
            } else if (d1.cards[i].compareCard (d2.cards[j]) == -1) {
                result.cards[k] = d1.cards[i];
                i++;
            } else {
                result.cards[k] = d2.cards[j];
                j++;
            }
        }
        return result;
    }

    public static Deck mergeSort1 (Deck deck) {
        int mid = (deck.cards.length - 1) / 2;

        Deck sub1 = subdeck (deck, 0, mid);
        Deck sub2 = subdeck (deck, mid + 1, deck.cards.length - 1);
        sort (sub1.cards);
        sort (sub2.cards);
        Deck d = merge (sub1, sub2);
        return d;
    }

    public static Deck mergeSort2 (Deck deck) {
        if (deck.cards.length <= 1)
            return deck;
        int mid = (deck.cards.length - 1) / 2;

        Deck sub1 = subdeck (deck, 0, mid);
        Deck sub2 = subdeck (deck, mid + 1, deck.cards.length - 1);
        sub1 = mergeSort2 (sub1);
        sub2 = mergeSort2 (sub2);
        Deck d = merge (sub1, sub2);
        return d;
    }

    public Deck[] getHands () {
        Deck d = new Deck ();
        Deck.shuffleDeck (d);
        Deck[] arr = new Deck[4];
        arr[0] = Deck.subdeck (d, 0, 4);
        arr[1] = Deck.subdeck (d, 5, 9);
        arr[2] = Deck.subdeck (d, 10, 14);
        arr[3] = Deck.subdeck (d, 15, 19);
        return arr;
    }

    public static boolean isFlush (Deck d) {
        return isFlush (d.cards);
    }

    public static boolean isThreeKind (Deck d) {
        Card[] cards = d.cards;
        int[] counter = new int[13];
        for (int i = 0; i < cards.length - 1; i++) {
            counter[cards[i].getRank ()]++;
        }
        for (int i = 0; i < counter.length; i++) {
            if (counter[i] == 3)
                return true;
            else
                continue;
        }
        return false;
    }

    public static boolean isFourKind (Deck d) {
        Card[] cards = d.cards;
        int[] counter = new int[13];
        for (int i = 0; i < cards.length - 1; i++) {
            counter[cards[i].getRank ()]++;
        }
        for (int i = 0; i < counter.length; i++) {
            if (counter[i] == 4)
                return true;
            else
                continue;
        }
        return false;
    }

    public static boolean isPair (Deck d) {
        Card[] cards = d.cards;
        int[] counter = new int[13];
        for (int i = 0; i < cards.length - 1; i++) {
            counter[cards[i].getRank ()]++;
        }
        for (int i = 0; i < counter.length; i++) {
            if (counter[i] == 2)
                return true;
            else
                continue;
        }
        return false;
    }

    public static boolean isTwoPair (Deck d) {
        Card[] cards = d.cards;
        int[] counter = new int[13];
        for (int i = 0; i < cards.length - 1; i++) {
            counter[cards[i].getRank ()]++;
        }
        int pair = 0;
        for (int i = 0; i < counter.length; i++) {
            if (pair == 2)
                return true;
            if (counter[i] == 2)
                pair++;
            else continue;
        }
        return false;
    }

    public static boolean isStraight (Deck d) {
        Card[] cards = d.cards;
        Card[] temp = new Card[cards.length];
        for (int i = 0; i < cards.length; i++)
            temp[i] = cards[i];
        for (int i = 0; i < temp.length; i++) {
            int index = getLowestCard (temp, i, temp.length - 1);
            Deck.swapCards (temp, i, index);
            if (i >= 1) {
                if (temp[i].getRank() != temp[i-1].getRank () + 1)
                    return false;
            }
        }
        return true;
        }

    public static boolean isStraightFlush () {
        if (isStraight () == true && isFlush () == true) {
            return true;
        }
        return false;
    }

    public static boolean isFullHouse (Deck d) {
        Card[] cards = d.cards;
        int[] counter = new int[13];
        for (int i = 0; i < cards.length - 1; i++) {
            counter[cards[i].getRank ()]++;
        }
        //return true if three and two are true
        boolean three = false;
        boolean two = false;
        for (int i = 0; i < counter.length; i++) {
            if (three && two)
                return true;
            if (counter[i] == 3)
                three = true;
            if (counter[i] == 2)
                two = true;
            else
                continue;
        }
        return false;
    }

    public static int getLowestCard (Card[] c, int low, int high) {
        int min = low;
        for (int i = low + 1; i <= high; i++) {
            if (c[i].getRank () < c[min].getRank ())
                min = i;
        }
        return min;
    }

    public static void printBestHand (Deck hand) {
        //printDeck (hand);
        if (isStraightFlush ()) {
            System.out.println ("is Straight Flush");
        } else
            if (isStraight (hand)) {
                System.out.println ("is Straight");
        } else
            if (isFlush (hand)) {
                System.out.println ("is Flush");
            } else
                if (isFourKind (hand)) {
                    System.out.println ("is Four of Kind");
                } else
                    if (isFullHouse (hand)) {
                        System.out.println ("is Full House");
                    } else
                        if (isTwoPair (hand)) {
                            System.out.println ("is Two Pair");
                        } else
                            if (isThreeKind (hand)) {
                                System.out.println ("is Three of Kind");
                            } else
                                if (isPair (hand)) {
                                    System.out.println ("is Pair");
                                } else
                                    System.out.println ("is Worthless");
    }
}

Main.java

package poker;

public class Main {

    public static void main(String[] args) {

        Deck unShuffledDeck = new Deck ();
        
        Deck hand = new Deck (5);
        hand.cards[0] = unShuffledDeck.cards[14];
        hand.cards[1] = unShuffledDeck.cards[15];
        hand.cards[2] = unShuffledDeck.cards[28];
        hand.cards[3] = unShuffledDeck.cards[35];
        hand.cards[4] = unShuffledDeck.cards[43];
        System.out.print ("Pair Test: ");
        
        Deck.printBestHand (hand);
        System.out.println ();
        
        hand.cards[0] = unShuffledDeck.cards[14];
        hand.cards[1] = unShuffledDeck.cards[15];
        hand.cards[2] = unShuffledDeck.cards[28];
        hand.cards[3] = unShuffledDeck.cards[35];
        hand.cards[4] = unShuffledDeck.cards[43];
        System.out.print ("Two Pair Test: ");
        Deck.printBestHand (hand);

        Deck d = new Deck (5);
        Card[] c = new Card[5];
        c[0] = new Card (3, 5);
        c[1] = new Card (0, 5);
        c[2] = new Card (0, 4);
        c[3] = new Card (4, 4);
        c[4] = new Card (1, 5);
        d.cards = c;
        Deck d1 = Deck.mergeSort2 (d);
        for (int i = 0; i < d1.cards.length; i++) {
            System.out.println (d1.cards[i].getSuit () + " " + d1.cards[i].getRank());
        }

        for (int x = 0; x < 10; x++) {
        Deck.shuffleDeck (hand);
        Deck sub = Deck.subdeck (hand, 0, 4);
        Deck.printBestHand (sub);
    }
    }

}

So far I have this code. How to fix the following:

(a) Error lines in the method isStraightFlush.
(b) How to add printCard and printDeck to my classes. What should I write in the method?
(c) shuffleDeck crashes if the deck has less than 52 cards. What shouild I do here?
(d) In several methods I have this code which crashes sometimes. How do I fix it?

for (int i = 0; i < cards.length - 1; i++) {
counter[cards.getRank ()]++;
}


PLEASE HELP :)

So far I have this code. How to fix the following:

(a) Error lines in the method isStraightFlush.
(b) How to add printCard and printDeck to my classes. What should I write in the method?
(c) shuffleDeck crashes if the deck has less than 52 cards. What shouild I do here?
(d) In several methods I have this code which crashes sometimes. How do I fix it?

for (int i = 0; i < cards.length - 1; i++) {
counter[cards.getRank ()]++;
}

a) Read the error messages they are pretty clear. You are not calling the methods correctly. Look how you have declared them and call them like that.

b) Do whatever you want. Usually you print their attributes.

public void printCard() {
  System.out.println("Card: "+suit+" "+rank);
}

With printDeck iterate the array and print each of the cards attributes.

d) Array go from 0 to length-1. So this would be correct: for (int i = 0; i <= cards.length - 1; i++) OR better and more commonly used: for (int i = 0; i < cards.length; i++) Also counter is an array with length 13 -> [0-12]. So the getRank: counter[cards[i].getRank ()] should return something between 0 and 12 not 1 and 13.

c) Try to print the values you are passing as parameters and see what happens. Maybe the random number is for some random cases 52 instead of 51.

Also if you are using:
int random = i + (int) (Math.random () * (c.length - 1))
Why do you have this:
random <= 51 && random >= 0

public static void shuffleDeck (Card[] c) {
  for (int i = 0; i < c.length; i++) {
    int random = i + (int) (Math.random () * (c.length - 1));
[B]    System.out.println("Random: "+random+". I: "+i+". Length: "+c.length);[/B]
    if (random < c.length && random >= 0)
        swapCards (c, i, random);
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.