/* * Memory - classic card game * (C) 2012 by Gavriel Fleischer * http://neswork.com/javascript/js1k/2012/ */ // define DOM selector function instead of jQuery :) function $(s,d){if(!d)d=b;return d.querySelectorAll(s)}; // firt I used the following 2 functions(A,R), but then I replaced them with C: // addClass //function A(e,c){e.classList.add(c)} // removeClass //function R(e,c){e.classList.remove(c)} function C(e,c){e.className=c} //function D(d){return"width:"+d+"px;height:"+d+"px;"} // add css3 prefixes function P(s){return";-webkit-"+s+";-moz-"+s+";-o-"+s+";"+s} // get out a random element from an array function R(a){return a.splice(0|a.length*Math.random(),1)[0]} // constants we use a lot of time //T="transform"; //Y=T+":rotateY"; //S=T+":scale"; //X=T+":scaleX"; //Y=T+":scaleY"; //http://www.ilovecolors.com.ar/css-card-flip-webkit-transitions/ // set up styles $("head",document)[0].innerHTML=""; /* * Flip the card * * We use classes to be able to count some group of cards: * "p" is used for "paired" cards that stay visible forever * "v" is used for the 1 or 2 cards that we turned on and are currently "visible" */ function F(t){ // increase the counter and display it // f.innerHTML=++e; // get the visible cards v=$(".v"); x=v[0]; y=v[1]; // if 2 cards are visible we need to turn them back /*v.length==2*/ if(y){ C(x,"w"); C(y,"w") } // if one card was visible we need to compare it with the one we just turned /* v.length==1 */ if(x&&!y&&x!=t&&x.innerHTML==t.innerHTML){ // if they match we sign them as "paired" C(x,"w p"); C(t,"w p") }else C(t,"w v"); // otherwise we set the currently turned card to visible // see if we finished the whole pack if($(".p").length==16) alert("I ♥ U Hila!") } // e:counter for number of steps, i:for iterator // e= // i=0; /* * Fill in an array that represents the pack of cards. * Put each digit twice. Those will be the pairs. */ q="A234567890JQK".split(""); r="♠♥♣♦".split(""); p=[]; for(c=0;c<4;c++) for(i=0;i<13;i++) p.push([c,q[i]]); m=[]; for(i=0;i<8;i++){ m[i]=m[i+8]=R(p); } // start to draw the screen // d='