Why introduce something as trivial as rock paper scissors? Let me do something more interesting. Something like Rock-paper-scissors-lizard-Spock .
:
var gestures = ["rock", "paper", "scissors", "lizard", "spock"];
:
var rules = {
rock: { scissors: "breaks", lizard: "crushes" },
paper: { rock: "covers", spock: "disproves" },
scissors: { paper: "cuts", lizard: "decapitates" },
lizard: { paper: "eats", spock: "poisons" },
spock: { scissors: "smashes", rock: "vaporizes" }
};
, :
function play(index) {
var your = gestures[index];
var mine = gestures[Math.floor(5 * Math.random())];
if (your === mine) return alert("Draw. We both played " + your + ".");
var win = rules[your].hasOwnProperty(mine);
var result = win ? "win" : "lose";
var a = win ? your : mine;
var b = win ? mine : your;
alert("You " + result + ": " + a + " " + rules[a][b] + " " + b + ".");
}
. :
var gestures = ["rock", "paper", "scissors", "lizard", "spock"];
var rules = {
rock: { scissors: "breaks", lizard: "crushes" },
paper: { rock: "covers", spock: "disproves" },
scissors: { paper: "cuts", lizard: "decapitates" },
lizard: { paper: "eats", spock: "poisons" },
spock: { scissors: "smashes", rock: "vaporizes" }
};
function play(index) {
var your = gestures[index];
var mine = gestures[Math.floor(5 * Math.random())];
if (your === mine) return alert("Draw. We both played " + your + ".");
var win = rules[your].hasOwnProperty(mine);
var result = win ? "win" : "lose";
var a = win ? your : mine;
var b = win ? mine : your;
alert("You " + result + ": " + a + " " + rules[a][b] + " " + b + ".");
}
<button onclick="play(0);">Rock</button>
<button onclick="play(1);">Paper</button>
<button onclick="play(2);">Scissors</button>
<button onclick="play(3);">Lizard</button>
<button onclick="play(4);">Spock</button>
Hide result, - , , .