var x=4,y=4,e=16,f=[e],g=[],h=f,k="||||",m=0,n=0,o=0,p=0,q=0,r=0,s=0,c=0,t=false,u=t,v=document,w=Math.round;m_();function m_(){{for(i=0;i<e;i+=2){h[i]=i/2;h[i+1]=i/2}};{f=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];for(var b=0;b<e;b++){a=h.splice(w(Math.random()*h.length-1),1);f[b]=a}};i_();return 0}function i_(){j_=0;v.write('<h1>Play memory!<table border="9" style="font-size:300%;cursor:pointer;">');for(i=0;i<x;i++){v.write("<tr>");for(j=0;j<y;j++){v.write('<td bgcolor="orange" id="'+j_+'" onclick="n_('+j_+')"> '+k+" </td>");j_++}v.write("</tr>")}}function k_(b){v.getElementById(b).innerHTML=String.fromCharCode((w(f[b])+65))}function l_(b){v.getElementById(b).innerHTML=k}function c_(b){e_=false;for(a=0;a<g.length;a++)if(w(g[a])==b)e_=true;return e_}function n_(b){if(c_(w(f[b])))return 0;if(t){if(b==r||b==s)return 0;l_(r);l_(s);t=false}if(o==0){o=1;p=w(f[b]);r=b;k_(b);return 0}if(o==1){q=w(f[b]);s=b;if(r==s)return 0;else{o=2;k_(b)}if(p==q){n++;n==e/2?alert(m):g.push(p)}else t=true;m++;return o=0}};
Original version I wrote in 2006:
// JScript File
// Author: Tobias Bräutigam
// Field
var sizex =4;
var sizey =4;
var cards_total =16; //sizex*sizey
var field =new Array (cards_total);
var field_found =new Array (); //remember found pair number
var helper =field;
var cardback ='X'; //"Back" of the card
//Game
var trials =0;
var pairs =0;
var clicks =0;
var card1 =0;
var card2 =0;
var card1_pos =0;
var card2_pos =0;
var c =0;
var wrongtry =false;
var nochars =false; //display no char but color or img instead
var mydoc =document; //replace DOM document by shorter var
var mRound =Math.round;
mainfunc(); //Main
function mainfunc()
{
init_field();
fill_field();
show_field();
return(0);
}
function init_field () //setup paired cards
{
for (i=0;i<cards_total;i=i+2)
{
helper[i]=i/2;
helper[i+1]=i/2;
}
}
function fill_field() //mix cards
{
field=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
for (var counter = 0; counter < cards_total; counter++)
{
a=helper.splice(mRound(Math.random()*helper.length-1),1);
field[counter]=a;
}
}
function show_field() //draw table
{
counter=0;
mydoc.write('<div align="center">Score <div id="555"></div><table border="1" id="mem" style="font-family:Verdana; font-size:200%;cursor:pointer;">');
for (i=0;i<sizex;i++)
{
mydoc.write('<tr>');
for (j=0;j<sizey;j++)
{
//show card value
mydoc.write('<td align="center" id="'+counter+'" onclick="play('+counter+')"> '+cardback+' </td>'); //hide card value
counter++;
}
mydoc.write('</tr>');
}
mydoc.write('</table></div>');
}
function show(id) // display card
{
mydoc.getElementById(id).innerHTML=cardface(mRound(field[id]));
mydoc.getElementById(id).style.backgroundColor='#'+(field[id]*15+768);
}
function hide(id) // hide card
{
mydoc.getElementById(id).innerHTML=cardback;
mydoc.getElementById(id).style.backgroundColor="White";
}
function cardstate(card) //check for found pairs
{
cardopen=false;
for (a=0;a<field_found.length;a++)
{
if (mRound(field_found[a])==card)
{
cardopen=true;
}
}
return(cardopen);
}
function cardface(cardid) //get card face call with number, not string!
{
cardchar=String.fromCharCode(cardid+65);
return (cardchar);
}
function play(id) // Called by event on table cells with cell ID
{
//clicked on open pair card?
if (cardstate(mRound(field[id])))
{
return(0);
}
if (wrongtry) //Hide cards one click after wrong try
{
if ((id == card1_pos) || (id == card2_pos)) //clicked on any currently open card?
{
return(0);
}
hide(card1_pos);
hide(card2_pos);
wrongtry=false;
}
if (clicks==0) //First card turned?
{
clicks=1;
card1=mRound(field[id]); //Remember card number [turned into object!?;(]
card1_pos=id; //Remember card position
show(id);
return(0);
}
if (clicks==1) //Second card turned?
{
card2=mRound(field[id]); //Remember card number
card2_pos=id; //Remember card position
if (card1_pos == card2_pos) //Clicked on same card?
{
return(0);
}
else
{
clicks=2;
show(id);
}
if (card1 == card2) //Found a pair?
{
pairs++;
if (pairs == cards_total/2) //Last pair found?
{
alert('Fertig!');
}
else
{
field_found.push(card1); //Remember guessed pair number
}
}
else
{
wrongtry=true; // Daneben!
}
trials++;
mydoc.getElementById(555).innerHTML=(trials+"/"+pairs);
clicks=0;
return(0);
}
return(0);
}