/*
* 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 style prefixes
function S(s){return ";-webkit-"+s+";-moz-"+s+";-o-"+s}
// constants we use a lot of time
T="transform";
Y=T+":rotateY";
//http://www.ilovecolors.com.ar/css-card-flip-webkit-transitions/
// set up styles
$("head",document)[0].innerHTML="";
/*
* Now we set up the onclick function for each 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){
// R(x,"v");
// R(y,"v")
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"
// A(t,"p");
// R(x,"v");
// A(x,"p")
C(t,"w p");
C(t,"w p");
// }else A(t,"v"); // otherwise we set the currently turned card to visible
}else C(t,"w v");
// see if we finished the whole pack
if($(".p").length==16)
alert("I ♥ U!")
}
function M(){
// 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.
*/
p=[];for(;i<8;i++){p[i]=p[i+8]=i}
// start to draw the screen
//TODO: change it to so it reloads the page
d='