[英]game of life "cells" not updating correctly
所以我試圖在 javaScript 中編寫康威的生活游戲,但有些東西不起作用。 如果我連續三個單元格,它們應該只是翻轉方向,而是形成一個 2 x 2 的正方形。 我環顧四周,發現您需要等待更新 position 直到掃描整個“板”。 但我至少認為我已經在這樣做了。 所以我真的不知道出了什么問題。
我調用updateGridValues function 運行一代
function getCellValue(row, col) { try { return arry[row][col]; } catch (e) { return 0; } } function countNeighbours(row, col) { let neighbours = 0; neighbours += getCellValue(parseInt(row - 1), parseInt(col - 1)); neighbours += getCellValue(parseInt(row - 1), parseInt(col)); neighbours += getCellValue(parseInt(row - 1), parseInt(col + 1)); neighbours += getCellValue(parseInt(row), parseInt(col - 1)); neighbours += getCellValue(parseInt(row), parseInt(col + 1)); neighbours += getCellValue(parseInt(row + 1), parseInt(col - 1)); neighbours += getCellValue(parseInt(row + 1), parseInt(col)); neighbours += getCellValue(parseInt(row + 1), parseInt(col + 1)); return neighbours; } function uppdateCell(i, j) { const total = countNeighbours(i, j); if (arry[i][j] === 0 && total == 3) { return 1; } else if (total > 4 || total < 3) { return 0; } else { return arry[i][j]; } } function uppdateGridValues() { for (var i = 0; i < cellsInRow; i++) { for (var j = 0; j < cellsInCollum; j++) { let newState = uppdateCell(i, j); arryOld[i][j] = newState; } } arry = arryOld; arryOld = arry; }
創建數組
let arry = [];
let arryOld = [];
for(var i = 0; i < cellsInRow; i++){
arry[i] = [];
for(var j = 0; j < cellsInCollum; j++){
arry[i][j] = 0;
}
}
arryOld = arry;
在 updateCell function 中寫出總變量時,我有時會得到 NaN。 我不知道為什么,因為如果從 countNeighbours 傳遞給 getCellValue 的值超出數組大小,我想返回 0。
您不能使用try/catch
來檢測數組外的讀數。 數組外的訪問只返回undefined
,它不會引發異常。
您可以使用||
如果未設置該值,則返回默認值(請注意,這在這種特殊情況下有效,因為沒有其他有效的“錯誤”值可以返回,它不能總是這樣使用)。
function getCellValue(row, col) {
return arry[row][col] || 0;
}
另一個問題是
arryOld = arry;
不會復制數組,兩個變量都引用同一個數組。 使用它來制作數組的深層副本。
arryOld = arry.map(row => [...row]);
我正在通讀,我發現了一些可能導致錯誤的東西:您在countNeighbors()
中調用parseInt()
) 。
來自parseInt()
上的 MDN JavaScript 文檔:
“
parseInt()
function 解析字符串參數並返回 integer... 或NaN
。(如果輸入在幾種不同的方式中無效)”
基本上, parseInt()
期待字符串輸入,你給它 integer 輸入。 我建議從countNeighbors()
中刪除對parseInt()
的調用,如下所示:
function countNeighbours(row, col) {
let neighbours = 0;
neighbours += getCellValue((row - 1), (col - 1));
neighbours += getCellValue((row - 1), (col));
neighbours += getCellValue((row - 1), (col + 1));
neighbours += getCellValue((row), (col - 1));
neighbours += getCellValue((row), (col + 1));
neighbours += getCellValue((row + 1), (col - 1));
neighbours += getCellValue((row + 1), (col));
neighbours += getCellValue((row + 1), (col + 1));
return neighbours;
}
當我閱讀這段代碼時,我想到了一些關於你可以做的事情的建議。 (這些完全是可選的。這些都不會導致您的代碼中斷。)
arry
作為全局變量在這里有效,但全局變量使代碼更難管理和理解。 我會將其設為主要 function 的局部變量,並將其傳遞給您的所有其他函數。arry
應該重命名為更具描述性的名稱; 可能是gameboard
或grid
。countNeighbors()
可以使用嵌套的 for 來縮短和簡化,類似於updateGridValues()
中的內容。 這是一個如何做到這一點的例子:function countNeighbours(row, col) {
let neighbourTotal = 0;
for (let y = -1; y <= 1; y++) {
for (let x = -1; x <= 1; x++) {
neighbourTotal += getCellValue(row + y, col + x);
}
}
return neighbourTotal;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.