Zetland
Zetland

Reputation: 567

Why is my code executing far more times than it's supposed to?

I'm currently working on a poker odds generator and it's pretty much done, except for one thing. The program runs far more often than it should. I know that this is coming from the compare() function, because when I add a variable Q to keep track of the number of times the main function playPoker() has run, it produces a huge number until I comment it out - at which point it returns the exact number I'd expect.

Can any of you point out where I'm going wrong with this. I can't see why one function should lead to Q being incremented so much more than it should be. Literally, the last time I ran it the number was (32,487 instead of 100). I present the code below (without the poker hand-checking functions because they're not really important). Why is playPoker() running so many times?!

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;

}

And here's the for-loop that sets it off.

for (z = 0; z < 100; z++;) {
    playPoker(4);
}

And here's the compare() function:

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

Upvotes: 1

Views: 64

Answers (1)

An0nC0d3r
An0nC0d3r

Reputation: 1303

Try using var declarations on your variables to manage their scope within their relevant functions?

Upvotes: 2

Related Questions