Javascript 为什么我的代码执行的次数远远超过它';应该是吗?
我目前正在开发一个扑克赔率生成器,除了一件事,它几乎完成了。这个程序运行的频率远远超过了它应该运行的频率。我知道这来自于Javascript 为什么我的代码执行的次数远远超过它';应该是吗?,javascript,Javascript,我目前正在开发一个扑克赔率生成器,除了一件事,它几乎完成了。这个程序运行的频率远远超过了它应该运行的频率。我知道这来自于compare()函数,因为当我添加一个变量Q来跟踪主函数playPoker()运行的次数时,它会产生一个巨大的数字,直到我将其注释掉——在这一点上,它会返回我期望的确切数字 你们谁能指出我在这件事上哪里出了问题。我不明白为什么一个函数会导致Q的增量比它应该的要大得多。从字面上看,我上次运行它时的数字是(32487而不是100)。我在下面介绍代码(没有扑克手检查函数,因为它们并
compare()
函数,因为当我添加一个变量Q
来跟踪主函数playPoker()
运行的次数时,它会产生一个巨大的数字,直到我将其注释掉——在这一点上,它会返回我期望的确切数字
你们谁能指出我在这件事上哪里出了问题。我不明白为什么一个函数会导致Q
的增量比它应该的要大得多。从字面上看,我上次运行它时的数字是(32487而不是100)。我在下面介绍代码(没有扑克手检查函数,因为它们并不重要)。为什么playPoker()
运行了这么多次
var Q = 0;
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, straightValues, players[m]);
//Find out whether the 5 table cards contain three cards of the same suit. If not, then a flush is impossible.
var flushPotential = threeSameSuit(cardTable.cards);
//Find out which hand each player has (i.e. straight, 3OAK, pair)
checkPokerHand(subjectCards, straightValues, valuesInOrder, flushPotential, numPairs, players[m])
}
var rankedPlayers = compare(players);
//return players;
Q++;
return Q;
}
尝试对变量使用
var
声明来管理它们在相关函数中的作用域?我认为在不知道所有这些函数都做什么的情况下回答这个问题是相当困难的。由于您认为compare
是问题所在,因此了解它的工作原理可能很重要。你能把你的代码简化成一个更小的例子,仍然有同样的意外行为吗?当然,我会这么做的now@andersschuller我已经更新了它,我可以建议您将怪物函数拆分为一组较小的函数吗?当然可以,playPoker()
或compare()
?这只是为了简化调试吗?
for (z = 0; z < 100; z++;) {
playPoker(4);
}
function compare(players) {
var remPlayers = players.slice();
var rankings = [];
var potentialWinners = [];
//Collect all the players' rankings in an array
for (i = 0; i < remPlayers.length; i++) {
rankings.push(remPlayers[i].rank);
}
//Find the highest ranking
var highestRank = getHighestValue(rankings);
//Move any players with the highest ranking to an array for potential winners
for (j = 0; j < remPlayers.length; j++) {
if (remPlayers[j].rank == highestRank) {
potentialWinners.push(remPlayers[j]);
remPlayers.splice(j, 1);
j--;
}
}
//With all potential winners gone, mark all other players with an L for losing.
for (k = 0; k < remPlayers.length; k++) {
remPlayers[k].result = 'L'
}
var foundWinner = false;
if (potentialWinners.length < 2) {
potentialWinners[0].result = 'W';
foundWinner = true;
}
//Check there is more than one potential winner. If not, the only guy in the array has won.
if (!foundWinner) {
//Loop through all players first cards and find the highest value, then delete any who don't have that, marking them with 'L'.
//If there is no single remnant, do the same for second cards, then third, then fourth, etc.
for (p = 0; p < 5; p++) {
var subRankings = [];
for (q = 0; q < potentialWinners.length; q++) {
subRankings.push(potentialWinners[q].bestHand[p]);
}
var highestSubRanking = getHighestValue(subRankings);
//Mark 'L' and remove any player who does not meet the highest subranking
for (m = 0; m < potentialWinners.length; m++) {
if (potentialWinners[m].bestHand[p] < highestSubRanking) {
potentialWinners[m].result = 'L';
potentialWinners.splice(m, 1);
}
if (potentialWinners.length < 2) {
potentialWinners[0].result = 'W';
//Set this flag to true to break the loop because a winner has been found
foundWinner = true;
break;
}
}
//Break the loop if we have found a winner
if (foundWinner) {
break;
}
//If we still haven't found a winner by the end of the 5th loop, all remaining players draw
if (p == 4) {
for (z = 0; z < potentialWinners.length; z++) {
potentialWinners[z].result = 'D';
}
}
if (foundWinner) {
break;
}
}
}
return players;
}