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

Example of using CASE WHEN in MySQL sorting

Preface In a previous project, the CASE WHEN sort...

WeChat Mini Program Lottery Number Generator

This article shares the specific code of the WeCh...

Vue Basics Listener Detailed Explanation

Table of contents What is a listener in vue Usage...

How to use JS to implement waterfall layout of web pages

Table of contents Preface: What is waterfall layo...

Implementation of Docker Compose multi-container deployment

Table of contents 1. WordPress deployment 1. Prep...

Using loops in awk

Let's learn about different types of loops th...

Analysis of uniapp entry-level nvue climbing pit record

Table of contents Preface Hello World image Set b...

Videojs+swiper realizes Taobao product details carousel

This article shares the specific code of videojs+...

js to write the carousel effect

This article shares the specific code of js to ac...

How to set process.env.NODE_ENV production environment mode

Before I start, let me emphasize that process.env...

Introduction and use of triggers and cursors in MySQL

Trigger Introduction A trigger is a special store...

Mac VMware Fusion CentOS7 configuration static IP tutorial diagram

Table of contents Install CentOS7 Configuring Sta...

JavaScript canvas to achieve raindrop effects

This article example shares the specific code of ...

Vue implements setting multiple countdowns at the same time

This article example shares the specific code of ...