Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<style type="text/css">
/*<![CDATA[*/
#tst {
position:relative;left:0px;top:0px;height:400px;width:400px;border:solid red 1px;
}
#tst IMG {
width:100px;height:100px;
}
/*]]>*/
</style></head>
<body>
<div id="tst" style="" ></div>
<input type="button" name="" value="Reset" onmouseup="matching.reset('tst');" /><input id="tries" />
<script type="text/javascript">
/*<![CDATA[*/
var matching={
init:function(o){
var p=document.getElementById(o.ID),cards=o.Cards,ary=[],rows=4,img,z0=0,z1=0
for (;z0<cards.length;z0++){
cards[z0][2]=z0;
}
o.ip=document.getElementById(o.TriesID);
o.nu=rows*rows;
o.imgs=[];
for (;z1<o.nu;z1++){
img=document.createElement('IMG')
o.imgs[z1]=[img];
p.appendChild(img);
this.addevt(img,'mouseup','click',o,z1);
}
this[o.ID]=o;
this.reset(o.ID);
},
click:function(o,nu){
if (o.cnt==2){
if (o.ary[0][3]!=o.ary[1][3]){
o.ary[0][1]=o.ary[1][1]=true;
o.ary[0][0].src=o.ary[1][0].src=o.CoverSRC;
}
o.cnt=0;
o.ary=[];
}
if (o.imgs[nu][1]){
o.imgs[nu][1]=false;
o.ary[o.cnt++]=o.imgs[nu];
o.imgs[nu][0].src=o.imgs[nu][2];
o.ip&&o.cnt==2?o.ip.value++:null;
}
},
reset:function(id){
if (this[id]){
var o=this[id],cards=this.shuffle(o.Cards),ary=[],z0=0,z1=0;
for (;z0<o.nu/2;z0++){
ary.push([cards[z0][0],cards[z0][2]]);
ary.push([cards[z0][1],cards[z0][2]]);
}
ary=this.shuffle(ary)
for (;z1<o.nu;z1++){
o.imgs[z1][0].src=o.CoverSRC;
o.imgs[z1][1]=true;
o.imgs[z1][2]=ary[z1][0];
o.imgs[z1][3]=ary[z1][1];
}
o.cnt=0;
o.ary=[];
o.ip?o.ip.value=0:null;
}
},
addevt:function(o,t,f,p,p1){
var oop=this;
if (o.addEventListener){
o.addEventListener(t,function(e){ return oop[f](p,p1);}, false);
}
else if (o.attachEvent){
o.attachEvent('on'+t,function(e){ return oop[f](p,p1); });
}
},
shuffle:function(ary){
for (var r,t,z0=0;z0<ary.length;z0++){
r=Math.floor(Math.random()*ary.length);
t=ary[z0];
ary[z0]=ary[r];
ary[r]=t;
}
return ary;
}
}
matching.init({
ID:'tst',
TriesID:'tries',
CoverSRC:'http://www.wisconsinhistory.org/kids/matching/card.jpg',
Cards:[
['http://www.wisconsinhistory.org/kids/matching/0.jpg','http://www.vicsjavascripts.org.uk/StdImages/0.gif'],
['http://www.wisconsinhistory.org/kids/matching/1.jpg','http://www.vicsjavascripts.org.uk/StdImages/1.gif'],
['http://www.wisconsinhistory.org/kids/matching/2.jpg','http://www.vicsjavascripts.org.uk/StdImages/2.gif'],
['http://www.wisconsinhistory.org/kids/matching/3.jpg','http://www.vicsjavascripts.org.uk/StdImages/3.gif'],
['http://www.wisconsinhistory.org/kids/matching/4.jpg','http://www.vicsjavascripts.org.uk/StdImages/4.gif'],
['http://www.wisconsinhistory.org/kids/matching/5.jpg','http://www.vicsjavascripts.org.uk/StdImages/5.gif'],
['http://www.wisconsinhistory.org/kids/matching/6.jpg','http://www.vicsjavascripts.org.uk/StdImages/6.gif'],
['http://www.wisconsinhistory.org/kids/matching/7.jpg','http://www.vicsjavascripts.org.uk/StdImages/7.gif'],
['http://www.wisconsinhistory.org/kids/matching/8.jpg','http://www.vicsjavascripts.org.uk/StdImages/8.gif'],
['http://www.wisconsinhistory.org/kids/matching/9.jpg','http://www.vicsjavascripts.org.uk/StdImages/9.gif'],
['http://www.wisconsinhistory.org/kids/matching/10.jpg','http://www.wisconsinhistory.org/kids/matching/10.jpg'],
['http://www.wisconsinhistory.org/kids/matching/11.jpg','http://www.wisconsinhistory.org/kids/matching/11.jpg'],
['http://www.wisconsinhistory.org/kids/matching/12.jpg','http://www.wisconsinhistory.org/kids/matching/12.jpg'],
['http://www.wisconsinhistory.org/kids/matching/13.jpg','http://www.wisconsinhistory.org/kids/matching/13.jpg'],
['http://www.wisconsinhistory.org/kids/matching/14.jpg','http://www.wisconsinhistory.org/kids/matching/14.jpg'],
['http://www.wisconsinhistory.org/kids/matching/15.jpg','http://www.wisconsinhistory.org/kids/matching/15.jpg'],
['http://www.wisconsinhistory.org/kids/matching/16.jpg','http://www.wisconsinhistory.org/kids/matching/16.jpg'],
['http://www.wisconsinhistory.org/kids/matching/17.jpg','http://www.wisconsinhistory.org/kids/matching/17.jpg']
]
});
/*]]>*/
</script>
</body>
</html>
Bookmarks