花了一些時間在HTML畫布中使用Javascript實現Conways遊戲。康威的生命遊戲算法錯誤(又名複製二維數組問題)
我的細胞沒有充當期待。它似乎正在拾取比實際存在的更多的相鄰細胞,並移除不應該被移除的細胞。任何幫助,將不勝感激!
//Globals
var currentCells = [
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
];
var currentGrid = currentCells;
var lastGrid = currentCells;
var cellsX = currentCells[1].length;
var cellsY = currentCells.length;
var canvas_id = "gameGrid";
var gridWidth = 100;
var gridHeight = 50;
var c = document.getElementById("gameGrid");
var ctx = c.getContext("2d");
var cellWidth = gridWidth/cellsX;
var cellHeight = gridHeight/cellsY;
ctx.fillStyle = "yellow";
window.setInterval(step, 1000);
function move(){
for(var a=0;a<cellsX;a++) {
for(var b=0; b<cellsY;b++) {
if (a > 0 && b > 0 && a < cellsY - 1 && b < cellsX){ //centre cells only
var currentNeighbours = neighbourCount(a, b);
if (lastGrid[a][b] == 0){
if(currentNeighbours == 3){
currentGrid[a][b] = 1;
}
}else if (lastGrid[a][b] == 1){
if(currentNeighbours > 3 || currentNeighbours < 2){
currentGrid[a][b] = 0;
//console.log("triggered " + currentNeighbours);
}
}
}
}
}
lastGrid = currentGrid;
}
function neighbourCount(a, b){
var currentNeighbours = 0;
if(lastGrid[a-1][b] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
if(lastGrid[a+1][b] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
if(lastGrid[a][b-1] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
if(lastGrid[a][b+1] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
if(lastGrid[a-1][b-1] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
if(lastGrid[a+1][b+1] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
if(lastGrid[a-1][b+1] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
if(lastGrid[a+1][b-1] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
//console.log(currentNeighbours);
return currentNeighbours;
}
function Draw(){
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
for(var a=0;a<cellsY;a++) {
var startX = 0;
for(var b=0; b<cellsX;b++) {
startX = cellWidth * b;
if (currentGrid[a][b] == 1){
ctx.fillRect(startX,(a*cellWidth) ,cellWidth,cellWidth);
}
ctx.strokeRect(startX,(a*cellWidth) ,cellWidth,cellWidth);
}
}
}
function step(){
move();
Draw();
}
<canvas id="gameGrid">
</canvas>
與你的問題並不真正相關,但是不是'neighbourCount'函數太複雜了嗎?爲什麼不簡單地添加鄰居單元格的值? –
根據你的建議,@JonathanM,我已經刪除了我的答案並編輯了OP。 – Snowmonkey
@JonathanM我不認爲這是一個問題,因爲'move'中的外部for循環顯式地排除了「邊緣」單元格(請參閱檢查「a'和'b'必須都大於0並小於它們各自的界限減1)。 – Xirema