1+ /** @format */
2+
3+ const grid = document . getElementById ( "grid" ) ;
4+ let lockGame = false ;
5+ const testMode = false ;
6+
7+ generateGrid ( ) ;
8+
9+ // Generate the Game grid
10+ function generateGrid ( ) {
11+ lockGame = false ;
12+ grid . innerHTML = "" ;
13+
14+ // Create a 10x10 grid
15+ for ( let i = 0 ; i < 10 ; i ++ ) {
16+ const row = grid . insertRow ( i ) ;
17+ for ( let j = 0 ; j < 10 ; j ++ ) {
18+ const cell = row . insertCell ( j ) ;
19+ cell . addEventListener ( "click" , ( ) => init ( cell ) ) ; // Add click event listener to each cell
20+ cell . dataset . mine = "false" ; // set Custom data attribute "mine" to false (no mine)
21+ }
22+ }
23+ generateMines ( ) ; //Call Function to randomly generate mines
24+ }
25+
26+ // Generate Random mines
27+ function generateMines ( ) {
28+ for ( let i = 0 ; i < 20 ; i ++ ) {
29+ const row = Math . floor ( Math . random ( ) * 10 ) ; // Generate a random row index (0-9)
30+ const col = Math . floor ( Math . random ( ) * 10 ) ; // Generate a random column index (0-9)
31+ const cell = grid . rows [ row ] . cells [ col ] ;
32+ cell . dataset . mine = "true" ;
33+ if ( testMode ) {
34+ cell . innerHTML = "X" ;
35+ }
36+ }
37+ }
38+
39+ // Reveal all mines on the Grid
40+ function revealMines ( ) {
41+ grid . querySelectorAll ( "[data-mine='true']" ) . forEach ( ( cell ) => {
42+ cell . classList . add ( "mine" ) ;
43+ } ) ;
44+ }
45+
46+ // Check if the game is complete (all non-mine cells revealed)
47+ function checkGameComplete ( ) {
48+ const cells = grid . querySelectorAll ( "td" ) ;
49+ const gameComplete = [ ...cells ] . every ( ( cell ) => {
50+ return (
51+ ( cell . dataset . mine === "true" && cell . classList . contains ( "mine" ) ) ||
52+ ( cell . dataset . mine === "false" && cell . innerHTML !== "" )
53+ ) ;
54+ } ) ;
55+
56+ if ( gameComplete ) {
57+ alert ( "You Found All Mines!" ) ;
58+ revealMines ( ) ;
59+ }
60+ }
61+
62+ function init ( cell ) {
63+ if ( lockGame ) {
64+ return ;
65+ }
66+
67+ if ( cell . dataset . mine === "true" ) {
68+ revealMines ( ) ;
69+ lockGame = true ;
70+ } else {
71+ cell . classList . add ( "active" ) ;
72+
73+ const mineCount = getMineCount ( cell ) ;
74+ cell . innerHTML = mineCount || "0" ; //Display mine count or empty string if zero
75+
76+ if ( mineCount === 0 ) {
77+ const cellRow = cell . parentNode . rowIndex ;
78+ const cellCol = cell . cellIndex ;
79+
80+ // loop through neighboring cells and recursively reveal them
81+ for ( let i = Math . max ( cellRow - 1 , 0 ) ; i <= Math . min ( cellRow + 1 , 9 ) ; i ++ ) {
82+ for ( let j = Math . max ( cellCol - 1 , 0 ) ; j <= Math . min ( cellCol + 1 , 9 ) ; j ++ ) {
83+ if ( neighborCell . innerHTML === "" ) ;
84+ }
85+ }
86+ }
87+
88+ checkGameComplete ( ) ; //Check if the Game is complete
89+ }
90+ }
91+
92+ // Calculate the number of adjacent mines for given cell
93+ function getMineCount ( cell ) {
94+ const cellRow = cell . parentNode . rowIndex ;
95+ const cellCol = cell . cellIndex ;
96+ let mineCount = 0 ;
97+
98+ for ( let i = Math . max ( cellRow - 1 , 0 ) ; i <= Math . min ( cellRow + 1 , 9 ) ; i ++ ) {
99+ for ( let j = Math . max ( cellCol - 1 , 0 ) ; j <= Math . min ( cellCol + 1 , 9 ) ; j ++ ) {
100+ const neighborCell = grid . rows [ i ] . cells [ j ] ;
101+ if ( neighborCell . dataset . mine === "true" ) {
102+ mineCount ++ ;
103+ }
104+ }
105+ }
106+ return mineCount ;
107+ }
0 commit comments