In: Computer Science
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.
<!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.