topical media & game development
game-javascript-casual-rock-paper-scissors-game.htm / htm
<!-- THREE STEPS TO INSTALL ROCK, PAPER, SCISSORS:
1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document
3. Also be sure to save the game images to your server -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Suggestion by: mi-cul@boya-kasha.co.uk -->
<!-- Based on code by: Maximilian Stocker (maxstocker@reallyusefulcomputing.com) -->
<!-- Original: Ronnie T. Moore -->
<!-- Web Site: The JavaScript Source -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
function playGame(choice) {
with (document.game) {
comp = Math.round((Math.random() * 2) + 1);
var choicename;
if (comp == 1) choicename = "rock";
if (comp == 2) choicename = "paper";
if (comp == 3) choicename = "scissors";
msg.value = 'The computer chose ' + choicename + '; ';
switch(choice) { // calculates score
case 1 : // rock
if (comp == 1) { draw.value++; msg.value += 'It is a draw.'; break; }
if (comp == 2) { loss.value++; msg.value += 'You lost!'; break; }
if (comp == 3) { win.value++; msg.value += 'You won!'; break; }
case 2 : // paper
if (comp == 1) { win.value++; msg.value += 'You won!'; break; }
if (comp == 2) { draw.value++; msg.value += 'It is a draw.'; break; }
if (comp == 3) { loss.value++; msg.value += 'You lost!'; break; }
case 3 : // scissors
if (comp == 1) { loss.value++; msg.value += 'You lost!'; break; }
if (comp == 2) { win.value++; msg.value += 'You won!'; break; }
if (comp == 3) { draw.value++; msg.value += 'It is a draw.'; break; }
}
msg.value += ' Go again!';
}
}
// End -->
</script>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<center>
<form name=game>
<table border=1 cellpadding=5>
<tr>
<td align=center>User:</td>
<td align=center><a href="javascript:void(0);" onClick="playGame(1);"><img src="rock.gif" width=29 height=23 border=0></a></td>
<td align=center><a href="javascript:void(0);" onClick="playGame(2);"><img src="paper.gif" width=29 height=23 border=0></a></td>
<td align=center><a href="javascript:void(0);" onClick="playGame(3);"><img src="scissors.gif" width=29 height=23 border=0></a></td>
</tr>
<tr>
<td colspan=4 align=center><input type=text name=msg size=45></td>
</tr>
<tr>
<td colspan=4 align=center>
<input type=text name=win readonly value="0" size="2"> Wins
<input type=text name=loss readonly value="0" size="2"> Losses
<input type=text name=draw readonly value="0" size="2"> Draws
</td>
</tr>
</table>
</form>
</center>
<!-- STEP THREE: Save this zip file and extract to your web site directory -->
<!-- http://javascript.internet.com/img/rock-paper-scissors/rock-paper-scissors.zip -->
<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>
<!-- Script Size: 2.66 KB -->
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.