topical media & game development
game-javascript-casual-guess-100-game.htm / htm
<!-- THREE STEPS TO INSTALL GUESS 1 - 100:
1. Paste the coding into the HEAD of your HTML document
2. Add the onLoad event handler to the BODY tag
3. Put the last coding into the BODY of your HTML document -->
<!-- STEP ONE: Copy this code into the HEAD of your HTML document -->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
var js_mult1=3141
var js_mult2=5821
var js_m1=100000000
var js_m2=10000
var js_iseed=0
var js_iseed1=0
var js_iseed2=0
function random(n) {
if (js_iseed == 0) {
now = new Date()
js_iseed = now.getHours() + now.getMinutes() * 60 + now.getSeconds() * 3600
}
js_iseed1 = js_iseed / js_m2
js_iseed2 = js_iseed % js_m2
var tmp = (((js_iseed2 * js_mult1 + js_iseed1 * js_mult2) % js_m2) *
js_m2 + (js_iseed2 * js_mult2)) % js_m1
js_iseed = (tmp + 1) % js_m1
return (Math.floor((js_iseed/js_m1) * n))
}
var nGuesses = 0
function GuessNum() {
var response
var num = parseInt(document.forms[0].guess.value)
document.forms[0].guess.value = num
nGuesses++
response = ""
if (num < myNumber) response = response + "Higher!"
if (num > myNumber) response = response + "Lower!"
if (num == myNumber) {
respone = "Correct!";
alert ("Right!! Reload the page to start another game....");
}
document.forms[1].result.value=response
document.forms[1].guesses.value=nGuesses
document.forms[0].guess.focus()
document.forms[0].guess.select()
return true
}
function GiveUp() {
var response
nGuesses = 0
alert("The number was " + myNumber +". Reload page to start a new game....");
}
// End -->
</SCRIPT>
<!-- STEP TWO: Add the onLoad event handler to the BODY tag -->
<BODY onLoad="document.forms[0].guess.focus();document.forms[0].guess.select()">
<!-- STEP THREE: Put the last code into the BODY of your HTML document -->
<CENTER>
<H2>Guessing Game</H2><p>
<font size=2>
Guess the same number that the computer has guessed. The number will range from 1 to 100.<p>Reload the page to start a new game.</font><SCRIPT>
myNumber = random();
nGuesses = 0
</SCRIPT>
<FORM onSubmit="GuessNum(); return false">
Your Guess: <INPUT TYPE="text" NAME="guess" SIZE=5>
<INPUT TYPE="button" VALUE=" Guess... " onClick="GuessNum();">
<INPUT TYPE="button" VALUE=" GiveUp " onClick="GiveUp();">
</FORM>
<FORM>
<CENTER>
#-Guesses: <INPUT TYPE="text" NAME="guesses" SIZE=3>
INFO: <INPUT TYPE="text" NAME="result" SIZE=10>
</CENTER>
</FORM>
<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.55 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.