mam mały(duży) problem z rekurencją, mianowicie od pewnego czasu próbuję stworzyć sapera w js i wszystko pięknie ładnie, ale:
CODE
function saper() {
that = this;
function odkryj (e, x, y) {
if (x == undefined) {
x = this.getAttribute('data-x');
y = this.getAttribute('data-y');
}
there = byId('saper_cell_'+y+'_'+x);
if(x == -1 || y == -1 || y == that.tab.length || x == that.tab[0].length) {
} else {
if(there.className == 'odkryta') {
} else if (there.className == 'zakryta') {
there.innerHTML = that.tab[y][x];
there.className = 'odkryta';
switch (that.tab[y][x]) {
case 'B':
break;
case 0:
odkryj('e', x-1, y-1); //1
odkryj('e', x-1, y);
odkryj('e', x-1, y+1);
odkryj('e', x, y-1);
odkryj('e', x, y+1);
odkryj('e', x+1, y-1);
odkryj('e', x+1, y);
odkryj('e', x+1, y+1);
break;
}
}
}
return true;
}
}
that = this;
function odkryj (e, x, y) {
if (x == undefined) {
x = this.getAttribute('data-x');
y = this.getAttribute('data-y');
}
there = byId('saper_cell_'+y+'_'+x);
if(x == -1 || y == -1 || y == that.tab.length || x == that.tab[0].length) {
} else {
if(there.className == 'odkryta') {
} else if (there.className == 'zakryta') {
there.innerHTML = that.tab[y][x];
there.className = 'odkryta';
switch (that.tab[y][x]) {
case 'B':
break;
case 0:
odkryj('e', x-1, y-1); //1
odkryj('e', x-1, y);
odkryj('e', x-1, y+1);
odkryj('e', x, y-1);
odkryj('e', x, y+1);
odkryj('e', x+1, y-1);
odkryj('e', x+1, y);
odkryj('e', x+1, y+1);
break;
}
}
}
return true;
}
}
mała legenda:
tab(that.tab) to tabela dwuwymiarowa zawierająca bomby/liczbę bomb obok pola,
byId to alias dla document.getElementById;
i gdzie jest problem? problem jest przy zerze w miejscu 1:
odkrywam jakieś pole i jeśli jest zero to mamy 8 wywołań metody odkryj z współrzędnymi pól otaczających dane pole
przy czym jeśli któreś z tych wywołań również napotka zero, to mogę pomarzyć o wykonaniu tych, które 'czekają'
efekt jest taki, że po odkryciu pola z zerem nie odkrywa się cały obszar, który normalnie się odkrywać powinien