简体   繁体   中英

While loop ignores initial condition and the browser crashes

So long story short - I'm trying to build a simple tennis match simulation (code below). Unfortunately, something is wrong with my code, because the while loop I created ignores the condition put in brackets and starts to make infinite number of itinerations (browser crashes). Could you please have a look at my code and tell me where the error lies?

var gamesPlayerOne = Math.floor(Math.random() * 8);
var gamesPlayerTwo = Math.floor(Math.random() * 8);
var tiebreak = Math.floor(Math.random() * 10);
var setsPlayerOne = 0;
var setsPlayerTwo = 0;
var scoreline = [];

function playTheGame(g1, g2) {

    while (setsPlayerOne < 2 && setsPlayerTwo < 2) {

        if (g1 === 6 && g2 < 5) {

            var result = g1.toString() + ":" + g2.toString();
            setsPlayerOne += 1;
            scoreline.push(result);

        } else if (g1 < 5 && g2 === 6) {

            var result = g1.toString() + ":" + g2.toString();
            setsPlayerTwo += 1;
            scoreline.push(result);

        } else if (g1 === 6 && g2 === 7) {

            var result = g1.toString() + ":" + g2.toString() + "(" + tiebreak + ")";
            setsPlayerTwo += 1;
            scoreline.push(result);

        } else if (g1 === 7 && g2 === 6) {

            var result = g1.toString() + ":" + g2.toString() + "(" + tiebreak + ")";
            setsPlayerTwo += 1;
            scoreline.push(result);

        }
    }
}

playTheGame(gamesPlayerOne,gamesPlayerTwo);
console.log(scoreline);

For example... you didn't specify any condition to increase setsPlayer[One/Two] when g1 is equals to 0 and g2 is equals to 7.

So you should add some condition to check it.

If the random numbers that you pass into your function don't match any of the if or else if conditions then none of your variables is ever updated so the while loop's condition remains true forever.

If you are trying to simulate the entire tennis match, it would make more sense not to pass the function any arguments, and instead on each iteration of the while loop decide randomly which player just won the current game and then test if either player has won a set yet, perhaps something like this:

 function playTheGame() { var g1 = 0; var g2 = 0; var setsPlayerOne = 0; var setsPlayerTwo = 0; var scoreline = []; while (setsPlayerOne < 2 && setsPlayerTwo < 2) { // determine a random winner for the current game if (Math.random() < 0.5) g1++; else g2++; // has one of the players just won a set? if (g1 >= 6 && g2 < g1 - 1) { var result = g1 + ":" + g2; setsPlayerOne += 1; scoreline.push(result); g1 = g2 = 0; } else if (g1 < g2 - 1 && g2 >= 6) { var result = g1 + ":" + g2; setsPlayerTwo += 1; scoreline.push(result); g1 = g2 = 0; } } return scoreline; } console.log(playTheGame()); 

Note that you don't need to call .toString() on g1 and g2 , because concatenating them with the string ":" implicitly converts the numbers to strings.

You could extend this to make one or the other player more likely to win (simulating different skill levels) by changing if (Math.random() < 0.5) to use a variable instead of hardcoding 0.5 .

PS I couldn't be bothered to look up the rules for tennis to confirm how you win a set, but my vague recollection is that you have to get at least 6 games and be at least two games ahead of the other player, so that's what the code I've shown tries to implement...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM