	//	JAVASCRIPT CONCENTRATION GAME	//	by "C.C." Chamberlin	//	See pastelet instructions for use	//	Visit my pastelet archive at www.leadingobject.com/seminars/javascript	//	CUSTOMIZATION OPTIONS	//	Change the two values below to represent the pixel width	//	and height of your card images			var	cardwidth = 80		var cardheight = 80				var	Cards			//	Array containing which cards are where		var	CardImages		//	Array containing the 9 card images (0 = back)		var WinImages		//	Array containing the 8 winning images		var Cardslots		//	Array containing the 16 card locations		var	Pairsfound		//	How many pairs have been found?		var	Firstcard		//	Previous card taken		var Currentstate	//	State of the game				function rand(num) {			return Math.floor(Math.random() * num)+1;		}			function initializeGame() {			//	This function handles loading all the images for the game			//	and other one-time initializations						CardImages = new Array(10)			for (looper=0; looper<10; looper++) {				CardImages[looper] = new Image				CardImages[looper].src = "images/card"+looper+".gif"			}						WinImages = new Array(10)			for (looper=1; looper<10; looper++) {				WinImages[looper] = new Image				WinImages[looper].src = "images/card"+looper+"win.gif"			}						Cards = new Array(17)						Cardslots = new Array(17)			for (looper=1; looper<17; looper++) {				Cardslots[looper] = eval("document.card"+looper)			}						newGame()		}				function newGame() {			//	This function handles setting up a new game						//	Set up the deck and shuffle it			for (looper=1; looper<9; looper++) {				Cards[looper]=looper				Cards[looper+8]=looper			}			for (looper=0; looper<60; looper++) {				s1 = rand(16)				s2 = rand(16)				tempcard = Cards[s1]				Cards[s1] = Cards[s2]				Cards[s2] = tempcard			}						//	Turn all the cards over			for (looper=1; looper<17; looper++) {				Cardslots[looper].src = CardImages[0].src;			}						Pairsfound = 0			Firstcard = 0			Currentstate = 0		//	Let player pick cards					}				function handleMe(whichcard) {			if (Currentstate != 0) return			if (Cardslots[whichcard].src != CardImages[0].src) return			//	Player has picked a face-down card			Cardslots[whichcard].src = CardImages[Cards[whichcard]].src			if (Firstcard == 0) {				//	First of a pair being picked				Firstcard = whichcard			} else {				//	Second of a pair being picked				if (Cards[Firstcard] == Cards[whichcard]) {					//	A match!					Pairsfound++					if (Pairsfound == 8) {						Currentstate = 1		//	Block player from picking						setTimeout("endgamecycle()",10)					} else {						Firstcard = 0					}				} else {					//	A miss!					Currentstate = 1		//	Block player from picking					setTimeout("flipback("+Firstcard+","+whichcard+")",500)				}			}		}				function endgamecycle() {			Currentstate++			if (Currentstate > 20) return newGame()			for (looper=1; looper<17; looper++) {				if (Currentstate % 2 == 0) {					Cardslots[looper].src = CardImages[Cards[looper]].src				} else {					Cardslots[looper].src = WinImages[Cards[looper]].src				}			}			setTimeout("endgamecycle()",30)			return true		}				function flipback(card1,card2) {			Firstcard = 0			Cardslots[card1].src = CardImages[0].src			Cardslots[card2].src = CardImages[0].src			Currentstate = 0		}				function setupcards() {			itemtrack = 1;			document.writeln("	<center><table border=0 cellpadding=0 cellspacing=0>")			for (loopy=1; loopy<5; loopy++) {				document.writeln("		<tr>")				for (loopx=1; loopx<5; loopx++) {					document.writeln("		<td>")					document.writeln("			<a href=\"#\" onClick=\"handleMe("+itemtrack+"); return true;\">")					document.writeln("			<img src=\"images/card0.gif\" width="+cardwidth+" height="+cardheight+" name=\"card"+itemtrack+"\" border=0></a>")					document.writeln("		</td>")					itemtrack++				}				document.writeln("		</tr>")			}			document.writeln("	</table></center>")		}				setupcards()		initializeGame()