Last active
October 17, 2015 21:10
-
-
Save sgnl/6aa3b188bbabc8c845dd to your computer and use it in GitHub Desktop.
functional RPS? for the lulz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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