
previous

page 9
next
card_clicked(card)
Putting it all together ...
function card_clicked(card) {
if (g_click_card==-1) { // first click
if (check_first(card)) {
g_click_card=card; // remember
card.classList.add('highlight');
}
} else { // second click
g_click_card.classList.remove('highlight');
if (card==g_click_card) { // same card
g_click_card=-1; // forget it
} else { // process g_click_card & card
if (check_second(card)) {
move(g_click_card,card);
draw();
}
g_click_card=-1; // forget first card
}
}
}
if (g_click_card==-1) { // first click
if (check_first(card)) {
g_click_card=card; // remember
card.classList.add('highlight');
}
} else { // second click
g_click_card.classList.remove('highlight');
if (card==g_click_card) { // same card
g_click_card=-1; // forget it
} else { // process g_click_card & card
if (check_second(card)) {
move(g_click_card,card);
draw();
}
g_click_card=-1; // forget first card
}
}
}
All that remains is to write the move function.
This I found quite tricky until I hit on a fail-safe and simple way of doing it.
Feel free to have a go yourself - it's very satisfying when you get something like this to work!
My function is on the next page.
previous
next