I want to make a simple memory game where you try to match two cards. It it doesn’t match, it will go back to the cover. The problem is that there seems to be something wrong with the logic of the program because the cards are sometimes hard to click on. sometimes they don’t turn at all. But idoes work sometimes, I’m really frustrated by it…. If you see the problem with it, please help me. Thanks a lot!
[code=php]
var cards=new Array(“1.png”,”2.png”,”3.png”,”1.png”,”2.png”,”3.png”);
var slots=new Array();
var s=0;
var storesfirstguess;
var storessecondguess;
var imagepic1;
var imagepic2;
function startgame(){
for (var i=0;i<6;i++){
var z=Math.floor(Math.random()*cards.length);
slots[i]=cards[z];
cards.splice(z,1);
}
}
function test(x){
if (s==3) {
s=0;
}
s++;
if (s==1) {
if (x==1) {
document.pick1.src=slots[0];
storesfirstguess=slots[0];
imagepic1=1;
}else if (x==2) {
document.pick2.src=slots[1];
storesfirstguess=slots[1];
imagepic1=2;
}else if (x==3) {
document.pick3.src=slots[2];
storesfirstguess=slots[2];
imagepic1=3;
}else if (x==4) {
document.pick4.src=slots[3];
storesfirstguess=slots[3];
imagepic1=4;
}else if (x==5) {
document.pick5.src=slots[4];
storesfirstguess=slots[4];
imagepic1=5;
}else{
document.pick6.src=slots[5];
storesfirstguess=slots[5];
imagepic1=6;
}
}
if (s==2) {
if (x==1) {
document.pick1.src=slots[0];
storessecondguess=slots[0];
imagepic2=1;
}else if (x==2) {
document.pick2.src=slots[1];
storessecondguess=slots[1];
imagepic2=2;
}else if (x==3) {
document.pick3.src=slots[2];
storessecondguess=slots[2];
imagepic2=3;
}else if (x==4) {
document.pick4.src=slots[3];
storessecondguess=slots[3];
imagepic2=4;
}else if (x==5) {
document.pick5.src=slots[4];
storessecondguess=slots[4];
imagepic2=5;
}else{
document.pick6.src=slots[5];
storessecondguess=slots[5];
imagepic2=6;
}
checkmatch();
}
}
function checkmatch() {
if (storessecondguess==storesfirstguess){
// if there is a match
}else{
// if not a match
if (imagepic2==1) {
document.pick1.src=”cover.jpg”;
} else if (imagepic2==2) {
document.pick2.src=”cover.jpg”;
} else if (imagepic2==3) {
document.pick3.src=”cover.jpg”;
} else if (imagepic2==4) {
document.pick4.src=”cover.jpg”;
} else if (imagepic2==5) {
document.pick5.src=”cover.jpg”;
}else{
document.pick6.src=”cover.jpg”;
}
if (imagepic1==1) {
document.pick1.src=”cover.jpg”;
} else if (imagepic1==2) {
document.pick2.src=”cover.jpg”;
} else if (imagepic1==3) {
document.pick3.src=”cover.jpg”;
} else if (imagepic1==4) {
document.pick4.src=”cover.jpg”;
} else if (imagepic1==5) {
document.pick5.src=”cover.jpg”;
}else{
document.pick6.src=”cover.jpg”;
}
}
}