Last active
December 21, 2015 22:18
-
-
Save jlbruno/6373997 to your computer and use it in GitHub Desktop.
JS script to play "Rock, paper, scissors, lizard, spock"
This file contains 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 get_random(topLim) { | |
return Math.round(Math.random()*topLim); | |
} | |
var RPSLS = function() { | |
var ret = {}; | |
var map = { | |
"rock" : { | |
"scissors" : "crushes", | |
"lizard" : "crushes" | |
}, | |
"scissors" : { | |
"paper" : "cuts", | |
"lizard" : "decapitates" | |
}, | |
"spock" : { | |
"scissors" : "smashes", | |
"rock" : "vaporizes" | |
}, | |
"lizard" : { | |
"spock" : "poisons", | |
"paper" : "eats" | |
}, | |
"paper" : { | |
"spock" : "disproves", | |
"rock" : "covers" | |
} | |
} | |
// TODO: Validate this against the map object | |
var userChoice = prompt("Rock, paper, scissors, lizard, spock?"); | |
// Randomly selecting correct value for computerChoice | |
var computerChoice = get_random(Object.keys(map).length - 1); | |
computerChoice = Object.keys(map)[computerChoice]; | |
// Function that compares the two values | |
ret.compare = function() { | |
if ( userChoice === computerChoice ) { | |
return "The result is a tie!"; | |
} | |
// if the userChoice object contains the computerChoice, the user wins | |
if ( map[userChoice].hasOwnProperty(computerChoice) ) { | |
return "You win! " + userChoice + " " + map[userChoice][computerChoice] + " " + computerChoice; | |
} else { | |
return "You lose! " + computerChoice + " " + map[computerChoice][userChoice] + " " + userChoice; | |
} | |
} | |
return ret; | |
}(); | |
RPSLS.compare(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment