【问题标题】:how to determine pattern of cards in 3 Card Game to Find Winner?如何确定三张牌游戏中的牌型以找到赢家?
【发布时间】:2017-09-28 13:02:58
【问题描述】:

我从一副 52 张牌中随机抽样创建了 N 三张牌手。现在,我想在 N 手牌中找到赢家。

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

  1. 试用(三张卡值相同)
  2. Double Run(三张牌,价值顺序相同,花色相同)
  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");    
    }

这是最好的方法吗?

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

  • 从最小到最大概率(检查从试验到正常)
  • 从最大到最小概率(检查从正常到试验)

欢迎提出任何建议。

【问题讨论】:

  • 你想如何返回结果?打印出来还是保存在适当的布尔值中?知道什么是最好的排名就足够了,还是应该知道它满足的所有规则?
  • 我想稍后将它保存到一个布尔值数组中。六种可能性中只有一种,即相互排斥,因此,如果找到六种可能性中的任何一种,那么我必须停下来。
  • 我想以最好的方式找到可能的情况

标签: java algorithm optimization playing-cards


【解决方案1】:

正如我所见,您需要使用 3 张牌扑克或 3 张 patti 或同花游戏来构建扑克牌游戏系统, 让我们识别所有涉及的对象

  1. 卡片 - 有 2 个属性颜色和基数,因此类卡片将是
    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. 牌组 - 牌组有 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 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]。

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

算法可以大大改进,给出的代码示例只是展示思考过程。

【讨论】:

  • 试着理解这个 calculateHandRank() 算法在做什么,如果有问题请告诉我,我会进一步解释
  • 感谢您的回答
  • 我已经制作了所有这些卡片手牌和套牌。我已经洗牌并分发了 3/3 卡给 N 个用户。你的逻辑对我来说似乎很好。太好了!
  • 太棒了,pl 用 AKQ 处理序列情况,因为 Ace 的基数是 1,你需要检查一下,以便 AKQ 和 A23 都被处理为 vslid 序列和序列 w 颜色。
  • 是的,我明白了。我使用了 2,3,4,5,6,7,8,9,10,J,Q,K,A 序列,所以 QKA 最高,但我必须将 A23 视为特例
【解决方案2】:
boolean trial, doublerun, run, color, same;
if (this.checkTrial()) {
    trial = true;
}
else if (this.checkDoubleRun() {
    doubleRun = true;
}
else if (...) {
    ...
}
...

【讨论】:

  • 我做过类似的,但它是最好的吗?
  • 至少可读性更强,更容易理解。在这么简单的问题中,真的没有其他标准。
  • 我对优化不太了解。我只想尽快
  • @Sagar 我对优化了解很多,我必须说:首先,获取一些干净简单的代码。完成后,测量哪个部分耗时最长,然后进行优化。 +++ 您的情况很简单:“一副 52 张牌中的三张牌”意味着52**3 = 140608 可能性。您可以将它们全部存储在一个表中并获得亚微秒级的时间。 +++ 请注意,这意味着到目前为止您优化的所有方法的速度几乎不重要,因为表只初始化一次并且可能存储到磁盘(如果确实需要)。
【解决方案3】:

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

【讨论】:

  • Hear 是我用 javascript 为青少年 patti 游戏编写的一些基本代码,为了好玩,并按顺序听到 QKA 不是 1,A23 不是 2 其他是 KQJ 不是 3 ......等等。
  • 首先,从 52 张牌组中生成 3 对牌组,在 3 对牌组得分后,我们可以决定谁是赢家。我是一名节点开发人员,如果有人有空缺,我需要一份工作。
猜你喜欢
  • 2012-01-13
  • 2017-02-16
  • 2023-03-28
  • 2014-04-24
  • 1970-01-01
  • 1970-01-01
  • 2017-10-06
  • 2012-12-03
  • 1970-01-01
相关资源
最近更新 更多