@@ -18,7 +18,7 @@ class Board {
1818 this . board [ column ] = [ ] ;
1919
2020 for ( let row = 0 ; row < rows ; row ++ ) {
21- this . board [ column ] [ row ] = new Square ( column , row ) ;
21+ this . board [ column ] [ row ] = new Cell ( column , row ) ;
2222 }
2323 }
2424
@@ -35,30 +35,32 @@ class Board {
3535 for ( let bomb = 0 ; bomb < parseInt ( mines ) ; bomb ++ ) {
3636 const row = parseInt ( Math . random ( ) * this . rows ) ;
3737 const col = parseInt ( Math . random ( ) * this . columns ) ;
38+ // TODO: validate that the pair row, col never repeats
3839 minesCoordinates . push ( [ col , row ] ) ;
3940 }
4041 }
4142
4243 minesCoordinates . forEach ( ( v ) => {
4344 const col = v [ 0 ] ;
4445 const row = v [ 1 ] ;
45- const square = this . getSquare ( col , row ) ;
46+ const cell = this . getCell ( col , row ) ;
4647
47- if ( ! square ) return ;
48+ if ( ! cell ) return ;
49+ if ( cell . hasBomb ( ) ) { throw new Error ( 'This cell already has a bomb!' ) ; }
4850
49- square . setBomb ( ) ;
51+ cell . setBomb ( ) ;
5052 this . mines ++ ;
5153
5254 // increase adjacent numbers
53- this . getAdjacents ( col , row ) . forEach ( ( square ) => square . increaseNumber ( ) ) ;
55+ this . getAdjacents ( col , row ) . forEach ( ( cell ) => cell . increaseNumber ( ) ) ;
5456 } ) ;
5557 }
5658
5759 getMinesCount ( ) {
5860 return Array . isArray ( this . mines ) ? this . mines . length : this . mines ;
5961 }
6062
61- getSquare ( col , row ) {
63+ getCell ( col , row ) {
6264 if ( col < 0 || col >= this . columns || row < 0 || row >= this . rows ) {
6365 return ;
6466 } else {
@@ -74,9 +76,9 @@ class Board {
7476 if ( x === y && x === 0 ) {
7577 continue ;
7678 }
77- const square = this . getSquare ( col + y , row + x ) ;
78- if ( square ) {
79- adjacents . push ( square ) ;
79+ const cell = this . getCell ( col + y , row + x ) ;
80+ if ( cell ) {
81+ adjacents . push ( cell ) ;
8082 }
8183 }
8284 }
@@ -85,14 +87,14 @@ class Board {
8587 }
8688
8789 revealNumbers ( col , row ) {
88- this . getAdjacents ( col , row ) . forEach ( ( square ) => {
89- if ( ! square . isHidden ) { return ; }
90+ this . getAdjacents ( col , row ) . forEach ( ( cell ) => {
91+ if ( ! cell . isHidden ) { return ; }
9092
9193 this . uncovered ++ ;
92- square . discover ( ) ;
94+ cell . discover ( ) ;
9395
94- if ( square . isEmpty ( ) ) {
95- this . revealNumbers ( square . col , square . row ) ;
96+ if ( cell . isEmpty ( ) ) {
97+ this . revealNumbers ( cell . col , cell . row ) ;
9698 }
9799 } ) ;
98100 }
@@ -105,17 +107,17 @@ class Board {
105107 * @returns {* }
106108 */
107109 play ( col , row , action = 'discover' ) {
108- const square = this . getSquare ( col , row ) ;
109- const value = square [ action ] ( ) ;
110+ const cell = this . getCell ( col , row ) ;
111+ const value = cell [ action ] ( ) ;
110112 if ( action === 'flag' ) { return ; }
111113
112114 this . uncovered ++ ;
113115
114- if ( square . hasBomb ( ) ) {
116+ if ( cell . hasBomb ( ) ) {
115117 throw 'Game Over' ;
116118 }
117119
118- if ( square . isEmpty ( ) ) {
120+ if ( cell . isEmpty ( ) ) {
119121 this . revealNumbers ( col , row ) ;
120122 }
121123
@@ -139,17 +141,20 @@ class Board {
139141 for ( let column = 0 ; column < this . board . length ; column ++ ) {
140142 string += '\n' ;
141143 for ( let row = 0 ; row < this . board [ column ] . length ; row ++ ) {
142- const square = this . board [ column ] [ row ] ;
143- const val = fn ? ( fn instanceof Function ? fn ( square ) : square [ fn ] ) : square . getValue ( ) ;
144+ const cell = this . board [ column ] [ row ] ;
145+ const val = fn ? ( fn instanceof Function ? fn ( cell ) : cell [ fn ] ) : cell . getValue ( ) ;
144146 string += `\t ${ val } ` ;
145147 }
146148 }
147149 return string ;
148150 }
149151}
150152
151- class Square {
152- constructor ( col , row , value = Square . EMPTY ) {
153+ /**
154+ * Cell
155+ */
156+ class Cell {
157+ constructor ( col , row , value = Cell . EMPTY ) {
153158 this . value = value ;
154159 this . col = col ;
155160 this . row = row ;
@@ -158,11 +163,11 @@ class Square {
158163 }
159164
160165 setBomb ( ) {
161- this . value = Square . BOMB ;
166+ this . value = Cell . BOMB ;
162167 }
163168
164169 increaseNumber ( ) {
165- if ( this . value === Square . BOMB ) {
170+ if ( this . value === Cell . BOMB ) {
166171 return ;
167172 }
168173
@@ -176,11 +181,11 @@ class Square {
176181 }
177182
178183 hasBomb ( ) {
179- return this . value === Square . BOMB
184+ return this . value === Cell . BOMB
180185 }
181186
182187 isEmpty ( ) {
183- return this . value === Square . EMPTY ;
188+ return this . value === Cell . EMPTY ;
184189 }
185190
186191 discover ( ) {
@@ -197,7 +202,7 @@ class Square {
197202 }
198203}
199204
200- Square . EMPTY = '.' ;
201- Square . BOMB = '*' ;
205+ Cell . EMPTY = '.' ;
206+ Cell . BOMB = '*' ;
202207
203208module . exports = Minesweeper ;
0 commit comments