/    Sign up×
Community /Pin to ProfileBookmark

Logic problem with memory game.. I think

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

}
}
} [/code]

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @markguo123 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...