@@ -7,12 +7,9 @@ let timerTime = 1000;
77let isDragging = false ;
88let dragMode = false ; // true: 黒にする, false: 白にする
99
10- const DEFAULT_BOARD_SIZE = 20 ;
11- const DEFAULT_CELL_SIZE = 30 ;
12-
1310//変数設定
14- let boardSize = 20 ;
15- let cellSize = 30 ;
11+ let boardSize = 20 ; //盤面の大きさ(20x20)
12+ const cellSize = 600 / boardSize ; //セルの大きさ(px)
1613
1714// around: 周囲の生きたセル数 self: 自身が生きているかどうか
1815function isNextAlive ( around , self ) {
@@ -27,11 +24,19 @@ function isNextAlive(around, self) {
2724 return false ;
2825}
2926
27+ // cellの状態に応じた色を返す関数
28+ function getStyle ( cell ) {
29+ // cellがtrueなら黒、falseなら白を返す
30+ return cell ? "black" : "white" ;
31+ }
32+
3033//Boardの初期化
3134let board = Array . from ( { length : boardSize } , ( ) => Array . from ( { length : boardSize } , ( ) => false ) ) ;
3235const table = document . getElementById ( "game-board" ) ;
36+
37+ //盤面をBoardに従って変更する関数達(Boardを変更したら実行する)
3338function renderBoard ( ) {
34- //盤面をBoardに従って変更する関数(Boardを変更したら必ず実行する)
39+ // 初回の盤面生成
3540 table . innerHTML = "" ;
3641 for ( let i = 0 ; i < boardSize ; i ++ ) {
3742 const tr = document . createElement ( "tr" ) ;
@@ -68,6 +73,30 @@ function renderBoard() {
6873 }
6974}
7075
76+ function rerender ( ) {
77+ // 2回目以降の盤面生成
78+ for ( let i = 0 ; i < boardSize ; i ++ ) {
79+ for ( let j = 0 ; j < boardSize ; j ++ ) {
80+ const button = table . children [ i ] . children [ j ] . children [ 0 ] ;
81+
82+ // 色の更新
83+ const currentCellColor = button . style . backgroundColor ;
84+ const expectedCellColor = getStyle ( board [ i ] [ j ] ) ;
85+ if ( currentCellColor !== expectedCellColor ) {
86+ button . style . backgroundColor = expectedCellColor ;
87+ }
88+
89+ // セルサイズの更新
90+ const currentCellsize = button . style . width ;
91+ const expectedCellsize = `${ cellSize } px` ;
92+ if ( currentCellsize !== expectedCellsize ) {
93+ button . style . width = expectedCellsize ;
94+ button . style . height = expectedCellsize ;
95+ }
96+ }
97+ }
98+ }
99+
71100document . addEventListener ( "mouseup" , ( ) => {
72101 isDragging = false ;
73102} ) ;
@@ -126,7 +155,7 @@ function progressBoard() {
126155 }
127156 board = newBoard ;
128157 generationChange ( generationFigure + 1 ) ;
129- renderBoard ( ) ;
158+ rerender ( ) ;
130159}
131160
132161function resetTimer ( ) {
@@ -147,15 +176,6 @@ on.pause = () => {
147176 resetTimer ( ) ;
148177} ;
149178
150- on . board_resize = ( newSize ) => {
151- boardSize = newSize ;
152- cellSize = Math . floor ( DEFAULT_CELL_SIZE * ( DEFAULT_BOARD_SIZE / newSize ) ) ;
153- board = Array . from ( { length : boardSize } , ( ) => Array . from ( { length : boardSize } , ( ) => false ) ) ;
154- renderBoard ( ) ;
155- generationChange ( 0 ) ;
156- resetTimer ( ) ;
157- } ;
158-
159179on . board_reset = ( ) => {
160180 //すべて白にBoardを変更
161181 board = Array . from ( { length : boardSize } , ( ) => Array . from ( { length : boardSize } , ( ) => false ) ) ;
0 commit comments