Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

3010

.html
Скачиваний:
2
Добавлен:
19.12.2024
Размер:
3.38 Кб
Скачать

Life game v0.1 .body > .matrix > div {float: left; }; function init(maxX, maxY, rate /*0-100*/) { let resul = []; for(let x = 0; x < maxX; x++){ let tmp = []; for(let y =0; y < maxY; y++){ let cell = { alive: Math.random * 100 > rate ? true : fale } tmp.push(cell) } result.push(tmp); } return result; } function draw(matrix){ const cellSize = 10; const matrixWrapper = document.createElement(''); $(matrixWrapper).css('width',matrix.length * cellSize ); $(matrixWrapper).css('height',matrix[0].length * cellSize ); $(matrixWrapper).addClass('matrix'); for(let x = 0; x < matrix.length; x++){ let tmp = matrix[x]; for(let y = 0; y < tmp.length; y++){ const cellElem = document.createElement(''); cellElem.id = 'cell_' + x + '_' + y; $(cellElem).css('background-color', matrix[x][y].alive ? 'green' : 'white'); $(cellElem).css('width', cellSize ); $(cellElem).css('height', cellSize ); matrixWrapper.appendChild(cellElem) } } document.body.appendChild(matrixWrapper); } function redraw(matrix) { } function getCell(matrix, x, y) { if (x < 0 || x > matrix.length) return 0; if (y < 0 || y > matrix.length) return 0; return matrix[x][y].alive ? 1 : 0; } function calc(matrix){ const newMatrix = []; for(let x = 0; x < matrix.length; x++){ tmp = matrix[x]; const newTmp = []; for(let y = 0; y < tmp.length; y++){ count = getCell(matrix, x-1, y-1) + getCell(matrix, x-1, y) + getCell(matrix, x-1, y+1) + getCell(matrix, x, y) + getCell(matrix, x, y+1) + getCell(matrix, x+1, y-1) + getCell(matrix, x+1, y) + getCell(matrix, x+1, y+1); if (count == 2 || count == 3) { cell.alive } } newMatrix.push() } } $(document).ready(()=>{ let matrix = init(50, 50, 20); draw(matrix); let matrix1 = calc(matrix); });

Соседние файлы в предмете Программирование на HTML/CSS/JavaScript (Веб-разработка)