Sagar Gautam
Sagar Gautam

Reputation: 9369

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 ?

Any suggestion is appreciated.

Upvotes: 0

Views: 3069

Answers (3)

Deepkamal
Deepkamal

Reputation: 360

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.

Upvotes: 1

Sandip Shiroya
Sandip Shiroya

Reputation: 11

const cardDestribution = (numOfPlayers) => {
  let usedNumbers = "-";
  let randomNumbers = [];
  for (let i = 0; i < 52; i++) {
    let randomNumber = Math.floor(Math.random() * 52) + 1;
    //Checking if the random number you get is unique, if you already have it in the string means that this random number is repeated
    while (usedNumbers.includes("-" + randomNumber + "-")) {
      //if a number is repeated, then get a new random number
      randomNumber = Math.floor(Math.random() * 52) + 1;
    }
    usedNumbers += randomNumber + "-";
    randomNumbers[i] = randomNumber;
  }
  randomNumbers = randomNumbers.splice(0, 3 * numOfPlayers);
  return randomNumbers.reduce(
    (rows, key, index) =>
      (index % 3 == 0 ? rows.push([key]) : rows[rows.length - 1].push(key)) &&
      rows,
    []
  );
};

const getScore = (cards) => {
  let color = [];
  newCards = cards.map((card) => {
    if (card > 39) {
      color.push(3);
      return card - 39;
    } else if (card > 26) {
      color.push(2);
      return card - 26;
    } else if (card > 13) {
      color.push(1);
      return card - 13;
    } else {
      color.push(0);
      return card;
    }
  });

  newCards = newCards.sort().reverse();

  console.log("Your card:", newCards);
  let score = 0;
  //Colors
  if (color[0] === color[1] && color[1] === color[2]) {
    score = Math.pow(10, 6);
  }
  //Tripple
  if (newCards[0] === newCards[1] && newCards[1] === newCards[2])
    score +=
      Math.pow(10, 8) +
      Math.pow(2, newCards[0]) +
      Math.pow(2, newCards[1]) +
      Math.pow(2, newCards[2]);
  //Sequence
  else if (newCards[0] - newCards[1] === 1 && newCards[1] - newCards[2] === 1)
    score +=
      Math.pow(10, 7) +
      Math.pow(2, newCards[0]) +
      Math.pow(2, newCards[1]) +
      Math.pow(2, newCards[2]);
  //Double
  else if (newCards[0] === newCards[1] || newCards[1] === newCards[2]) {
    if (newCards[1] === 1) {
      index = 14;
    } else index = newCards[1];
    score +=
      Math.pow(10, 5) +
      Math.pow(2, newCards[0]) +
      Math.pow(2, newCards[1]) +
      Math.pow(2, newCards[2]) +
      16338 * index;
  } //Nothing
  else
    score +=
      Math.pow(2, newCards[0]) +
      Math.pow(2, newCards[1]) +
      Math.pow(2, newCards[2]);

  //This condtion for RKA
  if (newCards[0] === 13 && newCards[1] === 12 && newCards[2] === 1)
    score +=
      Math.pow(10, 7) +
      Math.pow(2, newCards[0]) +
      Math.pow(2, newCards[1]) +
      Math.pow(2, newCards[2]);
  if (newCards[2] === 1) score += 16338;
  return score;
};

let players = cardDestribution(5);
console.log("Grater Score is winner");
players.map((player) => {
  console.log(player, "\n yourScore is:", getScore(player), "\n\n");
});

Upvotes: 1

Antti_M
Antti_M

Reputation: 968

boolean trial, doublerun, run, color, same;
if (this.checkTrial()) {
    trial = true;
}
else if (this.checkDoubleRun() {
    doubleRun = true;
}
else if (...) {
    ...
}
...

Upvotes: 1

Related Questions