如何确定 3 Card Game to Find Winner 中的纸牌图案?

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

我使用从一副 52 张牌中随机抽样创建了 N 三手牌。现在,我要在N手牌中找出胜利者。

这些是寻找获胜者的规则。

  1. 试用版(三张相同点数的牌)
  2. 双 运行(三张牌的点数相同且花色相同)
  3. 运行(一个序列中的三张牌)
  4. 颜色(三张同花色)
  5. 相同(三张牌中有两张牌的点数相同)
  6. 正常情况

卡牌的获胜优先级为降序,获得这些卡牌的概率为升序。

获得试用的概率最小,获胜优先级最高,依此类推

我检查了一下试用顺序是正常的。

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");    
    }

这是最好的方法吗?

我有两个选择 在这两个中找到获胜手牌的最佳方法是什么?

如有任何建议,我们将不胜感激。

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

正如我所见,您需要构建一个扑克牌游戏系统,包括 3 张纸牌扑克或 3 张 patti 或同花顺, 让我们识别所有涉及的对象

  1. Card - 有 2 个属性 Color 和 Cardinality,因此 Class card 将是
    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 是一组 52 张牌,因此
    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 - 一组 3 张牌,但最好使用 getHandRanking() 等方法创建 class,它实际上会计算发牌的牌力
   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];

        }
    }

现在你只需要看所有玩家手牌中哪个玩家的手牌等级最高,如果两个玩家的手牌等级相同则按Hand.cards[2]的顺序看谁的牌大, [1],[0].

如果需要任何解释,请告诉我。

算法可以大大改进,给出的代码示例只是为了展示思路。

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");
});