JS implements the rock-paper-scissors game

JS implements the rock-paper-scissors game

This article example shares the specific code of JS to implement the rock-paper-scissors game for your reference. The specific content is as follows

1. Simple version of rock-paper-scissors game

Write a rock-paper-scissors game between the user and the computer. The user inputs scissors, rock, or paper, and the input is compared with the computer's punch to determine the winner.

analyze:

1. First, use the prompt() method to create a user input box;
2. The core is to use the Math.random() function, which takes a random number between [0,1). This function can be used to make the computer punch randomly;
3. Use if-else statements to determine the various results that may occur and make decisions;

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: Rock 3: Paper');
var b = Math.random();
if (b < 0.3) {
    if (a == 1) {
        alert('The computer made the scissors, you made the scissors, it's a tie');
    } else if (a == 2) {
        alert('The computer played scissors, you played rock, you lose');
    } else {
        alert('The computer played scissors, you played cloth, you win');
    }
} else if (b < 0.6) {
    if (a == 1) {
        alert('The computer played rock, you played scissors, you lose');
    } else if (a == 2) {
        alert('The computer's stone and your stone are tied');
    } else {
        alert('The computer played rock, you played paper, you win');
    }
} else {
    if (a == 1) {
        alert('The computer played paper, you played scissors, you win');
    } else if (a == 2) {
        alert('The computer played paper, you played stone, you lost');
    } else {
        alert('The computer made the cloth, you made the cloth, it's a tie');
    }
}

2. Advanced version of rock-paper-scissors game

Record the system and player scores, the winner will get 1 point, and the draw and loser will not get any points.

analyze:

1. Two more variables need to be added to the original code, one to store the user's total score and the other to store the computer's total score;
2. A for loop is needed to limit the number of games;
3. Use the alert() statement to output the result score;

The specific code is as follows:

var sum=0;//people's scorevar snm=0;//computer's scorefor(var i=0;i<3;i++){
    var a=prompt('Please enter 1, scissors 2, rock 3, cloth');
    var b=Math.random();
    if (b < 0.3) {
        if (a == 1) {
            alert('The computer made the scissors, you made the scissors, it's a tie');
        } else if (a == 2) {
            snm++;
            alert('The computer played scissors, you played rock, you lose');
        } else {
            sum++;
            alert('The computer played scissors, you played cloth, you win');
        }
    } else if (b < 0.6) {
        if (a == 1) {
            snm++;
            alert('The computer played rock, you played scissors, you lose');
        } else if (a == 2) {
            alert('The computer's stone and your stone are tied');
        } else {
            sum++;
            alert('The computer played rock, you played paper, you win');
        }
    } else {
        if (a == 1) {
            sum++;
            alert('The computer played paper, you played scissors, you win');
        } else if (a == 2) {
            snm++;
            alert('The computer played paper, you played stone, you lost');
        } else {
            alert('The computer made the cloth, you made the cloth, it's a tie');
        }
    }
}
alert('computer'+snm +'your score'+sum);

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Writing a rock-paper-scissors game in JavaScript
  • JavaScript based on object-oriented implementation of the rock-paper-scissors game
  • js implements the rock-paper-scissors game
  • JavaScript implementation of the rock-paper-scissors game source code sharing
  • HTML+JS to implement the sample code of rock-paper-scissors game

<<:  Solve the grouping error Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated in MySQL versions greater than 5.7

>>:  Nginx implements dynamic and static separation example explanation

Recommend

How to use tcpdump to capture packets in Linux system

Let me look at the example code first: 1. Common ...

Native JavaScript to achieve skinning

The specific code for implementing skinning with ...

What is ZFS? Reasons to use ZFS and its features

History of ZFS The Z File System (ZFS) was develo...

Solution for FileZilla 425 Unable to connect to FTP (Alibaba Cloud Server)

Alibaba Cloud Server cannot connect to FTP FileZi...

How to understand Vue's simple state management store mode

Table of contents Overview 1. Define store.js 2. ...

Pure HTML+CSS to achieve Element loading effect

This is the effect of the Element UI loading comp...

Introduction to the use of HTML element noscript

noscript definition and usage The noscript elemen...

Knowledge about MySQL Memory storage engine

Knowledge points about Memory storage engine The ...

Web Design Tutorial (6): Keep your passion for design

<br />Previous article: Web Design Tutorial ...

How to use physics engine joints in CocosCreator

Table of contents mousejoint mouse joint distance...

Use standard dl, dt, dd tags to discard table lists

Now, more and more front-end developers are starti...

Docker builds kubectl image implementation steps

If the program service is deployed using k8s inte...