
previous

page 17
next
move_on(id) in functions.js
function move_on(id) {
if (g_found<g_letters) {
var row=parseInt(id/10);
var col=id%10;
col+=2;
if (col>9) {
row++; col=0;
if (row>9) row=0;
}
var id=rc2id(row,col);
var square=document.getElementById(id);
if (square.style.visibility=='hidden') {
move_on(id);
} else {
var a=square.getAttribute('a');
var v=square.textContent;
if (a==v) { // already found
move_on(id);
} else {
highlight(id);
}
}
}
}
if (g_found<g_letters) {
var row=parseInt(id/10);
var col=id%10;
col+=2;
if (col>9) {
row++; col=0;
if (row>9) row=0;
}
var id=rc2id(row,col);
var square=document.getElementById(id);
if (square.style.visibility=='hidden') {
move_on(id);
} else {
var a=square.getAttribute('a');
var v=square.textContent;
if (a==v) { // already found
move_on(id);
} else {
highlight(id);
}
}
}
}
What is really interesting about this function is that it calls itself!
This is a very dangerous way to program because if you're not very careful, the function may go round in circles forever!
Notice how the very first thing I do is to check whether there are any letters still to find.
Anyway, give it a go and see how beautifully it works.
previous
next