Skip to content

Instantly share code, notes, and snippets.

@sgnl
Last active October 17, 2015 21:10
Show Gist options
  • Save sgnl/6aa3b188bbabc8c845dd to your computer and use it in GitHub Desktop.
Save sgnl/6aa3b188bbabc8c845dd to your computer and use it in GitHub Desktop.
functional RPS? for the lulz
function init(user, computer){
if (user === computer) {
return {
playerChoice: user,
computerChoice: computer,
text: "It's a TIE"
};
}
if (user === "r" && computer === "s" || user === "p" && computer === "r" || user === "s" && computer === "p") {
return {
playerChoice: user,
computerChoice: computer,
text: "Player Wins"
};
}
return {
playerChoice: user,
computerChoice: computer,
text: "Computer Wins"
};
}
function getComputerChoice() {
switch(Math.floor(Math.random() * 2)) {
case 0:
return "r";
case 1:
return "p";
default:
return "s";
}
}
function displayResults(results) {
if (!results) {
displayResults(
init( prompt("'R', 'P', or 'S'").toLowerCase(), getComputerChoice())
);
} else {
if (confirm("User Chose: " + results.playerChoice + ". Computer Chose: " + results.computerChoice + ". " + results.text + "\n Play Again?")) {
displayResults();
}
}
}
displayResults();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment