<HTML><HEAD>
<!--
-----------
Memory Game
------------
-->
<SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers
/*
THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
Copyright (c)1998 by Charles River Media. All Rights Reserved.
This applet can only be re-used or modifed by license holders of the
JavaScript Cookbook CD-ROM. Credit must be given in the source
code and this copyright notice must be maintained. If you do
not hold a license to the JavaScript Cookbook, you may NOT
duplicate or modify this code for your own use.
Use at your own risk. No warranty is given or implied of the suitability
of this applet for any specific application. Neither Erica Sadun nor
Charles River Media will be held responsible for any unwanted effects
due to the use of this applet or any derivative.
*/
//------------------RANDOM NUMBERS----------------------------
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
// Return a Random Integer between 0 and N-1
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))
}
//-----------------GAME FUNCTIONS------------------------
// Pick a random color and return its letter
function randomColor()
{
n = random(4)
return "RGBY".substring(n,n+1)
}
// Initialize the sequences
var sequence = ""
var userSequence = ""
// Flash each color in sequence with pauses
function playSequence(i)
{
// Flash the color
eval("parent.JCcolor"+sequence.substring(i,i+1)+".flash()")
// Either proceed to the next color or prompt the user
if ((i+1) < sequence.length)
SeqTimeOutID = window.setTimeout('playSequence('+(i+1)+')',1000)
else
SeqTimeOutID = window.setTimeout(
'document.forms[0].gameStatus.value="Now you try it..."', 1000)
}
// Flash all the colors together n times
function groupFlash(n)
{
parent.JCcolorR.flash()
parent.JCcolorG.flash()
parent.JCcolorB.flash()
parent.JCcolorY.flash()
if (n > 1)
SeqTimeOutID = window.setTimeout('groupFlash('+(n-1)+')',400);
}
// Lose a game -- flash screen, reset everthing
function lose()
{
document.forms[0].gameStatus.value="Game Over. Press Button to Start."
sequence = ""
userSequence = ""
groupFlash(4)
}
// User taps on a color
function tap(aColor)
{
// user input
userSequence += aColor
// temporary variables needed to avoid losing memory references
var ulen = userSequence.length
var slen = sequence.length
var cols = sequence.substring(0, ulen)
// Did they match? If not, the player loses
var match = (cols == userSequence)
if (!(match)) return(lose())
// Did the player finish the sequence? If so, progress
if (ulen == slen)
{
document.forms[0].gameStatus.value="Correct!"
SeqTimeOutID = window.setTimeout('progress()', 1000)
return true
}
// Partial sequence. Give encouragement.
document.forms[0].gameStatus.value = "Good! " +ulen+" of "+slen+" so far."
return true
}
// Player requested replay.
function replaySequence()
{
if (sequence.length==0) return(alert("No Game in Progress."))
userSequence = ""
document.forms[0].gameStatus.value="Watch this sequence"
SeqTimeOutID = window.setTimeout('playSequence(0)',2000);
return true
}
// Player is rewarded with a longer sequence
function progress()
{
sequence += randomColor()
userSequence = ""
document.forms[0].gameStatus.value="Watch this sequence"
SeqTimeOutID = window.setTimeout('playSequence(0)',2000);
}
// Player has requested to begin again with a new sequence
function playGame()
{
sequence = ""
progress()
}
<!-- done hiding --></SCRIPT></HEAD>
<BODY bgcolor="ffffff" link="0000ff" vlink="770077">
<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/SPICE.JPG" WIDTH=37 HEIGHT=72
ALIGN = LEFT>The Memory Game</H1></FONT>
<BLOCKQUOTE><FONT COLOR="770000">
In this game, players try to memorize a sequence
as it grows longer and longer and longer. Tap on the
"Start Game" Button to begin. The text box keeps the
player informed about the game status.
</FONT></BLOCKQUOTE>
<FONT SIZE=4><CENTER><FORM>
<INPUT TYPE="Text" VALUE="Game Over..." SIZE=40 NAME=gameStatus><p>
<INPUT TYPE="BUTTON" VALUE="Start Game" onClick="playGame()">
<INPUT TYPE="BUTTON" VALUE="Repeat Sequence" onClick="replaySequence()">
</FORM></CENTER>
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
The memory game makes extensive use of timed delays. The playing
squares pause before and after they flash brighter. The status
bar pauses after saying "correct" and before returning to "watch
this sequence". The repeating sequence player waits a few seconds
to allow attention to return to the playing board. JavaScript
programmers must pay close attention timing when using these sorts
of delays.<p>
For example, in playSequence, the sequence plays with timed
pauses--including a delay before prompting the user to begin</b>
<PRE><FONT COLOR="770000">
function playSequence(i)
{
eval("parent.JCcolor"+sequence.substring(i,i+1)+".flash()")
if ((i+1) < sequence.length)
SeqTimeOutID = window.setTimeout('playSequence('+(i+1)+')',1000)
else
SeqTimeOutID = window.setTimeout(
'document.forms[0].gameStatus.value="Now you try it..."', 1000)
}
</FONT></PRE>
<h5>Copyright ©1996 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>