JS1K

#2: the original

Source for demo by Tobias Bräutigam. See also description.

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}};