<html> <head><title>Rock Paper Scissors Lizard Spock</title></head>
<body onload=updateStats()>
<script>
var WinList = {
Rock: {Scissors:'Crushes', Lizard:'Crushes'},
Paper: {Rock:'Covers', Spock:'Disproves'},
Scissors: {Paper:'Cuts', Lizard:'Decapitates'},
Lizard: {Spock:'Poisons', Paper:'Eats'},
Spock: {Rock:'Vaporizes', Scissors:'Smashes'}
}
var Stats = { roundNo:0, cpuWins:0, humanWins:0, ties:0 };
function evaluateMove() {
//process the human's move and make a CPU move
var move=document.getElementById('moveList').value;
if (move == 'Random'){ move = rndMove(); }
var cpuMove = rndMove();
document.getElementById('move').innerHTML = move;
document.getElementById('cpuMove').innerHTML = cpuMove;
var winner = (WinList[move][cpuMove]) ? move : cpuMove;
var loser = (winner == move) ? cpuMove : move;
var result = (winner == move) ? 'You Win! ' : 'Computer Wins! ';
result += winner + ' ' + WinList[winner][loser] + ' ' + loser + '.';
if (move == cpuMove) { result = 'Tie.'; }
document.getElementById('winner').innerHTML = result;
updateStats(result)
}
function rndMove() {
var moveList = new Array('Rock','Paper','Scissors','Lizard','Spock');
return moveList[Math.floor((Math.random()*5))];
}
function updateStats(result) {
if (result) {
Stats.roundNo++;
if (result.charAt(0)=='C') { Stats.cpuWins++; }
if (result.charAt(0)=='Y') { Stats.humanWins++; }
if (result.charAt(0)=='T') { Stats.ties++; }
}
document.getElementById('roundNo').innerHTML = Stats.roundNo;
document.getElementById('humanWins').innerHTML = formatStats('humanWins');
document.getElementById('cpuWins').innerHTML = formatStats('cpuWins');
document.getElementById('ties').innerHTML = formatStats('ties');
}
function formatStats(stat) {
//helper function, formats stats w/ percentage
if (Stats.roundNo) {
return Stats[stat] + ' (' +
(Math.round(Stats[stat]/Stats.roundNo * 100)) + '%)';
} else return Stats[stat] + ' (100%)';
}
</script>
<h2>Rock Paper Scissors Lizard Spock</h2>
Your move:
<select id=moveList>
<option>Rock</option> <option>Paper</option> <option>Scissors</option>
<option>Lizard</option> <option>Spock</option> <option>Random</option>
</select>
<input type=button value=Go onclick=evaluateMove()><br>
<hr/>
You: <span class=box id=move>-</span>
Computer: <span class=box id=cpuMove>-</span><br>
<p><b>Result: </b><span id=winner></span></p>
<hr/>
<h3>Statistics</h3>
Games Played: <span id=roundNo></span><br>
Human Wins: <span id=humanWins></span><br>
Computer Wins: <span id=cpuWins></span><br>
Ties: <span id=ties></span>
</body> </html>
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |