Question

In: Computer Science

Rock Paper Scissors Lizards Spock IN JAVASCRIPT! Purpose The purpose of this program is for students...

Rock Paper Scissors Lizards Spock

IN JAVASCRIPT!

Purpose

The purpose of this program is for students to demonstrate their understanding of selection. The program can be developed using if-statements with no logical operators. Hint: Use nested-if statements to help you construct the logic of the program.

Rules and Structure of Program

Rock Paper Scissors Lizard Spock is similar to the traditional two-player game Rock-Paper-Scissors with two additional game items to select. You will develop a program that replicates this game. The two players of the game are the user and the computer. Below is an image that defines the rules of the game.

The program starts by prompting the user to enter a number (0 - Rock, 1 - Paper, 2 - Scissors, 3 - Lizards, and 4 - Spock). The computer will randomize a number that represents one of these items. It will use the same numbering order to define the game item it selects. Include this line of code in your program to generate a random number between zero and four.

int com = (int)(Math.random() * 5); // generates a random number for the computer to represent the game item it selects

The program then compares the game item the user and computer selected and displays the winner of the game. If the computer and user select the same game item, there is no winner, and the round is a tie.

There is a likeliness that your program will not produce the same output for the first run because the computer generates a random number during each program execution. The input of your program should be identical to the sample input.

Enter a number (0 - Rock, 1 - Paper, 2 - Scissors, 3 - Lizard, 4 - Spock):

The output of your program should follow the same structure as the sample output.

The user selected [game item].
The computer selected [game item].
[game item] [action] [game item] // Only if the game items selected are different.
The [user/computer] is the winner. 

The action performed can be found in the image above that describes the rules.

Sample Input and Output

Sample Input
Enter a number (0 - Rock, 1 - Paper, 2 - Scissors, 3 - Lizard, 4 - Spock): 4

Sample Output
The user selected Spock.
The computer selected Lizard.
Lizard poisons Spock.
The computer is the winner.
Sample Input
Enter a number (0 - Rock, 1 - Paper, 2 - Scissors, 3 - Lizard, 4 - Spock): 0

Sample Output
The user selected Rock.
The computer selected Scissors.
Rock crushes Scissors.
The user is the winner.
Sample Input
Enter a number (0 - Rock, 1 - Paper, 2 - Scissors, 3 - Lizard, 4 - Spock): 2

Sample Output
The user selected Scissors.
The computer selected Lizard. 
Scissors decapitates the lizard.
The user is the winner.
Sample Input
Enter a number (0 - Rock, 1 - Paper, 2 - Scissors, 3 - Lizard, 4 - Spock): 3

Sample Output
The user selected Lizards.
The computer selected Lizards.
There is no winner.

Solutions

Expert Solution

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Rock Paper Scissors Lizards Spock</title>
  </head>
  <body>
    <input
      id="userInput"
      placeholder="Enter a number (0 - Rock, 1 - Paper, 2 - Scissors, 3 - Lizard, 4 - Spock)"
      onkeypress="handleKeyPress(event)"
      style="min-width: 420px"
    /><br />
    <button onclick="compareResult()">Play</button>
    <script>
      const handleKeyPress = (event) => {
        if (event.key === "Enter") compareResult();
      };

      const compareResult = () => {
        let computerChoice = parseInt(Math.random() * 5),
          userChoice = document.getElementById("userInput").value,
          items = ["Rock", "Paper", "Scissors", "Lizard", "Spock"];
        if (!(userChoice >= 0 && userChoice <= 5)) alert("Invalid Input");
        else {
          userChoice = parseInt(userChoice);
          if (userChoice === computerChoice) {
            alert(
              `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\nThere is no winner.`
            );
          }

          //If the user chose rock...
          else if (userChoice === 0) {
            if (computerChoice === 2) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[userChoice]} defeats ${items[computerChoice]}.\nThe user is the winner.`
              );
            } else if (computerChoice === 1) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[computerChoice]} defeats ${items[userChoice]}.\nThe computer is the winner.`
              );
            } else if (computerChoice === 3) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[userChoice]} defeats ${items[computerChoice]}.\nThe user is the winner.`
              );
            } else {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[computerChoice]} defeats ${items[userChoice]}.\nThe computer is the winner.`
              );
            }
          }

          //If the user chose paper...
          else if (userChoice === 1) {
            if (computerChoice === 2) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[computerChoice]} defeats ${items[userChoice]}.\nThe computer is the winner.`
              );
            } else if (computerChoice === 0) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[userChoice]} defeats ${items[computerChoice]}.\nThe user is the winner.`
              );
            } else if (computerChoice === 3) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[computerChoice]} defeats ${items[userChoice]}.\nThe computer is the winner.`
              );
            } else {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[userChoice]} defeats ${items[computerChoice]}.\nThe user is the winner.`
              );
            }
          }

          //If the user chose scissors...
          else if (userChoice === 2) {
            if (computerChoice === 1) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[userChoice]} defeats ${items[computerChoice]}.\nThe user is the winner.`
              );
            } else if (computerChoice === 0) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[computerChoice]} defeats ${items[userChoice]}.\nThe computer is the winner.`
              );
            } else if (computerChoice === 3) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[userChoice]} defeats ${items[computerChoice]}.\nThe user is the winner.`
              );
            } else {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[computerChoice]} defeats ${items[userChoice]}.\nThe computer is the winner.`
              );
            }
          }

          //If the user chose lizard...
          else if (userChoice === 3) {
            if (computerChoice === 2) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[computerChoice]} defeats ${items[userChoice]}.\nThe computer is the winner.`
              );
            } else if (computerChoice === 0) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[computerChoice]} defeats ${items[userChoice]}.\nThe computer is the winner.`
              );
            } else if (computerChoice === 1) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[userChoice]} defeats ${items[computerChoice]}.\nThe user is the winner.`
              );
            } else {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[userChoice]} defeats ${items[computerChoice]}.\nThe user is the winner.`
              );
            }
          }

          //If the user chose spock...
          else if (userChoice === 4) {
            if (computerChoice === 2) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[userChoice]} defeats ${items[computerChoice]}.\nThe user is the winner.`
              );
            } else if (computerChoice === 0) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[userChoice]} defeats ${items[computerChoice]}.\nThe user is the winner.`
              );
            } else if (computerChoice === 3) {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[computerChoice]} defeats ${items[userChoice]}.\nThe computer is the winner.`
              );
            } else {
              alert(
                `The user selected ${items[userChoice]}.\nThe computer selected ${items[computerChoice]}.\n${items[computerChoice]} defeats ${items[userChoice]}.\nThe computer is the winner.`
              );
            }
          }
        }
        document.getElementById("userInput").value = "";
      };
    </script>
  </body>
</html>

EXPLANATION: Here, I have created the Rock Paper Scissors Lizards Spock game in Javascript. The HTML is there to take inputs from the user and display alerts.The HTML takes input from the user and uses Javascript for processing the input and javascript then displays the appropriate alerts. It also checks for invalid inpuits from the user. The user can submit their input by clicking on the play button or by pressing "ENTER" on the keyboard.

THANKS,

PLEASE UPVOTE THE ANSWER.


Related Solutions

One file java program that will simulate a game of Rock, Paper, Scissors. One of the...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the two players will be the computer. The program will start by asking how many winning rounds are needed to win the game. Each round will consist of you asking the user to pick between rock, paper, and scissors. Internally you will get the computers choice by using a random number generator. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. You will report...
Develop a C++ program that plays out a round of Rock, Paper, Scissors using Functional Programming...
Develop a C++ program that plays out a round of Rock, Paper, Scissors using Functional Programming 1) Develop a function that prompts the user to enter their choice (1=Rock 2=Paper 3=Scissors) Return either a 1, 2, or 3 depending on the value the user has entered Do not continue the program until the user has entered a valid choice of 1, 2, 3 2) Develop a function that generates the computer player's choice Return either a 1, 2, or 3...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper,...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: • Rock beats scissors, because a rock can break a pair of scissors. • Scissors beats paper, because scissors can cut paper. • Paper beats rock, because a piece of paper can cover a rock. Create a game...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper,...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: • Rock beats scissors, because a rock can break a pair of scissors. • Scissors beats paper, because scissors can cut paper. • Paper beats rock, because a piece of paper can cover a rock. Create a game...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play the game of rock, paper, scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the input from the user is always valid (so no need to check), that is it contains either one of `R`, `P`, or `S` as a single character, or has matching parenthesis, like, `(S&P)` or `((R&P)&S)`, and the `&` character. So for example, the user inputs `(P&R)` and the program will output `P` since paper beats rock. Or if the user inputs `((S&R)&(S&S))` the output...
Solve the scissors, paper, rock game. This game is well known in many parts of the...
Solve the scissors, paper, rock game. This game is well known in many parts of the world. Two players simultaneously present a hand in one of three positions: an open hand (paper), a closed fist (rock), or two open fingers (scissors). The payoff is 1 unit according to the rule “Paper covers rock, rock breaks scissors, and scissors cut paper.” If both players present the same form, the payoff is 0. Set up the payoff matrix for the game and...
Please write a scheme code for the rock paper scissors game between a player and the...
Please write a scheme code for the rock paper scissors game between a player and the computer (AI). In scheme programming language please.
Many of you probably played the game “Rock, Paper, Scissors” as a child. Consider the following...
Many of you probably played the game “Rock, Paper, Scissors” as a child. Consider the following variation of that game. Instead of two players, suppose three players play this game, and let us call these players A, B, and C. Each player selects one of these three items—Rock, Paper, or Scissors—independent of each other. Player A will win the game if all three players select the same item, for example, rock. Player B will win the game if exactly two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT