如何确定3张牌游戏中的牌模​​式以寻找获胜者?

时间:2017-05-01 02:44:20

标签: java algorithm optimization playing-cards

我使用52张牌的随机抽样创建了N三张牌。现在,我想在N手中找到胜利者。

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

  1. 试用(三张具有相同值的卡片)
  2. Double Run(具有序列和相同诉讼值的三张牌)
  3. 运行(序列中的三张卡片)
  4. 颜色(同一套装的三张牌)
  5. 相同(三张牌中有两张牌的价值相同)
  6. 正常情况
  7. 卡的获胜优先级按降序排列,获得这些卡的概率按升序排列。

    获得审判的可能性最小,获胜的优先级最高,等等。

    我已经检查了正常的试验顺序。

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

    这是最好的方式吗?

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

    • 最不可能的概率(从试验检查到正常)
    • 最不可能的概率(从正常到试验检查)

    任何建议都表示赞赏。

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

正如我所知,你需要建立一个游戏系统的扑克牌,玩3张扑克游戏或3张游戏或冲洗, 让我们识别所有涉及的对象

  1. - 有2个属性Color and Cardinality,ergo Class卡将

    类卡{

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

    }

  2. 甲板 - 甲板上有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;
    
    }
    

    }

  3. - 设置3张牌,但最好是使用getHandRanking()这样的方法创建一个类,它实际上会计算出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];
    
    }
    

    }

  4. 现在你只需要看看所有玩家手牌中哪个牌手的等级最高,如果两个玩家碰巧有相同的手牌等级,那么看看Hand.cards序列中谁有高牌[2],[1], [0]。

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

    算法可以大大改进,给定的代码示例只是为了展示思维过程。

答案 2 :(得分:1)

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