1- import { arraysCompare , genCells , getNeighborCells } from " ./helpers" ;
1+ import { arraysCompare , genCells , getNeighborCells } from ' ./helpers' ;
22
33export class Minesweeper {
44 constructor ( arena , minesCount , redrawFn ) {
@@ -25,19 +25,32 @@ export class Minesweeper {
2525 } ;
2626
2727 handleCellClick = i => {
28- if ( this . gameState !== " playing" ) return ;
28+ if ( this . gameState !== ' playing' ) return ;
2929
3030 const cell = this . cells [ i ] ;
3131
32+ if ( this . flaggingMode && ! cell . opened ) {
33+ this . flagCell ( i ) ;
34+ return ;
35+ }
36+
3237 if ( cell . flagged ) return ;
3338
3439 if ( cell . opened ) this . _openNeighborCells ( i ) ;
3540 else this . _openCell ( i ) ;
3641 this . _render ( ) ;
3742 } ;
3843
44+ toggleFlaggingMode = ( ) => {
45+ if ( this . gameState === 'playing' ) {
46+ this . flaggingMode = ! this . flaggingMode ;
47+ this . _render ( ) ;
48+ }
49+ }
50+
3951 _explode = i => {
40- this . gameState = "lost" ;
52+ this . gameState = 'lost' ;
53+ this . flaggingMode = false ;
4154 this . endTime = Date . now ( ) ;
4255 } ;
4356
@@ -73,7 +86,8 @@ export class Minesweeper {
7386 const openedCells = this . cells . filter ( ( { opened } ) => ! opened ) ;
7487
7588 if ( arraysCompare ( this . minedCells , openedCells ) ) {
76- this . gameState = "won" ;
89+ this . gameState = 'won' ;
90+ this . flaggingMode = false ;
7791 this . stopTimer ( ) ;
7892 }
7993 }
@@ -97,7 +111,7 @@ export class Minesweeper {
97111 } ;
98112
99113 flagCell = i => {
100- if ( this . gameState !== " playing" ) return ;
114+ if ( this . gameState !== ' playing' ) return ;
101115
102116 const cell = this . cells [ i ] ;
103117 if ( ! cell . opened ) {
@@ -127,19 +141,21 @@ export class Minesweeper {
127141 opened : this . openedCells ,
128142 flagged : this . flaggedCells ,
129143 timerValue : this . startTime ? ( this . endTime || Date . now ( ) ) - this . startTime : null ,
130- endTime : this . endTime
144+ endTime : this . endTime ,
145+ flaggingMode : this . flaggingMode
131146 } ;
132147 }
133148
134149 reset = ( ) => {
135150 this . stopTimer ( ) ;
136151 this . openedCells = 0 ;
137152 this . flaggedCells = 0 ;
138- this . gameState = " playing" ;
153+ this . gameState = ' playing' ;
139154 this . cells = genCells ( this . arena , 0 , 0 ) ;
140155 this . minedCells = [ ] ;
141156 this . startTime = null ;
142157 this . endTime = null ;
158+ this . flaggingMode = false ;
143159 this . _render ( ) ;
144160 } ;
145161
0 commit comments