You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🔍 To create the next frame, each cell and its neighboring cells are analyzed:
⬜
Left
Center
Right
Top
n[y-1][x-1]
n[y-1][x]
n[y-1][x+1]
Center
n[y][x-1]
n[y][x]
n[y][x+1]
Bottom
n[y+1][x-1]
n[y+1][x]
n[y+1][x+1]
🧭 Taking the cardinal points as a reference. If the neighboring cell of the Northwest (NW) is equal to 1, internally in the CalculateCardinals () function and thus the same with N, NE, E, SE, S, SW and W:
🏗 The result is processed by the sumLife() function:
constsumLife=(array,row,column,center)=>{constresult=CalculateCardinals(array,row,column);if(center===0){// if it's a dead cellularif(result===3)return1return0}if(center===1){// if it's a living cellularif(result===2||result===3)return1return0}};// sumLife()
🎦 And finally actualFrame is modified with a useEffect () to show an updated "frame" of the next generation:
useEffect(()=>{if(isGameRun.value){// Check if it is paused before executing the codeif(actualFrame.toString()===gameProperty.noLife){// If no more life...returnisGameRun.setFalse();}else{constsaveFrame=initialFrame;// We keep a copysetTimeout(()=>{generation.increase();actualFrame.forEach((row,indexRow)=>{row.forEach((cellular,indexColumn)=>{returnsaveFrame[indexRow][indexColumn]=sumLife(actualFrame,indexRow,indexColumn,cellular);})})setActualFrame(saveFrame);},generationSpeed.value);// default generationSpeed.value === 0};};},[isGameRun.value,actualFrame]);