Skip to Content

The Rock-Paper-Scissors Game

rock_paper_scissors.js
/* The Rock-Paper-Scissors game. */ // START PROGRAM const call_options = ["Rock","Paper","Scissors"]; const player1_name = "Adam"; const player2_name = "Eve"; let player1_score = 0; let player2_score = 0; const number_of_rounds = 10; let no_score_count = 0; function generateRandomNumber(min, max){ let random_number = Math.floor(Math.random() * (max - min + 1) ) + min; return random_number; } // The game logic begins here. // In this version we are not accepting any user input. // We will be running the game for ten rounds with random calls for two // fictitious players named Adam and Eve. let index = 1; for(index; index <= number_of_rounds; index++){ let player1_call_number = generateRandomNumber(1, 3); let player2_call_number = generateRandomNumber(1, 3); let player1_call_value = call_options[player1_call_number - 1]; let player2_call_value = call_options[player2_call_number - 1]; console.log("Round " + index + ": " + player1_name + ": " + player1_call_value + ", " + player2_name + ": " + player2_call_value + ". "); if(player1_call_value === "Rock" && player2_call_value === "Paper"){ player2_score++; console.log(player2_name + " won this round!"); } else if(player1_call_value === "Rock" && player2_call_value === "Scissors"){ player1_score++; console.log(player1_name + " won this round!"); } else if(player1_call_value === "Paper" && player2_call_value === "Rock"){ player1_score++; console.log(player1_name + " won this round!"); } else if(player1_call_value === "Paper" && player2_call_value === "Scissors"){ player2_score++; console.log(player2_name + " won this round!"); } else if(player1_call_value === "Scissors" && player2_call_value === "Rock"){ player2_score++; console.log(player2_name + " won this round!"); } else if(player1_call_value === "Scissors" && player2_call_value === "Paper"){ player1_score++; console.log(player1_name + " won this round!"); } else{ no_score_count++; console.log("Draw!"); } } // Finally, print the scores and declare a winner. console.log("FINAL SCORE: " + player1_name + " won: " + player1_score + ", " + player2_name + " won: " + player2_score + ", " + "Draws: " + no_score_count); if(player1_score > player2_score){ console.log(player1_name + " wins the game!"); } else if(player1_score < player2_score){ console.log(player2_name + " wins the game!"); } else{ console.log("It's a draw!"); } // END PROGRAM

Codepen Video