构建扑克概率计算器 - 如何收集关于不断被删除和重新创建的对象的数据?
Building a poker probabilities calculator - how can I collect data about objects which keep getting deleted and recreated?
我目前正在研究一种扑克概率计算器,它主要用于根据 his/her 起手牌预测用户赢得一场扑克游戏的几率。它 'plays' 数百万场比赛,然后汇总每手起手牌导致其拥有者获得最高排名手牌的频率。
我有很多游戏代码已经完成了,你可以在下面看到:
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)和值 属性(从 2 到 14)的对象。我已经掌握了计算排名的所有逻辑,这些逻辑非常有效——但我不知道如何整理每个卡片对象的数据(即 K♥ K♠ 导致排名最高的手的频率,50 次中有多少次百万?)
显然,我可以创建 if 语句,每次右起手获胜时该语句都会递增,但我需要创建 52^2 个这样的语句。有人对此有更优雅的解决方案吗?谢谢!
你只需要一本169键的字典,都是独一无二的起手牌。他们的确切花色并不重要,只要他们是同花,彩虹色或一对。有了这样一个字典,你可以增加最高排名手的相应值。
我目前正在研究一种扑克概率计算器,它主要用于根据 his/her 起手牌预测用户赢得一场扑克游戏的几率。它 'plays' 数百万场比赛,然后汇总每手起手牌导致其拥有者获得最高排名手牌的频率。
我有很多游戏代码已经完成了,你可以在下面看到:
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)和值 属性(从 2 到 14)的对象。我已经掌握了计算排名的所有逻辑,这些逻辑非常有效——但我不知道如何整理每个卡片对象的数据(即 K♥ K♠ 导致排名最高的手的频率,50 次中有多少次百万?)
显然,我可以创建 if 语句,每次右起手获胜时该语句都会递增,但我需要创建 52^2 个这样的语句。有人对此有更优雅的解决方案吗?谢谢!
你只需要一本169键的字典,都是独一无二的起手牌。他们的确切花色并不重要,只要他们是同花,彩虹色或一对。有了这样一个字典,你可以增加最高排名手的相应值。