Search code examples
javaalgorithmoptimizationplaying-cards

how to determine pattern of cards in 3 Card Game to Find Winner?


I have created N Three card hands using random sampling from a deck of 52 cards. Now, I want to find winner among the N hands.

These are the rules to find winner.

  1. Trial(Three cards with same values)
  2. Double Run (Three Cards with values in a sequence and of same suit)
  3. Run (Three Cards in a sequence)
  4. Color(Three Cards of same suit)
  5. Same(two out of three cards have same values)
  6. Normal condition

The winning priority of Cards is in descending order and probability of getting these cards is in ascending order.

i.e.

Probability of getting trial is least and winning priority is most and so on.

I have checked in sequence of trial to normal.

boolean trial = this.checkTrial();
    if(!trial){
        boolean doubleRun = this.checkDoubleRun();
        if(!doubleRun){
            boolean run = this.checkRun();
            if(!run){
                boolean color = this.checkColor();
                if(!color){
                    boolean same = this.checkSame();
                    if(!same){
                        this.checkHighest();
                        System.out.println("Normal");
                    }
                    else{
                        System.out.println("Same");
                    }
                }
                else{
                    System.out.println("Color");    
                }
            }
            else{
                System.out.println("Run");    
            }
        }
        else{
            System.out.println("Double Run");    
        }
    }
    else{
        System.out.println("Trial");    
    }

Is this the best way ?

I have two options What is the best way to find winning hand among these two ?

  • least to most probability(check from Trial to normal)
  • most to least probability(check from normal to trial)

Any suggestion is appreciated.


Solution

  • As i see you need to build a game system of playing cards with game of 3 card poker or 3 patti or flush, lets identify all objects involved

    1. Card - has 2 attribute Color and Cardinality, ergo Class card will be
        class Card {
    
            char color;
            short number;
    
            public Card(char color, short number) {
                this.color = color;
                this.number = number;
    
            }
    
            public char getColor() {
                return color;
            }
    
            public void setColor(char color) {
                this.color = color;
            }
    
            public short getNumber() {
                return number;
            }
    
            public void setNumber(short number) {
                this.number = number;
            }
        }
    
    1. Deck - Deck is set of 52 cards ,thus
        class Deck {
    
            char[] deckColors = {'♦', '♠', '♣', '♥'};
            short[] cardNum = {(short) 'A', (short) '2', (short) '3', (short) '4', (short) '5', (short) '6', (short) '7',
                               (short) '8', (short) '9', (short) 'T', (short) 'J', (short) 'Q', (short) 'K'};
            int cardCount;
    
            public Card[] getShuffledDeck() {
    
                Random r = new Random();
    
    
                Card[] deckCards = new Card[(deckColors.length * cardNum.length)];
    
                int cnt = 0;
    
                for (char c : deckColors) {
                    for (short s : cardNum) {
                        deckCards[cnt++] = new Card(c, s);
                    }
                }
    
                Card[] shuffledDeck = new Card[deckCards.length];
    
                int addedCount = 0;
    
    
                while (addedCount < deckCards.length) {
                    int tInt = r.nextInt((deckCards.length));
    
                    Card c = deckCards[tInt];
    
                    if (c != null) {
    
                        shuffledDeck[addedCount++] = c;
                        deckCards[tInt] = null;
    
                    } else {
                        
                    }
                }
    
                return shuffledDeck;
    
            }
        }
    
    1. Hand - set of 3 cards,but its better to have a class created with methods like getHandRanking() which will actually calculate dealt Hand's strength
       class Hand {
    
            Card[] cards;
    
            int handRank;
    
            public Hand(Card[] cards) {
    
                this.cards = new Card[3];
    
                //sort all cards
                if (cards[0].getNumber() > cards[1].getNumber()) {
                    if (cards[0].getNumber() > cards[2].getNumber()) {
                        //0 index is highest card
                        this.cards[2] = cards[0];
                        if (cards[2].getNumber() > cards[1].getNumber()) {
                            //2 is second highest
                            this.cards[1] = cards[2];
                            this.cards[0] = cards[1];
                        } else {
                            //1 is second highest
                            this.cards[1] = cards[1];
                            this.cards[0] = cards[2];
                        }
    
                    } else {
                        //2 index is highest
    
                        this.cards[2] = cards[2];
                        if (cards[0].getNumber() > cards[1].getNumber()) {
                            //0 is second highest
                            this.cards[1] = cards[0];
                            this.cards[0] = cards[1];
                        } else {
                            //1 is second highest
                            this.cards[1] = cards[1];
                            this.cards[0] = cards[0];
                        }
                    }
    
                } else {
                    if (cards[1].getNumber() > cards[2].getNumber()) {
                        //1 index is highest card
                        this.cards[2] = cards[1];
                        if (cards[2].getNumber() > cards[0].getNumber()) {
                            //2 is second highest
                            this.cards[1] = cards[2];
                            this.cards[0] = cards[0];
                        } else {
                            //0 is second highest
                            this.cards[1] = cards[0];
                            this.cards[0] = cards[2];
                        }
                    } else {
                        //2 index is highest
                        this.cards[2] = cards[2];
                        if (cards[0].getNumber() > cards[1].getNumber()) {
                            //0 is second highest
                            this.cards[1] = cards[0];
                            this.cards[0] = cards[1];
                        } else {
                            //1 is second highest
                            this.cards[1] = cards[1];
                            this.cards[0] = cards[0];
                        }
                    }
                }
            }
    
            public int getHandRank() {
                return handRank > 0 ? handRank : calculateHandRank();
            }
    
            public int calculateHandRank() {
                //assuming 3 cards dealt
                //Trial - ColorSeq - Seq - Color - Pair
                int[] powerOf2s = {1, 2, 4, 8, 16};
                return ((cards[0].getNumber() == cards[1].getNumber() && cards[1].getNumber() == cards[2].getNumber()) ? 1 : 0) * powerOf2s[4]
                        + (((cards[2].getNumber() - 1 == cards[1].getNumber() && cards[1].getNumber() - 1 == cards[0].getNumber()) && (cards[2].getColor() == cards[1].getColor() && cards[1].getColor() == cards[0].getColor())) ? 1 : 0) * powerOf2s[3]
                        + ((cards[2].getNumber() - 1 == cards[1].getNumber() && cards[1].getNumber() - 1 == cards[0].getNumber()) ? 1 : 0) * powerOf2s[2]
                        + (((cards[2].getColor() == cards[1].getColor() && cards[1].getColor() == cards[0].getColor())) ? 1 : 0) * powerOf2s[1]
                        + ((cards[0].getNumber() == cards[1].getNumber() || cards[1].getNumber() == cards[2].getNumber() || cards[0].getNumber() == cards[2].getNumber()) ? 1 : 0) * powerOf2s[0];
    
            }
        }
    

    Now you just need to see which player's hand's rank is highest among all player hands, and if two player happen to have same hand rank then see who has high card in sequence of Hand.cards[2],[1],[0].

    let me know if any explanation is required.

    the algo can be improved greatly, the given code example is just to show the thought process.