Brakuje mi pomysłów jak rysować nowe elementy td dla komórek. Kod chyba nie wymaga komentarzy bo jest napisany po polsku specjalnie żeby ułatwić pomoc jeśli się znajdzie ktoś tak życzliwy i zerknie na te parę chwil...
Teoretycznie wykonuję w pętli funkcję ozywKomorke która niestety - ożywia mi tylko i wyłącznie jedną. Co źle robię? Serio już mam dość tego problemu ale poddawać się nie chce.
<!DOCTYPE html> <head> <style> #game{ background:red; min-width:100px; min-height:100px; } .zywa{background:white;width:8px;height:8px;border:1px solid black;} .martwa{background:grey;width:8px;height:8px;border:1px solid blue;} </style> <script> function test() { game = new zycie(10, 5); } function zycie(rows, columns) { var swiat = budujSwiat(); function budujSwiat() { var swiat = []; for(var i = 0; i<rows; i++) { var innerArr = []; for(var j = 0; j<columns; j++) { innerArr.push(0); } swiat.push(innerArr); } return swiat; } function pokazSwiat(swiat) { // wyswietlanie swiata for (var x = 0; x < swiat.length; x++) { for (var y = 0; y < swiat.length; y++) { ozywKomorke(x, y, swiat[x][y]); } } } function ozywKomorke(x, y, alive) { var swiat = document.getElementById('game'); var komorka = ""; alive : komorka += '<td class="zywa"></td>'; swiat.innerHTML = komorka; } function losowaPopulacja(swiat) { for(var x = 0; x < swiat.length; x++) { for(y = 0; y < swiat[x].length; y++) { if(Math.log(Math.random()*1) < -0.6) { swiat[x][y]=1; } } } } function manual(swiat) { swiat[20][20] = 1; swiat[21][20] = 1; swiat[22][20] = 1; } function zyweC(swiat, x, y) { if(x > 0 && y > 0 && x < rows-1 && y < columns-1) { var wszystkieZywe = swiat[x-1][y-1]+swiat[x][y-1]+swiat[x+1][y-1]+ swiat[x-1][y]+swiat[x+1][y]+swiat[x-1][y+1]+ swiat[x][y+1]+swiat[x+1][y+1]; console.log(wszystkieZywe); return wszystkieZywe; } else { return 0; } } function sprawdz(swiat) { var nowySwiat = budujSwiat(); for(var x = 0; x < swiat.length; x++) { for(var y = 0; y < swiat[x].length; y++) { var komorka = swiat[x][y]; var zywe = zyweC(swiat, x,y); if(komorka == 1) { if(zywe < 2) { nowySwiat[x][y] = 0; } else if(zywe == 2 || zywe == 3) { nowySwiat[x][y] = 1; } else if(zywe > 3) { nowySwiat[x][y] = 0; } } else if(komorka == 0 && zywe == 3) { nowySwiat[x][y] = 1; } } } return nowySwiat; } losowaPopulacja(swiat); //manual(swiat); pokazSwiat(swiat); setInterval(function() { var nowySwiat = sprawdz(swiat); pokazSwiat(nowySwiat); swiat = nowySwiat; }, 50); } </script> </head> <body onload="test()"> <table id="game"> </table> </body> </html>
Sugestie?