[Beschreibung]
JavaScript Variante eines Memoryspiels
[Kompatibilität]
[Code]
<SCRIPT language=JavaScript><!-- // // ############### init functions ################################### // globals var running= 0; var count; // game state and tiles matched counter var tiles= 6; // number of tiles per set var sets= 4; // number of sets var howmany; var width; var height; // how many tiles per game and board shape // get difficulty level: var passed= window.location.search.substring(1); var diff= getParm(passed,'d')- 0; if (diff==0) diff=2; if (diff== 1) { howmany= tiles*(sets-2); width= 6; var height= 4; } else if (diff== 2) { howmany= tiles*(sets-1); width= 6; var height= 6; } else { // diff== 3 howmany= tiles*sets; width= 6; var height= 8; } // (make sure that howmany*2 == width*height) var board= new Array(width*height); // the hidden tiles var sel= -1; // the tile currently selected var delay= 750; // length of time (ms) that pairs are revealed function preload() { if (document.images) { tile= new makeArray(tiles*sets+1); tile[0].src= "images/tileup.gif"; for (var s=1; s<=sets; s++) { for (var t=1; t<=tiles; t++) { tile[((s-1)*tiles)+t].src= "images/"+s+"_"+t+".gif"; } } blank= new Image(); blank.src= "../common/blank.gif"; } else { alert("Sorry, this game needs to run on a browser\nwhich supports JavaScript"); } } function select(pos) { if (!running) return; // do we have a tile already revealed? if (sel>= 0) { // are we choosing an unrevealed tile? if (document.images[""+pos].src== tile[0].src) { // reveal the new tile: document.images[""+pos].src= tile[board[pos]].src; // do we have a match? if (board[pos]== board[sel]) { // yep, so clear the tiles: running= 0; document.user.goes.value++; count++; setTimeout('clear('+sel+','+pos+',1)', delay); if (count== howmany) { setTimeout('alert("Well done!\\n")', delay+10); } } else { // nope, so re-hide the tiles: running= 0; document.user.goes.value++; setTimeout('clear('+sel+','+pos+',0)', delay); } sel= -1; } } else if (document.images[pos].src!= blank.src) { // no tile currently selected: document.images[""+pos].src= tile[board[pos]].src; sel= pos; } } function clear(posA, posB, flag) { var clear= (flag) ? blank.src : tile[0].src document.images[""+posA].src= clear; document.images[""+posB].src= clear; running= 1; } function newgame() { var index= document.user.diff.selectedIndex; var tmp_d= document.user.diff.options[index].value; if (tmp_d!= diff) { // options have changed so we need to redraw the page: window.location.href= "index.htm?d="+tmp_d; } else { // reset the board: for (var i=0; i<(width*height); i++) { board[i]= 0; document.images[""+i].src= tile[0].src; } var temp= new Array(tiles*sets+1); for (var i=0; i<tiles*sets+1; i++) { temp[i]=0; } var what= (rand(sets)*tiles)+rand(tiles)+1; var where= rand(width*height); // pick random tiles out of the ones we have available: for (var i=0; i<howmany; i++) { while (temp[what]) { what= (rand(sets)*tiles)+rand(tiles)+1; } temp[what]= 1; while (board[where]) { where= rand(width*height); } board[where]= what; while (board[where]) { where= rand(width*height); } board[where]= what; } document.user.goes.value= 0; sel= -1; count= 0; running= 1; } } function help() { alert("Icon Match v0.10 by Keith Drakard (kif@irt.org) 27/01/99\n\nThe aim of this memory game is to match up the pairs of tiles\nin as few moves as you can.\n"); } // The Central Randomizer 1.3 (C) 1997 by Paul Houle (houle@msc.cornell.edu) // See: http://www.msc.cornell.edu/~houle/javascript/randomizer.html // NOTE:- this example is set up to produce integers between 0-(limit-1) rnd.today=new Date(); rnd.seed=rnd.today.getTime(); function rnd() { rnd.seed = (rnd.seed*9301+49297) % 233280; return rnd.seed/(233280.0); } function rand(limit) { return Math.ceil(rnd()*limit)-1; } // The following functions were written by Martin Webb at http://www.irt.org/ function makeArray(n) { this.length= n; for (i=0; i<n; i++) { this[i]= new Image(); } return this; } function getParm(string,parm) { var startPos= string.indexOf(parm+"="); if (startPos> -1) { startPos= startPos+ 2; var endPos= string.indexOf("&",startPos); if (endPos== -1) endPos= string.length; return unescape(string.substring(startPos,endPos)); } return ''; } //--></SCRIPT>
<SCRIPT language=JavaScript><!-- // var output= ''; output+= '<FORM NAME="user">'; output+= '<TABLE ALIGN=center CELLSPACING=4 CELLPADDING=0 BORDER=0>\n'; output+= '<TR><TD COLSPAN=2 ALIGN=center><H1>Icon Match</H1></TD></TR>\n'; // create the board output+= '<TR><TD COLSPAN=2 ALIGN=center><TABLE CELLSPACING=2 CELLPADDING=0 BORDER=1>\n'; for (y=0; y<height; y++) { for (x=0; x<width; x++) { if (x== 0) output+= '<TR>'; output+= '<TD><A HREF="javascript:select('+((y*width)+x)+')" onFocus="blur()">'; output+= '<IMG SRC="images/tileup.gif" onDblClick="select('+((y*width)+x)+')"'; output+= ' NAME="'+((y*width)+x)+'" WIDTH=40 HEIGHT=40 ALT="" BORDER=0></A></TD>'; if (x== width-1) output+= '</TR>\n'; } } output+= '</TABLE></TD></TR>\n'; // add timer?? output+= '<TR><TD ALIGN=center><br><br>Goes: <INPUT TYPE="text"'; output+= ' SIZE=3 VALUE=0 onFocus="blur()" NAME="goes"></TD>'; output+= '<TD ALIGN=center><br><br>Difficulty: <SELECT NAME="diff" SIZE=1>'; output+= '<OPTION VALUE=1'; if (diff==1) output+=' SELECTED'; output+= '>Easy<OPTION VALUE=2'; if (diff==2) output+=' SELECTED'; output+= '>Medium<OPTION VALUE=3'; if (diff==3) output+= ' SELECTED'; output+= '>Hard</SELECT></TD></TR>'; output+= '<TR><TD COLSPAN=2 ALIGN=center><br><INPUT TYPE="button" VALUE=" New Game "'; output+= ' onClick="newgame()" onFocus="blur()" NAME="new"></TD></TR>'; output+= '</TABLE></FORM>\n'; output+= '<B><A HREF="javascript:help()">INSTRUCTIONS</A></B><br><br><SMALL><A HREF="mailto:kif\@irt.org">'; output+= 'Keith Drakard</A> v0.10 27th January 1999<br><br><A HREF="../../all.htm">'; output+= 'Back to WebGames</A></SMALL>\n'; document.write(output); preload(); newgame(); //--></SCRIPT>
IRT.org