Skip to content

Instantly share code, notes, and snippets.

@wayspurrchen
Created October 2, 2014 01:15
Show Gist options
  • Save wayspurrchen/ae7dbb4819c925a40f63 to your computer and use it in GitHub Desktop.
Save wayspurrchen/ae7dbb4819c925a40f63 to your computer and use it in GitHub Desktop.
RPS Code
<!DOCTYPE html>
<html>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
</html>
<body>
<button id="scissors">scissors</button>
<button id="rock">rock</button>
<button id="paper">paper</button>
<h1 id="decision">OUTCOME GOES HERE</h1>
<script>
$('#scissors').on('click', function(){
var result = compare('scissors', computerChoice());
$("#decision").html(result);
});
$('#rock').on('click', function() {
var result = compare('rock', computerChoice());
$("#decision").html(result);
});
$('#paper').on('click', function() {
var result = compare('paper', computerChoice());
$("#decision").html(result);
});
var compare = function(me, opponent) {
if (me === 'rock') {
if (opponent === 'rock') {
return 'Tie!';
} else if (opponent === 'paper') {
return 'My rock lost to paper :(';
} else if (opponent == 'scissors') {
return 'My rock crushed scissors! :D';
}
} else if (me === 'paper') {
if (opponent === 'rock') {
return 'My paper wrapped up rock! :D';
} else if (opponent === 'paper') {
return 'Tie!';
} else if (opponent == 'scissors') {
return 'My paper was cut by scissors :(';
}
} else if (me === 'scissors') {
if (opponent === 'rock') {
return 'My scissors were crushed by rock :(';
} else if (opponent === 'paper') {
return 'My scissors cut up paper! :D';
} else if (opponent == 'scissors') {
return 'Tie!';
}
} else {
return " That doesn't make any sense! "
}
};
var computerChoice = function() {
var random = Math.random();
if (random < 0.333) {
return 'rock';
} else if (random < 0.67777) {
return 'paper';
} else {
return 'scissors';
}
};
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment