Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More efficient choice comparison for Rock Paper Scissors

Tags:

javascript

This is an ongoing school project that I would like to improve. The point is to make the code as efficient (or short) as possible. I would like to reduce it by finding an alternative to all the else ifs when comparing the computer's choice with the user's choice.

Here is the code:

let weapons = ["Rock", "Paper", "Scissors"];
let random = Math.floor(Math.random()*3);
let chosenOne = weapons[random];

let rps = prompt("Welcome to Rock, Paper, Scissors. Would you like to play?" 
+ '\n' + "If you do, enter number 1." + '\n' + "If you don't, enter number 
2.");

if (rps === "1") {
    alert("Remember:" + '\n' + " - Rock beats the scissors" + '\n' + " - 
    Paper beats the rock" + '\n' + " - The scissors cut the paper");

let weapon = prompt("Make your choice:" + '\n' + "Rock, Paper, Scissors");
    weapon = weapon.charAt(0).toUpperCase() + weapon.slice(1).toLowerCase();
    alert("You chose: " + weapon + '\n' + "The computer chose: " + 
    chosenOne);
if (weapon === chosenOne) {
        alert("It's a tie! Try again to win!");
    } else if (weapon === "Rock" && chosenOne === "Paper") {
        alert("You lost! Paper beats the rock.");
    } else if (weapon === "Paper" && chosenOne === "Scissors") {
        alert("You lost! The scissors cut the paper.");
    } else if (weapon === "Scissors" && chosenOne === "Rock") {
        alert("You lost! The rock beats the scissors.");
    } else if (weapon === "Scissors" && chosenOne === "Paper") {
        alert("You won! Scissors cut the paper.");
    } else if (weapon === "Paper" && chosenOne === "Rock") {
        alert("You won! Paper beats the rock.");
    } else if (weapon === "Rock" && chosenOne === "Scissors") {
        alert("You won! The rock beats the scissors.");
    }
} else if (rps === "2") {
    alert("Thanks for visiting! See you later.");
} else if (rps !== "1" || rps !== "2") {
    alert("Invalid option. Closing game.");
}

I have thought about using switch statements, but since we are still beginners, I haven't grasped the subject fully. Any help is appreciated.

like image 497
TBG Avatar asked Dec 11 '18 19:12

TBG


People also ask

What is the best choice for rock-paper-scissors?

Because scissors is the statistically least often thrown move, and because rock is the most often thrown move, paper is the best way to go. Paper will beat rock, which is the most commonly thrown move. Scissors can beat paper, but because it's the least often thrown move the chances of losing are much less likely.

What is the safest choice in rock-paper-scissors?

And finally, when all else is lost, your safest bet is paper because it statistically only gets delivered 29.6 percent of the time, very slightly less than the 33.33 percent you would expect.

What is Nash equilibrium for rock-paper-scissors?

Theorem 1 The game of Rock-Paper-Scissors has a unique mixed Nash equilibrium. In this equilibium, both players play the mixed strategy that puts equal probabilities on all three actions.


1 Answers

You can define an object that define if your move is weak or strong against another. Example:

const myChoice = 'Rock'
const enemyChoice = 'Scissors' 

const weapons = {
   Rock: {weakTo: 'Paper', strongTo: 'Scissors'},
   Paper: {weakTo: 'Scissors', strongTo: 'Rock'},
   Scissors: {weakTo: 'Rock', strongTo: 'Paper'}
}

if (weapons[myChoice].strongTo === enemyChoice) {
    // I won
    return;
}

if (weapons[myChoice].weakTo === enemyChoice) {
    // I Lost
    return;
}

// tie
like image 69
Federkun Avatar answered Sep 20 '22 17:09

Federkun