Я решил написать собственную реализацию игры жизни Конвея и выбрал Javascript. Это чистый DHTML. Я разобрался с основами, но, похоже, он не соответствует нормальному поведению Game of Life. Например, модели типа планеров не «скользят», осцилляторы не колеблются. Я потратил много времени, пробуя различную логику, и переписал более десяти раз.
Итак, если бы я сделал планер планера, он превратился бы в квадратный натюрморт через одно поколение.
Вот код Javascript (извините за незакомментированный код):
var chanceOfLiveCells = 0.1;
var gridReference = null;
var gridDimension = 15;
var cells = null;
var cellsAlive = 0;
var cellsDead = 0;
var currentGeneration = 0;
function init() {
gridReference = document.getElementById('grid');
cells = new Array();
drawGrid();
placeRandomCells();
nextGeneration();
}
function drawGrid() {
var gridArray = new Array();
var counter = 0;
for(var x = 0; x <= gridDimension - 1; x = x + 1) {
gridArray.push('<tr>');
for(var y = 0; y <= gridDimension - 1; y = y + 1) {
//gridArray.push('<td id="' + counter + '">' + x + ', ' + y + ' (' + counter + ')' + '</td>');
gridArray.push('<td id="' + counter + '"></td>');
cells.push(counter);
counter = counter + 1;
}
gridArray.push('</tr>');
}
grid = gridArray.join('');
gridReference.innerHTML = grid;
}
function nextGeneration() {
currentGeneration = currentGeneration + 1;
for(var x = 0; x <= gridDimension - 1; x = x + 1) {
for(var y = 0; y <= gridDimension - 1; y = y + 1) {
var neighbours = cellNeighbourCount(x, y);
if(isCellLive(x, y) == true) {
if(neighbours < 2) {
setDeadCell(x, y);
cellsDead = cellsDead + 1;
}
if(neighbours == 2 || neighbours == 3) {
setLiveCell(x, y);
cellsAlive = cellsAlive + 1;
}
if(neighbours > 3) {
setDeadCell(x, y);
cellsDead = cellsDead + 1;
}
}
else if(isCellLive(x, y) == false){
if(neighbours == 3) {
setLiveCell(x, y);
cellsAlive = cellsAlive + 1;
}
}
}
}
document.getElementById('currentGeneration').innerHTML = currentGeneration;
document.getElementById('cellsAlive').innerHTML = cellsAlive;
document.getElementById('cellsDead').innerHTML = cellsDead;
cellsAlive = 0;
cellsDead = 0;
setTimeout('nextGeneration()', 200);
}
function placeRandomCells() {
for(var x = 0; x <= gridDimension - 1; x = x + 1) {
for(var y = 0; y <= gridDimension - 1; y = y + 1) {
if(Math.random() < chanceOfLiveCells) {
setLiveCell(x, y);
}
else {
setDeadCell(x, y);
}
}
}
}
function setLiveCell(x, y) {
document.getElementById(getCell(x, y)).style.backgroundColor = 'red';
}
function setDeadCell(x, y) {
document.getElementById(getCell(x, y)).style.backgroundColor = 'maroon';
}
function cellNeighbourCount(x, y) {
var count = 0;
if(isCellLive(x - 1, y) == true) {
count = count + 1;
}
if(isCellLive(x - 1, y + 1) == true) {
count = count + 1;
}
if(isCellLive(x, y + 1) == true) {
count = count + 1;
}
if(isCellLive(x + 1, y + 1) == true) {
count = count + 1;
}
if(isCellLive(x + 1, y) == true) {
count = count + 1;
}
if(isCellLive(x + 1, y - 1) == true) {
count = count + 1;
}
if(isCellLive(x, y - 1) == true) {
count = count + 1;
}
if(isCellLive(x - 1, y - 1) == true) {
count = count + 1;
}
return count;
}
function isCellLive(x, y) {
if(document.getElementById(getCell(x, y)).style.backgroundColor == 'red') {
return true;
}
return false;
}
function getCell(x, y) {
if(x >= gridDimension) {
x = 0;
}
if(y >= gridDimension) {
y = 0;
}
if(x < 0) {
x = gridDimension - 1;
}
if(y < 0) {
y = gridDimension - 1;
}
return cells[x * gridDimension + y];
}
function createSquare() {
setLiveCell(4, 4);
setLiveCell(4, 5);
setLiveCell(4, 6);
setLiveCell(5, 6);
setLiveCell(6, 6);
setLiveCell(6, 5);
setLiveCell(6, 4);
setLiveCell(5, 4);
}
function createBlinker() {
setLiveCell(4, 5);
setLiveCell(5, 5);
setLiveCell(6, 5);
}
function createBeacon() {
setLiveCell(4, 4);
setLiveCell(4, 5);
setLiveCell(5, 4);
setLiveCell(5, 5);
setLiveCell(6, 6);
setLiveCell(6, 7);
setLiveCell(7, 6);
setLiveCell(7, 7);
}