JS realizes guessing game

  • 2021-11-10 08:40:00
  • OfStack

This article example for everyone to share the JS guessing game to achieve specific code, for your reference, the specific content is as follows

1. Simple version of guessing game

Write a game of guessing boxing for users and computers. Users input scissors, Stone or cloth, and compare with punches of computers to judge the outcome.

Analysis:

1. First of all, a user input box must be established by prompt () method;
2. The core is to use the function Math. random (). The function is to take random numbers between [0, 1]. You can use this function to make the computer punch randomly;
3. Use if-else statements to judge various results and make judgments;

The specific code is as follows:


/**
 * a Is the content entered by the user 
 * b Is the random content of the computer 
 */
var a = prompt(' Please enter 1: Scissors  2: Stone  3: Cloth ');
var b = Math.random();
if (b < 0.3) {
    if (a == 1) {
        alert(' Scissors from the computer, scissors from you, draw ');
    } else if (a == 2) {
        alert(' Scissors from the computer, Stone from you, and you lose ');
    } else {
        alert(' Scissors from the computer, cloth from you, you win ');
    }
} else if (b < 0.6) {
    if (a == 1) {
        alert(' The Stone from the computer, the scissors from you, and you lost ');
    } else if (a == 2) {
        alert(' Stone from computer, Stone from you, draw ');
    } else {
        alert(' The Stone from the computer, the cloth from you, and you win ');
    }
} else {
    if (a == 1) {
        alert(' The cloth from the computer, the scissors from you, you win ');
    } else if (a == 2) {
        alert(' The cloth from the computer, the Stone from you, and you lose ');
    } else {
        alert(' The cloth from the computer, the cloth from you, draw ');
    }
}

2. Advanced version of guessing game

Record the scores of the system and players, with 1 point added to the winner, and no points scored for the draw and loser

Analysis:

1. On the basis of the original code, two variables need to be added, one is used to store the total score of users and the other is used to store the total score of computers;
2. for cycle is needed to limit the number of games;
3. Use alert () statement to output the result score;

The specific code is as follows:


var sum=0;// Person's score 
var snm=0;// Computer score 
for(var i=0;i<3;i++){
    var a=prompt(' Please enter 1 , scissors 2 Stone 3 , cloth ');
    var b=Math.random();
    if (b < 0.3) {
        if (a == 1) {
            alert(' Scissors from the computer, scissors from you, draw ');
        } else if (a == 2) {
            snm++;
            alert(' Scissors from the computer, Stone from you, and you lose ');
        } else {
            sum++;
            alert(' Scissors from the computer, cloth from you, you win ');
        }
    } else if (b < 0.6) {
        if (a == 1) {
            snm++;
            alert(' The Stone from the computer, the scissors from you, and you lost ');
        } else if (a == 2) {
            alert(' Stone from computer, Stone from you, draw ');
        } else {
            sum++;
            alert(' The Stone from the computer, the cloth from you, and you win ');
        }
    } else {
        if (a == 1) {
            sum++;
            alert(' The cloth from the computer, the scissors from you, you win ');
        } else if (a == 2) {
            snm++;
            alert(' The cloth from the computer, the Stone from you, and you lose ');
        } else {
            alert(' The cloth from the computer, the cloth from you, draw ');
        }
    }
}
alert(' Computer '+snm +' Your score '+sum);

Related articles: