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

Rock-Paper-Scissors Implement Rock-Paper-Scissors such that the user can play the computer in a best-of series! The...
Rock-Paper-Scissors Implement Rock-Paper-Scissors such that the user can play the computer in a best-of series! The user inputs the number of rounds which should be positive and odd. This is a free form assignment but structure your code similar to the test cases provided. USING MATLAB ONLY!!! Program Inputs • How many rounds would you like to play (odd rounds only)?: XXX – XXX should be positive and odd. Restart the game if the input is not positive or odd....
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a random choice (Rock, Paper or Scissors) then ask the user to choose Rock, Paper or Scissors. After that the program will display its choice and a message showing if the player won, lost or tied. Next, the program should prompt the user to play again or not. Once the player selects to stop playing the game, the program should print the number of wins,...
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...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
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...
Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will...
Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will enter an integer to determine whether they use rock, paper or scissors. Player 2 will also enter an integer to determine whether they use rock, paper or scissors. Use named constants for rock, paper and scissors and set their values as shown below. Use if-else statements to determine the results of the game. Use the named constants to compare with the player 1 and...
1.Create full program in Java that will simulate a Rock Paper Scissors Game You will create...
1.Create full program in Java that will simulate a Rock Paper Scissors Game You will create the code so that when the program is run the user will see in the console the following: We are going to play rock paper scissors. Please choose 1 for Rock 2 for Paper or 3 for scissors. The program will have your choice which is the integer-valued typed in by the user and compChoice which will be the randomly generated value of either...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT