1- import { arraysCompare , genCells , getNeighborCells } from './helpers' ;
1+ import {
2+ arraysCompare ,
3+ genCells ,
4+ getNeighborCells ,
5+ CELL_FLAGGED ,
6+ CELL_MINED ,
7+ CELL_OPENED ,
8+ getCellNeighborMines ,
9+ toggleCellFlag
10+ } from './helpers' ;
211
312export class Minesweeper {
413 constructor ( arena , minesCount , redrawFn ) {
@@ -29,14 +38,14 @@ export class Minesweeper {
2938
3039 const cell = this . cells [ i ] ;
3140
32- if ( this . flaggingMode && ! cell . opened ) {
41+ if ( this . flaggingMode && ! ( cell & CELL_OPENED ) ) {
3342 this . flagCell ( i ) ;
3443 return ;
3544 }
3645
37- if ( cell . flagged ) return ;
46+ if ( cell & CELL_FLAGGED ) return ;
3847
39- if ( cell . opened ) this . _openNeighborCells ( i ) ;
48+ if ( cell & CELL_OPENED ) this . _openNeighborCells ( i ) ;
4049 else this . _openCell ( i ) ;
4150 this . _render ( ) ;
4251 } ;
@@ -58,17 +67,18 @@ export class Minesweeper {
5867 const cell = this . cells [ i ] ;
5968
6069 if ( this . openedCells === 0 ) {
61- this . cells = genCells ( this . arena , this . minesCountTotal , i ) ;
62- this . minedCells = this . cells . filter ( ( { mined } ) => mined ) ;
70+ const [ cells , minedCells ] = genCells ( this . arena , this . minesCountTotal , i ) ;
71+ this . cells = cells ;
72+ this . minedCells = minedCells ;
6373 this . startTimer ( ) ;
6474 }
65- if ( cell . mined ) {
75+ if ( cell & CELL_MINED ) {
6676 this . _explode ( i ) ;
67- } else if ( ! cell . opened ) {
77+ } else if ( ! ( cell & CELL_OPENED ) ) {
6878 let cellsToOpen = [ i ] ;
6979
7080 for ( let x = 0 ; x < cellsToOpen . length ; x ++ ) {
71- if ( ! this . cells [ cellsToOpen [ x ] ] . neighborMines ) {
81+ if ( ! getCellNeighborMines ( this . cells [ cellsToOpen [ x ] ] ) ) {
7282 const neighborCells = getNeighborCells (
7383 this . arena ,
7484 cellsToOpen [ x ]
@@ -79,13 +89,16 @@ export class Minesweeper {
7989 }
8090
8191 cellsToOpen . forEach ( i => {
82- this . cells [ i ] . opened = true ;
92+ this . cells [ i ] = ( this . cells [ i ] | CELL_OPENED )
8393 this . openedCells ++ ;
8494 } ) ;
8595
86- const openedCells = this . cells . filter ( ( { opened } ) => ! opened ) ;
96+ const restCells = this . cells
97+ . map ( ( cell , i ) => ( cell & CELL_OPENED ) ? null : i )
98+ . filter ( cell => cell !== null ) ;
8799
88- if ( arraysCompare ( this . minedCells , openedCells ) ) {
100+ if ( arraysCompare ( this . minedCells , restCells ) ) {
101+
89102 this . gameState = 'won' ;
90103 this . flaggingMode = false ;
91104 this . stopTimer ( ) ;
@@ -95,16 +108,16 @@ export class Minesweeper {
95108
96109 _openNeighborCells = i => {
97110 const cell = this . cells [ i ] ;
98- if ( ! cell . mined ) {
111+ if ( ! ( cell & CELL_MINED ) ) {
99112 const neighborCells = getNeighborCells ( this . arena , i ) ;
100113 const flaggedNeighborsCount = neighborCells
101114 . map ( i => this . cells [ i ] )
102- . filter ( ( { flagged } ) => ! ! flagged ) . length ;
115+ . filter ( cell => ! ! ( cell & CELL_FLAGGED ) ) . length ;
103116
104- if ( flaggedNeighborsCount === cell . neighborMines ) {
117+ if ( flaggedNeighborsCount === getCellNeighborMines ( cell ) ) {
105118 neighborCells
106119 . map ( i => [ i , this . cells [ i ] ] )
107- . filter ( ( [ i , cell ] ) => ! cell . flagged )
120+ . filter ( ( [ i , cell ] ) => ! ( cell & CELL_FLAGGED ) )
108121 . forEach ( ( [ i ] ) => this . _openCell ( i ) ) ;
109122 }
110123 }
@@ -114,9 +127,9 @@ export class Minesweeper {
114127 if ( this . gameState !== 'playing' ) return ;
115128
116129 const cell = this . cells [ i ] ;
117- if ( ! cell . opened ) {
118- cell . flagged = ! cell . flagged ;
119- cell . flagged ? this . flaggedCells ++ : this . flaggedCells -- ;
130+ if ( ! ( cell & CELL_OPENED ) ) {
131+ this . cells [ i ] = toggleCellFlag ( cell ) ;
132+ ( this . cells [ i ] & CELL_FLAGGED ) ? this . flaggedCells ++ : this . flaggedCells -- ;
120133 }
121134 this . _render ( ) ;
122135 } ;
@@ -126,11 +139,11 @@ export class Minesweeper {
126139 }
127140
128141 _countFlaggedCells ( ) {
129- return this . cells . filter ( ( { flagged } ) => flagged === true ) . length ;
142+ return this . cells . filter ( cell => ! ! ( cell & CELL_FLAGGED ) ) . length ;
130143 }
131144
132145 _countOpenedCells ( ) {
133- return this . cells . filter ( ( { opened } ) => opened === true ) . length ;
146+ return this . cells . filter ( cell => ! ! ( cell & CELL_OPENED ) ) . length ;
134147 }
135148
136149 getStats ( ) {
@@ -147,12 +160,13 @@ export class Minesweeper {
147160 }
148161
149162 reset = ( ) => {
163+ const [ cells , minedCells ] = genCells ( this . arena , 0 , 0 ) ;
150164 this . stopTimer ( ) ;
151165 this . openedCells = 0 ;
152166 this . flaggedCells = 0 ;
153167 this . gameState = 'playing' ;
154- this . cells = genCells ( this . arena , 0 , 0 ) ;
155- this . minedCells = [ ] ;
168+ this . cells = cells ;
169+ this . minedCells = minedCells ;
156170 this . startTime = null ;
157171 this . endTime = null ;
158172 this . flaggingMode = false ;
0 commit comments