javascript - 构建扑克概率计算器 - 如何收集有关不断删除和重新创建的对象的数据?

标签 javascript aggregation poker

我目前正在开发一个扑克概率计算器,它基本上是为了根据用户的起手牌来预测用户赢得扑克游戏的几率。它“玩”了数百万局游戏,然后汇总每手起手牌导致其所有者拥有最高排名手牌的频率。

我已经制定了很多游戏代码,您可以在下面看到:

function playPoker(tableSize) {

//Create the players, the deck and the card table which stores the 5 cards the players have in common
var players = createPlayers(tableSize);
var deck = createDeck();
var cardTable = new CardTable();

//Deal each player two cards
for (i = 0; i < 2; i++) {
    for (j = 0; j < players.length; j++) {
        deal(deck, players[j]);
    }
}

//Put five cards down on the table
for (k = 0; k < 5; k++) {
    deal(deck, cardTable);
}

//Check for various winning hands here for each player
for (m = 0; m < players.length; m++) {

    //Merge the player's two cards with the five cards on the table
    var subjectCards = (players[m].cards).concat(cardTable.cards);

    //Make an array of the values of each of the seven cards, which will be used to determine 4 of a kind, 3 of a kind and pairs
    var valuesInOrder = getValuesInOrder(subjectCards);

    //Create a dummy array, so that valuesInOrder remains unchanged
    var straightValues = valuesInOrder.slice();

    //Remove any duplicate card, meaning that the array contains only unique values (i.e. 2, 4, 5, 7, K ... NOT 2, 2, 2, 8, K, K, A)
    var straightValues = straightenUp(straightValues);

    //Calculate how many pairs are in the hand
    var numPairs = howManyPairs(valuesInOrder);

    //Check whether the player has a royal flush, the highest ranking hand - then check the other hands by ranking
    checkRoyalFlush(subjectCards, straightValues);

    checkStraightFlush(subjectCards, straightValues);

    checkFourOAK(valuesInOrder);

    checkFullHouse(valuesInOrder)

    checkFlush(subjectCards);

    checkStraight(straightValues);

    checkThreeOAK(valuesInOrder);

    checkTwoPairs(numPairs);

    checkPair(numPairs);

}

}`

基本上,每张牌都是一个具有西装属性(从 1 到 4)和值(value)属性(从 2 到 14)的对象。我已经掌握了非常有效地计算排名的所有逻辑 - 但我不知道如何整理每个卡对象的数据(即 K♥ K♠ 导致最高排名手牌的频率,50 次中有多少次)百万?)

显然,我可以创建 if 语句,每次右起手牌获胜时,该语句都会递增,但我需要创建 52^2 个 if 语句。有人对此有更优雅的解决方案吗?谢谢!

最佳答案

你只需要一本有 169 个键的字典,所有的都是独特的起手牌。他们的确切花色并不重要,只要他们是花色,彩虹色或一对。使用这样的字典,您可以增加最高排名手牌的相应值。

关于javascript - 构建扑克概率计算器 - 如何收集有关不断删除和重新创建的对象的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33244280/

相关文章:

java - 如何修复 Poker Deck 使其不重新实例化并将其限制为一副牌中只有 52 张牌?

javascript - Angular 2,textarea(用文字附加表情符号)

javascript - React Native - 如何在多个按钮上应用 Prop

javascript - React Native onPress 事件延迟

javascript - 如何使用 Mongoose 羽毛适配器编写聚合?

database - 如何在 SQL 中对包含多列的时间序列数据进行下采样?

javascript - 函数定义中参数周围的方括号

mongodb - 如何使用 MongoDB 聚合对多个操作进行分组

java - 设计扑克游戏的结构

algorithm - 什么RNG(随机数生成器)算法适合扑克牌洗牌?