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 gameWrite 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; 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 gameRecord 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; 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:
|
>>: Nginx implements dynamic and static separation example explanation
Let me look at the example code first: 1. Common ...
Effect If you use it, please optimize the code an...
The specific code for implementing skinning with ...
I am currently learning about Redis and container...
History of ZFS The Z File System (ZFS) was develo...
Shopify Plus is the enterprise version of the e-c...
Alibaba Cloud Server cannot connect to FTP FileZi...
Table of contents Overview 1. Define store.js 2. ...
This is the effect of the Element UI loading comp...
noscript definition and usage The noscript elemen...
Knowledge points about Memory storage engine The ...
<br />Previous article: Web Design Tutorial ...
Table of contents mousejoint mouse joint distance...
Now, more and more front-end developers are starti...
If the program service is deployed using k8s inte...