Advertisement
yash_tomar_

jsRps

Mar 8th, 2022
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const rock = document.getElementById('rock');
  2. const paper = document.getElementById('paper');
  3. const scissors = document.getElementById('scissors');
  4.  
  5.  
  6. const reload1 = document.getElementById('reload');
  7. reload1.addEventListener('click',(e) => {
  8.     location.reload();
  9. })
  10. let gameRound = 0;
  11. let ComputerScore = 0;
  12. let PlayerScore = 0;
  13. let tie = 0;
  14. const computerChoices = [];
  15. const array1 = ['rock', 'paper', 'scissors'];
  16.  
  17. //function for computer to guess
  18. function computerPlay(array1) {
  19.     for( i = 0 ; i < 5 ; i ++ ){
  20.    
  21.     let randomElement = Math.floor(Math.random() * array1.length);
  22.  
  23.     computerChoices.push(array1[randomElement]);
  24. }}
  25. computerPlay(array1);
  26. computerChoices.forEach(element => {
  27.     console.log(element);
  28.    
  29. });
  30.  
  31. //function to get the player choice
  32. function playerChoice(){
  33. for ( let i = 0 ; i < 5 ; i++ )
  34. {
  35. rock.addEventListener('click',function(){
  36.     playRound('rock',computerChoices);
  37. });
  38.  
  39. paper.addEventListener('click',function(){
  40.     playRound('paper',computerChoices);
  41. });
  42.  
  43. scissors.addEventListener('click',function(){
  44.     playRound('scissors',computerChoices);
  45. });
  46. }
  47. }
  48.  
  49. // const computerSelection = computerPlay();
  50. function playRound(string1,computerChoices){
  51.     for ( j = 0 ; j < 5 ; j++ ){
  52.     switch (string1) {
  53.         case 'rock': if (computerChoices[j] == 'rock') {
  54.                  document.getElementById('tie').textContent= ++tie;
  55.                  Output();
  56.             }
  57.                     else if (computerChoices[j] == 'paper') {
  58.                      document.getElementById('computerScore').textContent= ++ComputerScore;
  59.                      Output();
  60.             }
  61.                     else {
  62.                         document.getElementById('playerScore').textContent= ++PlayerScore;
  63.                         Output();
  64.             }
  65.             break;
  66.         case 'paper': if(computerChoices[j] == 'rock'){
  67.             document.getElementById('playerScore').textContent= ++PlayerScore;
  68.             Output();
  69.             }
  70.                     else if (computerChoices[j] == 'paper'){
  71.                         document.getElementById('tie').textContent= ++tie;
  72.                         Output();
  73.             }
  74.                     else {
  75.                         document.getElementById('computerScore').textContent= ++ComputerScore;
  76.                         Output();
  77.             }
  78.             break;
  79.         case 'scissors': if(computerChoices[j] == 'rock'){
  80.             document.getElementById('computerScore').textContent= ++ComputerScore;
  81.             Output();
  82.             }
  83.                           else if (computerChoices[j] == 'paper'){
  84.                             document.getElementById('playerScore').textContent= ++PlayerScore;
  85.                             Output();
  86.             }
  87.                          else {
  88.                             document.getElementById('tie').textContent= ++tie;
  89.                             Output();
  90.             }
  91.             break;
  92.         default : document.getElementById('result').textContent="error, please try again later";
  93.     }
  94. }}
  95. // console.log("the computer had",computerSelection);
  96.  
  97. function Output(){
  98.     if(ComputerScore + PlayerScore + tie == 5){
  99.         if(ComputerScore > PlayerScore){
  100.             document.getElementById('output').textContent += "The computer wins";
  101.         }
  102.         else if (PlayerScore > ComputerScore){
  103.             document.getElementById('output').textContent += "you win";
  104.         }
  105.         else {
  106.             document.getElementById('output').textContent += "the game ends with a tie";
  107.         }
  108.     }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement