previous

page 6

next
card_clicked(card)
function card_clicked(card) {
if (g_click_card==-1) { // first click
g_click_card=card; // remember
card.classList.add('highlight');
} else { // second click
g_click_card.classList.remove('highlight');
if (card==g_click_card) { // same
g_click_card=-1; // forget 1st card
} else {
// process g_click_card & card
g_click_card=-1; // forget first card
}
}
}
process g_click_card & card

For the moment assume we have a legitimate move and implement move(card1,card2).

move(card1,card2)
function move(card1,card2) {
var c1=card1.style.gridColumnStart;
var c2=card2.style.gridColumnStart;
var id=g_main_cols[c1].pop();
g_main_cols[c2].push(id);
}

pop is the opposite to push.

function card_clicked(card) {
...
} else { // process g_click_card & card
move(g_click_card,card);
draw();
g_click_card=-1; // forget first card
}
}
}
previous
next