Comparing cards in a poker game?

OK, so I was here before to ask about help for a Poker program. I got it finished thanks in large part to the people here, so I would like to say thank you very much. Now I'm furthering it by editing the Hand class to display the hand ranks (before it just displayed the hands).
I've easily edited toString to display the rankings, now its just a matter of figuring out how to compare the cards.
Basically, what I need to know is what would be the "best" why to compare the cards. Obviously this needs to be done by getting the card number and, for some rankings, the suit. I'm not sure of exactly how to go about this. If someone could maybe post code for just a pair (only comparing the first 2 cards) I'm sure I could figure out the rest of the rankings. If seeing my code would help (there is Card.java, Deck.java, and Hand.java, the one I'm editing) I will gladly post it. Thank you so much if anyone takes the time to help.

this is probably way overkill for you right now, but here it is anyway. this analyzes a 7 card hand (in texas hold'em style poker). but you can see it really only tests 5 cards at a time for the best hand. hopefully it'll get you started.... i have the source for the rest of my classes too if you'd like to see it....
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
* Created on Feb 22, 2005
* Notes:
* @author MCKNIGDM
public class Hand {
    private static final int ASC  = 1;
    private static final int DESC = 2;
    private Card hole1;
    private Card hole2;
    private Card flop1;
    private Card flop2;
    private Card flop3;
    private Card turn;
    private Card river;
    ArrayList cards;
    Ranking ranking;
    public Hand() {
    public Hand(Card hole1, Card hole2) {
        this.hole1 = hole1;
        this.hole2 = hole2;
    public void setFlop(Card flop1, Card flop2, Card flop3) {
        this.flop1 = flop1;
        this.flop2 = flop2;
        this.flop3 = flop3;
    public void setTurn(Card turn) {
        this.turn = turn;
    public void setRiver(Card river) {
        this.river = river;
    public void rankHand() {
        placeCardsIntoArray();
        boolean suited      = false;
        int suit          = -1;
        // check for at least 5 cards of the same suit
        int count           = 0;
        Iterator itr      = null;
        if(cards.size() >= 5) {
             for(int i=0; i<4; i++) {
                 count      = 0;
                 itr      = cards.iterator();
                 while(itr.hasNext()) { if(((Card) itr.next()).getSuit() == i) count++; }
                 if(count >= 5) {
                     suited = true;
                     suit   = i;
                     break;
        // if suited, the best hands possible are:
        //      - royal flush
        //      - straight flush
        //      - flush
        // so check for those only (the hand can't be any worse than a flush)
        if(suited) {
            // first, remove any unsuited cards from the hand
            for(int i=0; i<cards.size(); i++) {
                if(((Card) cards.get(i)).getSuit() != suit) {
                    cards.remove(i);
                    i--;
            // now order the hand ASC - place the highest cards first
            orderArray(ASC);
            // look for a royal/straight flush
            int start = 0;
            int end = 4;
            do {
                int rank = ((Card) cards.get(start)).getRank();
                boolean straightFlush = false;
                for(int i=start+1; i<=end; i++) {
                    if(((Card) cards.get(i)).getRank() == rank-1) {
                        rank--;
                    } else {
                        break;
                    if(i == end) straightFlush = true;
                if(straightFlush) {
                    // it's either a royal or straight flush
                    ranking = new Ranking((((Card) cards.get(start)).getRank() == 14 ? Ranking.ROYAL_FLUSH : Ranking.STRAIGHT_FLUSH),
                                            (Card) cards.get(start), (Card) cards.get(start+1), (Card) cards.get(start+2), (Card) cards.get(start+3), (Card) cards.get(start+4));
                    return;
                start++;
                end++;
            } while(end < cards.size());
            // check for a low straight (A-2-3-4-5)
            boolean hasAce = ((Card) cards.get(0)).getRank() == Utils.ACE;
            boolean hasTwo = ((Card) cards.get(cards.size()-1)).getRank() == Utils.TWO;
            if(hasAce && hasTwo) {
                boolean hasThree = false;
                boolean hasFour = false;
                boolean hasFive = false;
                int threeIndex = -1;
                int fourIndex = -1;
                int fiveIndex = -1;
                for(int k=1; k<cards.size()-1; k++) {
                    if(((Card) cards.get(k)).getRank() == Utils.THREE) {
                        hasThree = true; threeIndex = k;
                    } else if(((Card) cards.get(k)).getRank() == Utils.FOUR) {
                        hasFour = true; fourIndex = k;
                    } else if(((Card) cards.get(k)).getRank() == Utils.FIVE) {
                        hasFive = true; fiveIndex = k;
                if(hasAce && hasTwo && hasThree && hasFour && hasFive) {
                    ranking = new Ranking(Ranking.STRAIGHT_FLUSH, (Card) cards.get(fiveIndex), (Card) cards.get(fourIndex), (Card) cards.get(threeIndex), (Card) cards.get(cards.size()-1), (Card) cards.get(0));
                    return;
            // at this point, the hand must be a flush
            // since we have all the suited cards and they are in order, just take the first 5
            ranking = new Ranking(Ranking.FLUSH, (Card) cards.get(0), (Card) cards.get(1), (Card) cards.get(2), (Card) cards.get(3), (Card) cards.get(4));
            return;
        } // end of the suited-only rankings
        // check for four of a kind
        if(checkFourKind()) return;
        // check for full house
        if(checkFullHouse()) return;
        // check for straight
        if(checkStraight()) return;
        // check for three of a kind
        if(checkThreeKind()) return;
        // check for two pair
        if(checkTwoPair()) return;
        // check for a pair
        if(checkPair()) return;
        // check for high card
        checkHighCard();
    private boolean checkHighCard() {
        Card card1 = null;
        Card card2 = null;
        Card card3 = null;
        Card card4 = null;
        Card card5 = null;
        for(int j=0; j<cards.size(); j++) {
            if(card1 == null) {
                card1 = ((Card) cards.get(j)); continue;
            } else {
                if(card1.getRank() < ((Card) cards.get(j)).getRank()) {
                    card1 = ((Card) cards.get(j)); continue;
            if(card2 == null) {
                card2 = ((Card) cards.get(j)); continue;
            } else {
                if(card2.getRank() < ((Card) cards.get(j)).getRank()) {
                    card2 = ((Card) cards.get(j)); continue;
            if(card3 == null) {
                card3 = ((Card) cards.get(j)); continue;
            } else {
                if(card3.getRank() < ((Card) cards.get(j)).getRank()) {
                    card3 = ((Card) cards.get(j)); continue;
            if(card4 == null) {
                card4 = ((Card) cards.get(j)); continue;
            } else {
                if(card4.getRank() < ((Card) cards.get(j)).getRank()) {
                    card4 = ((Card) cards.get(j)); continue;
            if(card5 == null) {
                card5 = ((Card) cards.get(j)); continue;
            } else {
                if(card5.getRank() < ((Card) cards.get(j)).getRank()) {
                    card5 = ((Card) cards.get(j)); continue;
        ranking = new Ranking(Ranking.HIGH_CARD, card1, card2, card3, card4, card5);
        return true;
    private boolean checkPair() {
        int pairRank   = -1;
        int count;
        // look for the largest pair
        for(int i=Utils.TWO; i<=Utils.ACE; i++) {
            count = 0;
            for(int j=0; j<cards.size(); j++) {
                if(((Card) cards.get(j)).getRank() == i) {
                    count++;
            if(count == 2) {
                pairRank = i;
        if(pairRank == -1) return false;
        // we have a pair -- get the pair and the 3 kickers
        Card card1 = null;
        Card card2 = null;
        Card card3 = null;
        Card card4 = null;
        Card card5 = null;
        for(int j=0; j<cards.size(); j++) {
            if(((Card) cards.get(j)).getRank() == pairRank) {
                if     (card1 == null) card1 = (Card) cards.get(j);
                else if(card2 == null) card2 = (Card) cards.get(j);
                continue;
            if(((Card) cards.get(j)).getRank() != pairRank) {
                if(card3 == null) {
                    card3 = ((Card) cards.get(j)); continue;
                } else {
                    if(card3.getRank() < ((Card) cards.get(j)).getRank()) {
                        card3 = ((Card) cards.get(j)); continue;
                if(card4 == null) {
                    card4 = ((Card) cards.get(j)); continue;
                } else {
                    if(card4.getRank() < ((Card) cards.get(j)).getRank()) {
                        card4 = ((Card) cards.get(j)); continue;
                if(card5 == null) {
                    card5 = ((Card) cards.get(j)); continue;
                } else {
                    if(card5.getRank() < ((Card) cards.get(j)).getRank()) {
                        card5 = ((Card) cards.get(j)); continue;
        ranking = new Ranking(Ranking.PAIR, card1, card2, card3, card4, card5);
        return true;
    private boolean checkTwoPair() {
        int pair1Rank   = -1;
        int pair2Rank   = -1;
        int count;
        // look for the largest pair
        for(int i=Utils.TWO; i<=Utils.ACE; i++) {
            count = 0;
            for(int j=0; j<cards.size(); j++) {
                if(((Card) cards.get(j)).getRank() == i) {
                    count++;
            if(count == 2) {
                pair1Rank = i;
        if(pair1Rank == -1) return false;
        // look for the largest pair
        for(int i=Utils.TWO; i<=Utils.ACE; i++) {
            if(i == pair1Rank) continue;
            count = 0;
            for(int j=0; j<cards.size(); j++) {
                if(((Card) cards.get(j)).getRank() == i) {
                    count++;
            if(count == 2) {
                pair2Rank = i;
        if(pair2Rank == -1) return false;
        // we have two pair -- get the pairs and the highest kicker
        Card card1 = null;
        Card card2 = null;
        Card card3 = null;
        Card card4 = null;
        Card card5 = null;
        for(int j=0; j<cards.size(); j++) {
            if(((Card) cards.get(j)).getRank() == pair1Rank) {
                if     (card1 == null) card1 = (Card) cards.get(j);
                else if(card2 == null) card2 = (Card) cards.get(j);
                continue;
            if(((Card) cards.get(j)).getRank() == pair2Rank) {
                if     (card3 == null) card3 = (Card) cards.get(j);
                else if(card4 == null) card4 = (Card) cards.get(j);
                continue;
            if(((Card) cards.get(j)).getRank() != pair1Rank && ((Card) cards.get(j)).getRank() != pair2Rank) {
                if(card5 == null) {
                    card5 = ((Card) cards.get(j));
                } else {
                    if(card5.getRank() < ((Card) cards.get(j)).getRank()) {
                        card5 = ((Card) cards.get(j));
        ranking = new Ranking(Ranking.TWO_PAIR, card1, card2, card3, card4, card5);
        return true;
    private boolean checkStraight() {
        orderArray(ASC);
        int start = 0;
        int end = 4;
        do {
            int rank = ((Card) cards.get(start)).getRank();
            boolean straight = false;
            for(int i=start+1; i<=end; i++) {
                if(((Card) cards.get(i)).getRank() == rank-1) {
                    rank--;
                } else {
                    break;
                if(i == end) straight = true;
            if(straight) {
                ranking = new Ranking(Ranking.STRAIGHT, (Card) cards.get(start), (Card) cards.get(start+1), (Card) cards.get(start+2), (Card) cards.get(start+3), (Card) cards.get(start+4));
                return true;
            start++;
            end++;
        } while(end < cards.size());
        // check for a low straight (A-2-3-4-5)
        boolean hasAce = ((Card) cards.get(0)).getRank() == Utils.ACE;
        boolean hasTwo = ((Card) cards.get(6)).getRank() == Utils.TWO;
        if(hasAce && hasTwo) {
            boolean hasThree = false;
            boolean hasFour = false;
            boolean hasFive = false;
            int threeIndex = -1;
            int fourIndex = -1;
            int fiveIndex = -1;
            for(int k=1; k<cards.size()-1; k++) {
                if(((Card) cards.get(k)).getRank() == Utils.THREE) {
                    hasThree = true; threeIndex = k;
                } else if(((Card) cards.get(k)).getRank() == Utils.FOUR) {
                    hasFour = true; fourIndex = k;
                } else if(((Card) cards.get(k)).getRank() == Utils.FIVE) {
                    hasFive = true; fiveIndex = k;
            if(hasAce && hasTwo && hasThree && hasFour && hasFive) {
                ranking = new Ranking(Ranking.STRAIGHT, (Card) cards.get(fiveIndex), (Card) cards.get(fourIndex), (Card) cards.get(threeIndex), (Card) cards.get(6), (Card) cards.get(0));
                return true;
        return false;
    private boolean checkFourKind() {
        orderArray(ASC);
        int start = 0;
        int end = 4;
        do {
            int rank = ((Card) cards.get(start)).getRank();
            boolean fourKind = false;
            for(int i=start+1; i<=end-1; i++) {
                if(((Card) cards.get(i)).getRank() == rank) {
                } else {
                    break;
                if(i == (end-1)) fourKind = true;
            if(fourKind) {
                ranking = new Ranking(Ranking.FOUR_KIND, (Card) cards.get(start), (Card) cards.get(start+1), (Card) cards.get(start+2), (Card) cards.get(start+3), (Card) cards.get((start == 0 ? 4 : 0)));
                return true;
            start++;
            end++;
        } while(end < cards.size()+1);
        return false;
    private boolean checkThreeKind() {
        int setRank   = -1;
        int count;
        // look for the largest set
        for(int i=Utils.TWO; i<=Utils.ACE; i++) {
            count = 0;
            for(int j=0; j<cards.size(); j++) {
                if(((Card) cards.get(j)).getRank() == i) {
                    count++;
            if(count == 3) {
                setRank = i;
        if(setRank == -1) return false;
        // we have a set -- get the 5 cards for the ranking
        Card card1 = null;
        Card card2 = null;
        Card card3 = null;
        Card card4 = null;
        Card card5 = null;
        for(int j=0; j<cards.size(); j++) {
            if(((Card) cards.get(j)).getRank() == setRank) {
                if     (card1 == null) card1 = (Card) cards.get(j);
                else if(card2 == null) card2 = (Card) cards.get(j);
                else if(card3 == null) card3 = (Card) cards.get(j);
            if(((Card) cards.get(j)).getRank() != setRank) {
                if(card4 == null) {
                    card4 = (Card) cards.get(j);
                    continue;
                } else {
                    if(card4.getRank() < ((Card) cards.get(j)).getRank()) {
                        card4 = ((Card) cards.get(j));
                        continue;
                if(card5 == null) {
                    card5 = (Card) cards.get(j);
                } else {
                    if(card5.getRank() < ((Card) cards.get(j)).getRank()) {
                        card5 = ((Card) cards.get(j));
        ranking = new Ranking(Ranking.THREE_KIND, card1, card2, card3, card4, card5);
        return true;     
    private boolean checkFullHouse() {
        int setRank   = -1;
        int pairRank   = -1;
        int count;
        // look for the largest set
        for(int i=Utils.TWO; i<=Utils.ACE; i++) {
            count = 0;
            for(int j=0; j<cards.size(); j++) {
                if(((Card) cards.get(j)).getRank() == i) {
                    count++;
            if(count == 3) {
                setRank = i;
        if(setRank == -1) return false;
        // look for the largest pair
        for(int i=Utils.TWO; i<=Utils.ACE; i++) {
            if(i == setRank) continue;
            count = 0;
            for(int j=0; j<cards.size(); j++) {
                if(((Card) cards.get(j)).getRank() == i) {
                    count++;
            if(count >= 2) {
                pairRank = i;
        if(pairRank == -1) return false;
        // we have a full house -- get the 5 cards for the ranking
        Card card1 = null;
        Card card2 = null;
        Card card3 = null;
        Card card4 = null;
        Card card5 = null;
        for(int j=0; j<cards.size(); j++) {
            if(((Card) cards.get(j)).getRank() == setRank) {
                if     (card1 == null) card1 = (Card) cards.get(j);
                else if(card2 == null) card2 = (Card) cards.get(j);
                else if(card3 == null) card3 = (Card) cards.get(j);
            if(((Card) cards.get(j)).getRank() == pairRank) {
                if     (card4 == null) card4 = (Card) cards.get(j);
                else if(card5 == null) card5 = (Card) cards.get(j);
        ranking = new Ranking(Ranking.FULL_HOUSE, card1, card2, card3, card4, card5);
        return true;
    public void placeCardsIntoArray() {
        cards = new ArrayList();
        if(hole1 != null) cards.add(hole1);
        if(hole2 != null) cards.add(hole2);
        if(flop1 != null) cards.add(flop1);
        if(flop2 != null) cards.add(flop2);
        if(flop3 != null) cards.add(flop3);
        if(turn  != null) cards.add(turn);
        if(river != null) cards.add(river);
    public void orderArray(int order) {
        TreeSet set = new TreeSet(new Comparator() {
            public int compare(Object obj1, Object obj2) {
                Card c1 = (Card) obj1;
                Card c2 = (Card) obj2;
                if(c1.getRank() > c2.getRank()) {
                    return -1;
                } else if(c1.getRank() <= c2.getRank()) {
                    return 1;
                } else {
                    return 0;
        // add the cards to the tree set and remove from array
        while(cards.size() >= 1) {
            set.add(cards.get(0));
            cards.remove(0);
        // put the cards from the set into the array
        cards.addAll(set);
        if(order == DESC) {
            Collections.reverse(cards);
    public void printHand() {
        placeCardsIntoArray();
        orderArray(ASC);
        Iterator itr = cards.iterator();
        while(itr.hasNext()) {
            Card c = ((Card) itr.next());
            if(c.getRank() == hole1.getRank() && c.getSuit() == hole1.getSuit()) {
                System.out.println(c.getName() + " (h1)");
            } else if(c.getRank() == hole2.getRank() && c.getSuit() == hole2.getSuit()) {
                System.out.println(c.getName() + " (h2)");
            } else {
                System.out.println(c.getName());
    public void printRanking() {
        if(ranking == null) System.out.println("Null ranking...");
        else ranking.printRanking();
    public Card getFlop1() {
        return flop1;
    public void setFlop1(Card flop1) {
        this.flop1 = flop1;
    public Card getFlop2() {
        return flop2;
    public void setFlop2(Card flop2) {
        this.flop2 = flop2;
    public Card getFlop3() {
        return flop3;
    public void setFlop3(Card flop3) {
        this.flop3 = flop3;
    public Card getHole1() {
        return hole1;
    public void setHole1(Card hole1) {
        this.hole1 = hole1;
    public Card getHole2() {
        return hole2;
    public void setHole2(Card hole2) {
        this.hole2 = hole2;
    public Card getRiver() {
        return river;
    public Card getTurn() {
        return turn;
    public Ranking getRanking() {
        return ranking;
}

Similar Messages

  • Poker game logic

    has anybody ever tried to make a poker game
    if you have, please help me.
    if you have not, please help me if you can.
    how would you check weather there is a winning hand.
    the possibilities are Royal Flush, Straight Flush, 4 of a kind, full house, flush, straight, 3 of a kind, 2 pair, pair.
    pseudocode would be very nice.
    also, if you know a website where i could find some examples, that would also be very nice.
    Thanks.

    Although I dislike using ints for enums, I might do it here on the groups that maps from Object to int aren't readily available, and constantly boxing and unboxing is nice. Anyway...
    Each Card should have a getSuit() method and a getValue() method. Have two arrays, int[] suitCount = new int[4], and int[] valueCount = new int[13]. Go through the five cards incrementing the various indices. Then e.g. you have a flush if one of the suitCount elements is 5; a full house if one of the valueCount elements is 3 and one is 2, etc. Straights are slightly more work - you could check them by initialising an int straightCount = 0 and then for i = 0 to 13 (because A counts high and low) if valueCount[i % 13] == 1 then {straightCount++; if straightCount == 5 you have a straight} else straightCount = 0.

  • Poker game logic(again)

    i think that this will be the last thread that i will start on the subject of poker.
    if you have not looked at the other poker game logic threads, it might help.
    if figured out ways to check for repeaded cards(2,3,4,2&2,2&3) and how to do the flush. I am only having trouble figuring out if ther is one of the straights(cards in ascending order).
    you can check the suit and the rank of each card and there are 5. how do you figure if they are in a particular order?
    THANX!

    Sort the card array by face value of card. then
    boolean straight()
    if( ((a[0]+1) == a[1]) && ((a[1]+1) == a[2]) && ... etc. )
    return true;
    You will have to account for ace-2-3-4-5 too though. or 10-j-q-k-ace depending on the value you assign an ace.

  • HT203433 I want to buy chips for a poker game and it keeps telling me to visit itunes/support

    I want to buy chips for a poker game and it keeps telling me to visit www.apple.com/support/support/itens/ww/. My credit card doesn't have a hold and it is a good credit card.  please someone help

    You need to contact itunes support.

  • Poker game help needed!

    I am a beginning Java programmer. I am trying to teach myself and found an example of code for a Java poker program when I was looking online for things to do. It uses classes/methods (which I am still learning), and consists of: Card, Deck, and Hand (and PokerHand). Help with any of the sections would be excellent!
    * Card
    * Member Variables:
    *   INSERT DESCRIPTION OF MEMBER VARIABLES
    * Private Methods:
    *   INSERT DESCRIPTION OF PRIVATE METHODS (IF ANY)
    * Public Methods:
    *   INSERT DESCRIPTION OF PUBLIC METHODS (IF ANY)
    public class Card
        // constants representing the suits
        public static final int HEARTS = 0;
        public static final int CLUBS = 1;
        public static final int DIAMONDS = 2;
        public static final int SPADES = 3;
        // constants representing the named faces
        public static final int JACK = 11;
        public static final int QUEEN = 12;
        public static final int KING = 13;
        public static final int ACE = 14;
          public static final int CARD_PER_SUIT = 13;
        // member variabes
        // INSERT MEMBER VARIABLES HERE
         * Card Constructor
         * Takes as parameters the face and suit of the card
        public Card(int face, int suit)
            // INSERT CODE HERE
         * Card Constructor
         * Takes as parameter the card number
        public Card (int cardno)
         // INSERT CODE HERE
         * toString
         * Returns a String representation of the card
        public String toString()
            // return a String that contains a text representation of the card
                // INSERT CODE HERE
         * getFace
         * Returns the card's face value
        public int getFace()
            // return the face
            // INSERT CODE HERE
         * getSuit
         * Returns the card's suit value
        public int getSuit()
            // return the suit
            // INSERT CODE HERE
         * getCardNo
         * Returns the card number
          public int getCardNo()
                 // return the card number
               // INSERT CODE HERE
    } // end of class Card
    * Hand
    * Member Variables:
    *   INSERT DESCRIPTION OF MEMBER VARIABLES
    * Private Methods:
    *   INSERT DESCRIPTION OF PRIVATE METHODS (IF ANY)
    * Public Methods:
    *   INSERT DESCRIPTION OF PUBLIC METHODS (IF ANY)
    public class Hand
         public static final int HANDSIZE = 5;
         public static final int HIGH_CARD = 0;
         public static final int ONE_PAIR = 1;
         public static final int TWO_PAIRS = 2;
         public static final int THREE_OF_A_KIND = 3;
         public static final int STRAIGHT = 4;
         public static final int FLUSH = 5;
         public static final int FULL_HOUSE = 6;
         public static final int FOUR_OF_A_KIND = 7;
         public static final int STRAIGHT_FLUSH = 8;
         // member variables
         // INSERT MEMBER VARIABLES HERE
         // Hand Constructor
         // INSERT DESCRIPTION OF CONSTRUCTOR HERE
         public Hand()
              // instantiate a hand of cards
              // INSERT CODE HERE
         // resets a hand and throws away all cards in the hand
         // INSERT DESCRIPTION OF METHOD HERE
         public void resetHand()
              // INSERT CODE HERE
         // accepts a card to the hand
         // INSERT DESCRIPTION OF METHOD HERE
         public void TakeCard(Card card)
              // INSERT CODE HERE
         // How many cards does the hand have?
         // INSERT DESCRIPTION OF METHOD HERE
         public int getNumCards()
              // INSERT CODE HERE
         // return the card number of a card in a hand at a specified position
         // INSERT DESCRIPTION OF METHOD HERE
         public int getCard(int cardPosition)
              // INSERT CODE HERE
         // is this hand sorted?
         // INSERT DESCRIPTION OF METHOD HERE
         public boolean isSorted()
              // INSERT CODE HERE
         // sort the cards in this hand from low to high
         // INSERT DESCRIPTION OF METHOD HERE
         public void SortHand()
              // INSERT CODE HERE
         // returns a String that represents the hand
         // INSERT DESCRIPTION OF METHOD HERE
         public String toString()
              // INSERT CODE HERE
    * Deck
    * Member Variables:
    *   INSERT DESCRIPTION OF MEMBER VARIABLES
    * Private Methods:
    *   INSERT DESCRIPTION OF PRIVATE METHODS (IF ANY)
    * Public Methods:
    *   INSERT DESCRIPTION OF PUBLIC METHODS (IF ANY)
    public class Deck
         public static final int DECKSIZE = 52;
         public static final int SHUFFLE_TIMES = 1000000;
         // member variables
         /* INSERT MEMBER VARIABLES HERE */
         * Deck Constructor
         * INSERT DESCRIPTION OF CONSTRUCTOR HERE
         public Deck()
              // instantiate the deck of cards
                 /* INSERT CODE HERE */
         // shuffle the deck
         // INSERT DESCRIPTION OF METHOD HERE
         public void Shuffle(int n)
              /* INSERT CODE HERE */
         // deal a card from the deck
         // INSERT DESCRIPTION OF METHOD HERE
         Card Deal()
              // INSERT CODE HERE
         // how many cards are left in the deck?
         // INSERT DESCRIPTION OF METHOD HERE
         public int cardsLeft()
              // INSERT CODE HERE
         // INSERT DESCRIPTION OF METHOD HERE
         public String toString()
              // INSERT CODE HERE
         // INSERT DESCRIPTION OF METHOD HERE
         public void swap(int i, int j)
              // INSERT CODE HERE
    public class PokerHand
         public static void main (String[] args)
              Deck deck = new Deck();
              Hand hand;
              int i;
              String str;
              hand = new Hand();
              for (i = 0; i < Hand.HANDSIZE; i++) {
                   hand.TakeCard(deck.Deal());
              System.out.println("Player's hand (unsorted): ");
              System.out.println(hand.toString());
              hand.SortHand();
              System.out.println("Player's hand (sorted): ");
              System.out.println(hand.toString());
    }From what I can tell in the directions provided, what it has to do is display a list of the cards, both sorted and unsorted (in order of face value). I think it will be interesting (to my limited exposure), so, as said, any help with any of the classes would be appreciated. Thank you!

    Here's some old code - a game of Pontoon (aka BlackJack, 21 etc)
    a similar structure to your poker game, you'll just have to modify the hand values
    for pairs/straights etc, sorting and a few other things, but it should show you the
    relationship between the objects
    import java.util.*;
    import java.io.*;
    class Pontoon
      BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
      Deck deck = new Deck();
      public Pontoon()
        try
          String playAgain = "y";
          while(playAgain.equalsIgnoreCase("y"))
            playGame();
            System.out.print("\nAnother game? (y/n): ");
            playAgain = input.readLine();
        catch(Exception e){System.out.println("error - terminating");}
        System.exit(0);
      public void playGame() throws IOException
        Hand player = new Hand();
        Hand bank = new Hand();
        String anotherCard = "y";
        player.add(deck.drawCard());
        bank.add(deck.drawCard());
        player.add(deck.drawCard());
        bank.add(deck.drawCard());
        do
          for(int x = 0; x < 20; x++) System.out.println();
          if(player.getHandValue() > 21)
            for(int x = 0; x < 20; x++) System.out.println();
            bank.showHand(0,1);
            player.showHand(0,0);
            System.out.println("\nBank wins - player busted.\n");
            break;
          else
            for(int x = 0; x < 20; x++) System.out.println();
            bank.showHand(1,1);
            player.showHand(0,0);
            System.out.print("Draw another card? (y/n): ");
            anotherCard = input.readLine();
            if(anotherCard.equalsIgnoreCase("y"))
              player.add(deck.drawCard());
            else
              for(int x = 0; x < 20; x++) System.out.println();
              bank.showHand(0,1);
              while(bank.getHandValue() < 16)
                System.out.print("\npress [ENTER] to draw another card for bank.");
                String junk = input.readLine();
                bank.add(deck.drawCard());
                for(int x = 0; x < 20; x++) System.out.println();
                bank.showHand(0,1);
              if(bank.getHandValue() > 21)
                System.out.println("\nPlayer wins - bank busted.\n");
              else
                if(player.getHandValue() > bank.getHandValue())
                  System.out.println("\nPlayer wins - " + player.getHandValue() +
                                           " to " + bank.getHandValue() + ".\n");
                else if(player.getHandValue() < bank.getHandValue())
                  System.out.println("\nBank wins - " + bank.getHandValue() +
                                           " to " + player.getHandValue() + ".\n");
                else
                  System.out.println("\nDraw - " + player.getHandValue() +
                                           " to " + bank.getHandValue() + ".\n");
        }while(anotherCard.equalsIgnoreCase("y"));
        player = null;
        bank = null;
      public static void main(String[] args){new Pontoon();}
    class Hand
      java.util.List hand = new java.util.ArrayList();
      public Hand()
      public void add(Card card)
        hand.add(card);
      public int getHandValue()
        int handValue = 0;
        for(int x = 0; x < hand.size(); x++)
          handValue += ((Card)hand.get(x)).getValue();
        return handValue;
      public void showHand(int numberOfCards, int playerOrBanker)
        String whoseHand = playerOrBanker == 0? "Player":"Bank";
        System.out.println(whoseHand + "'s hand:");
        if(numberOfCards == 1)
          System.out.println(((Card)hand.get(0)).getName() + "\n");
        else
          for(int x = 0; x < hand.size(); x++)
            System.out.println(((Card)hand.get(x)).getName());
          System.out.println("\nHand total = " + getHandValue() + "\n");
    class Deck
      String[] faces = {"Ace","Two","Three","Four","Five","Six","Seven",
                             "Eight","Nine","Ten","Jack","Queen","King"};
      String[] suits = {"Hearts","Diamonds","Clubs","Spades"};
      final int DECKSIZE = 52;
      int cardsDealt;
      Card[] cards = new Card[DECKSIZE];
      java.util.List random = new java.util.ArrayList();
      public Deck()
        int value;
        for(int x = 0; x < cards.length; x++)
          value = (x % faces.length) + 1;
          if(value > 10) value = 10;
          else if(value == 1) value = 11;
          cards[x] = new Card(faces[x % faces.length],suits[x / faces.length],value);
          random.add(new Integer(x));
        shuffleDeck();
      private void shuffleDeck()
        java.util.Collections.shuffle(random);
        cardsDealt = 0;
      public Card drawCard()
        if(cardsDealt > 40) shuffleDeck();
        Card cardDrawn = cards[((Integer)random.get(cardsDealt)).intValue()];
        cardsDealt++;
        return cardDrawn;
    class Card
      private String name;
      private int value;
      public Card(String pFace, String pSuit, int pValue)
        name = pFace + " of " + pSuit;
        value = pValue;
      public String getName() {return name;}
      public int getValue() {return value;}
    }

  • Why I can not buy the poker game money..

    Why I can not buy the poker game money...

    More information would be useful.
    Not available in your country's App Store.
    No credit card information or invalid credit card information or some other problem with your card
    Some other reason

  • Multithreaded Poker game *lost*

    Hi,
    I have an assignment in which I am to make a poker game, one server and four clients.
    "Up to 4 players should be able to connect to your server and play. All four players need to be logged into the server before cards are dealt.
    Players should have to enter their name and each player should be able to see all the players that are connected to the server.
    You can represent playing cards however you wish.
    You don�t have to implement betting, only logging into the server, cards being dealt, the draw and the determination of the winner.
    Server communication with the players and player communication with the server should be handled by separate threads."
    To be honest, I am lost. I just don't understand how it should work.
    Based on the example in the book, I created a server-client classes with client-thread.
    When the client connects, a gui window object is created from the client class.
    I can talk to clients, and clients can talk to me.
    I tried to echo the messages that I get from clients, so that the others can see what has been posted, but I can get it.
    Before I even start trying to create the game itself, I should know how to handle it.
    Can you please just explain to me where to start?
    I uploaded what I've got sofar. Can you please take a look? I am desperate :-(
    www.figuric.com/java/src.rar
    Thank you!

    Has anyone an idea, please?
    Thanks!

  • How do I move cards in a solitaire game on the ipad

    How do I move cards in a solitaire game on the ipad?

    lawrencefromcheshire wrote:
    How do I move cards in a solitaire game on the ipad?
    Touch card, drag/move to desired position.
    Stedman

  • HT4098 I just fill in my credit card to purchase in games but always pls contact itune support, i try to but its hard to change my cc data in it,pls help me,tks

    I just fill in my credit card to purchase in games but always pls contact itune support, i try to but its hard to change my cc data in it,pls help me,tks

    When you get the message to contact iTunes support, that is what you have to do. There is nobody here that can help you, as it must be some issue with your account or credit card that only an Apple employee can help you with.
    Do as instructed in the message and contact iTunes Support. You can start here. Change to your own country if needed by tapping on the link in the upper right corner of this website.
    http://www.apple.com/emea/support/itunes/contact.html

  • Poker game experiencing lag problems

    I've recently written a semi-functional Poker game however I'm experiencing problems with the game slowing down drastically as game play continues. I only have some basic high school programming knowledge so please forgive me for my extremely poor coding practice. Included are are the source files and jar. Any help would be appreciated. Thanks

    Ducky24 wrote:
    Upon using the Net Beans profiler I am finding myself quite shocked. The results show that invoke my formatJButton some 181000 times however I have no idea why it is invoked such.You have your swingComponent() method being called from inside of your paintComponent() method, and that seems like a bad idea to me. Every time your screen redraws, it will call this method, and it seems to me that it should be the other way around -- Your swingComponent should be involved with changing things and then call revalidate() and repaint() to relayout and redraw things.
    Again, I can't force myself to look at all of your code, but I wonder if you should be using a CardLayout here to swap JPanels rather than removing and repositioning and such.

  • Why can,t i load the poker game fantasy poker

    why cant i not load the fantasy poker game poker

    Hello,
    What type of computer and internet connection are you using?
    MrMatthew - HP Support Forums Moderator
    Click the Kudos star as a way to say "thank you" for helpful posts.
    Be sure to come back and click the 'Accept as Solution' button on the post that solved your issue - it may help someone else.

  • Optimal communication strategy for poker game

    Hi,
    I'm designing a client/server poker game, and I'm trying to decide on the best protocol for client/sever comms. It's a personal project, so it doesn't need to be 100% robust or extensible.
    I was thinking of the following model:
    Client(s) and server communicate via Object Input/Output Streams over a socket. Client opens a Listener thread which listens in a while(true) loop, and Server also listens in a while(true) and processes incoming messages. The communication workflow will follow a request-response model.
    I will create my own Message class(String sender, int msgType, Object msgValue), and both listening classes will use switch(msgType) statements to process requests.
    Does anybody have any recomendations/stipulations regarding this model? There are a couple of issues I can think of:
    1. Use of while(true) loops should be avoided
    2. What happens to forever-listening ObjectInputSrteams when the client disconnects unsafely?
    3. How should the server handle client disconnects? Is it sufficient to use the onWindowClose method in the client GUI?
    Any feedback appreciated, thanks in advance,
    Sean

    Why not use RMI instead of plain sockets? The server part can be a
    simple facade to the real poker server (which can be a simple POJO).
    The 'business logic' (the poker game itself) is handled by the real
    poker server while all communication stuff is handled by RMI. That way
    the clients don't have to be aware of the fact that they're talking to some
    thing remote and it frees you from implementing that communication
    layer with all its burden.
    kind regards,
    Jos

  • HT1918 I can not purchase credit card. In the game Clash Of Calns please help

    I can not purchase credit card. In the game Clash Of Calns please help

    What happens when you try to buy them, does the 'buy' button not work, do you get any error messages ... ?
    If you are getting a message to contact iTunes Support then you can do so via this link and ask them for help (we are fellow users on these forums, we won't know why the message is appearing) : http://www.apple.com/support/itunes/contact/ - click on Contact iTunes Store Support on the right-hand side of the page, then Purchases, Billing & Redemption
    If it's a different problem ... ?

  • Rolling Images for poker Game

    Hello everybody,
    i m trying to make poker game in java. it is some what like this, there will three labels which will show continuos scrolling images or numbers. there will a button "HIT" button to stop moving images.after that those images will stop one by one.if all images are same then he will get message of congratulation. first of all i m trying to show numbers in labels. but it should look like it is rolling. with settext method it is directly printing the number. can anybody suggest me how to print number in Jlabel with rolling effect? . Thanks in advance

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    public class SpinningGeeks extends JPanel implements ActionListener,
                                                         Runnable {
        BufferedImage image;
        Rectangle[] rects;
        int[] yPos;
        int dy = 3;
        Random seed = new Random();
        boolean[] runs = new boolean[3];
        Thread thread;
        long delay = 25;
        boolean stopping = false;
        public void actionPerformed(ActionEvent e) {
            String ac = e.getActionCommand();
            if(ac.equals("START"))
                start();
            if(ac.equals("STOP"))
                stopping = true;
        public void run() {
            int[] ds = new int[yPos.length];
            for(int j = 0; j < yPos.length; j++) {
                ds[j] = dy -1 + seed.nextInt(3);
            int runIndex = 0;
            boolean countingDown = false;
            long time = 0;
            while(runs[runs.length-1]) {
                try {
                    Thread.sleep(delay);
                } catch(InterruptedException e) {
                    break;
                if(stopping && !countingDown) {
                    int remainder = yPos[runIndex] % rects[runIndex].height;
                    boolean atEdge = remainder < ds[runIndex]/2;
                    if(atEdge) {
                        // Stop here, start waiting 2 seconds before
                        // moving on to stop the next image.
                        countingDown = true;
                        time = 0;
                        runs[runIndex] = false;
                        runIndex++;
                        if(runIndex > runs.length-1) {
                            continue;
                } else if(countingDown) {
                    // Wait 2 seconds before stopping the next image.
                    time += delay;
                    if(time > 2000) {
                        countingDown = false;
                // Advance all images that have not been stopped.
                for(int j = 0; j < yPos.length; j++) {
                    if(runs[j]) {
                        yPos[j] += ds[j];
                        if(yPos[j] > image.getHeight()) {
                            yPos[j] = 0;
                repaint();
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            int pad = 5;
            int x = (getWidth() - (3*rects[0].width + 2*pad))/2;
            int y = (getHeight() - rects[0].height)/2;
            for(int j = 0; j < rects.length; j++) {
                rects[j].setLocation(x, y);
                x += rects[j].width + pad;
            Shape origClip = g2.getClip();
            for(int j = 0; j < yPos.length; j++) {
                x = rects[j].x;
                y = rects[j].y - yPos[j];
                // Comment-out next line to see what's going on.
                g2.setClip(rects[j]);
                g2.drawImage(image, x, y, this);
                if(yPos[j] > image.getHeight() - rects[j].height) {
                    y += image.getHeight();
                    g2.drawImage(image, x, y, this);
            g2.setClip(origClip);
            g2.setPaint(Color.red);
            for(int j = 0; j < rects.length; j++) {
                g2.draw(rects[j]);
        private void start() {
            if(!runs[runs.length-1]) {
                Arrays.fill(runs, true);
                stopping = false;
                thread = new Thread(this);
                thread.setPriority(Thread.NORM_PRIORITY);
                thread.start();
        private void stop() {
            runs[runs.length-1] = false;
            if(thread != null) {
                thread.interrupt();
            thread = null;
        private JPanel getContent(BufferedImage[] images) {
            // Make a film strip assuming all images have same size.
            int w = images[0].getWidth();
            int h = images.length*images[0].getHeight();
            int type = BufferedImage.TYPE_INT_RGB;
            image = new BufferedImage(w, h, type);
            Graphics2D g2 = image.createGraphics();
            int y = 0;
            for(int j = 0; j < images.length; j++) {
                g2.drawImage(images[j], 0, y, this);
                y += images[j].getHeight();
            g2.dispose();
            // Initialize clipping rectangles.
            rects = new Rectangle[3];
            for(int j = 0; j < rects.length; j++) {
                rects[j] = new Rectangle(w, images[0].getHeight());
            // Initialize yPos array.
            yPos = new int[rects.length];
            for(int j = 0; j < yPos.length; j++) {
                yPos[j] = 2*j*images[0].getHeight();
            return this;
        private JPanel getControls() {
            String[] ids = { "start", "stop" };
            JPanel panel = new JPanel();
            for(int j = 0; j < ids.length; j++) {
                JButton button = new JButton(ids[j]);
                button.setActionCommand(ids[j].toUpperCase());
                button.addActionListener(this);
                panel.add(button);
            return panel;
        public static void main(String[] args) throws IOException {
            String[] ids = { "-c---", "--g--", "---h-", "----t", "-cght" };
            BufferedImage[] images = new BufferedImage[ids.length];
            for(int j = 0; j < images.length; j++) {
                String path = "images/geek/geek" + ids[j] + ".gif";
                images[j] = javax.imageio.ImageIO.read(new File(path));
            SpinningGeeks test = new SpinningGeeks();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getContent(images));
            f.add(test.getControls(), "Last");
            f.setSize(400,300);
            f.setLocation(200,200);
            f.setVisible(true);
    }geek images from [Using Swing Components Examples|http://java.sun.com/docs/books/tutorial/uiswing/examples/components/index.html]

  • My calendar is in arabic langauge,how can i make it english?even the chios amount in my poker game it shows squares not numbers.but my ipad is in english already.

    my calendar is in arabic langauge,how can i make it english?even the chips amount in my poker game it shows squares not numbers.but my ipad is in english already.

    Make sure you have set the REGION to one where English is spoken.

Maybe you are looking for

  • Screen is dead, and restoring isn't fixing it

    A few days after updating my 1st gen iPod touch to 3.1.3, I was suddenly given a white screen. This was followed by a black screen after reseting it. After restoring my iPod, the screen will not turn on at all but the iPod will behave as if it is in

  • What is the private tab.

    What is the private tab which has mysteriously appeared on Safari ?

  • Rollback Adobe Reader version on Mac OSX 10.6

    Hello all, I recently upgraded to Adobe Reader version 9.2. I use a drm software on my machine from Oracle that is not compatible with 9.2. I need to roll back to 9.1 I went into my applications folder and deleted Adobe Reader 9.2. searched for all r

  • Computer ID and inventory continuity

    Hi guys, I have some questions about computer identification - does anyone can tell me how ZCM recognize computer ID and what is that unique ID for the computer? Is that GUID and only GUID? My potential customer asked me about it, and i'm concidering

  • Adobe after affects download trial problem

    whenever i try to download adobe after affects the trial this shows up please help!