Skip to content

Commit 96a19b6

Browse files
committed
Added gameWin() & addFlag() for right click event #Day47
1 parent a08e3a6 commit 96a19b6

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

minesweeper-game/README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@
1414

1515
1. Create a constant grid Container using querySelector(".grid"), and a const GAME_WIDTH to specify the game size while creating the game board.
1616
2. Use document.addEventListener("DOMContentLoaded"()=>{ } ) for the rest of the game functionality.
17-
3. Create the board by adding 100 boxes to it, using createElement(), setAttribute() & AppendChild() in the for loop.
17+
3. Create the board using createBoard() method, by adding 100 boxes to it, using createElement(), setAttribute() & AppendChild() in the for loop.
1818
4. Create a ShuffledArray that includes bomb & other elements at random position, and add it to each box in the for loop using box.classList.add().
1919
5. In method addNumbersToBoard(), specify conditions to add number of neighbouring bombs to the each valid (non-bomb) box.
2020
7. Use setAttribute() to add the "total" attribute and it's value to each valid box element.
21-
8. Add eventListener for click event, and add a new class "checked" on click of a valid box.
21+
8. Add eventListener for click event in createBoard(), and call a new method click(). Then add a new class "checked" on click of a valid box.
2222
9. Assign value of "total" to the valid box using getAttribute(), and then call a new method checkBox() for recursive steps.
2323
10. Create a new file recurse.js and export the checkBox() method.
24+
11. Implement recursion in the checkBox() method, to display total value of neighbouring boxes of the clicked box. The recursion continues until and edge or a bomb is the neighbouring element.
25+
12. Create a gameOver() method, the assigns true to isGameOver variable, and displays all the bombs once a bomb is clicked by the player.
26+
13. Create addFlag() method, to add flags on the boxes that have bomb element and are right clicked by the player, to mark as safe.
27+
14. Use onContextMenu property in createBoard(), that calls addFlag() on right click on a box with bomb element.
28+
15. Create a gameWin() method to check if the number of flags added by the player matches the total number of bombs. If yes, then the player is declared as Winner. This method is called everytime a flag is added to the game board.
2429

2530
## Resources
2631

2732
1. [MineSweeper using JS](https://www.youtube.com/watch?v=W0No1JDc6vE&t)
33+
2. [About MineSweeper](https://en.wikipedia.org/wiki/Minesweeper_(video_game))

minesweeper-game/game.js

+39
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const gridContainer = document.querySelector(".grid");
44
const GRID_WIDTH = 100;
55
let bombCount = 20;
66
let isGameOver = false;
7+
let flag = 0;
78

89
document.addEventListener("DOMContentLoaded", () => {
910
createBoard();
@@ -22,12 +23,33 @@ let createBoard = () => {
2223
box.classList.add(shuffledArray[i]); // add bomb/valid classes to the boxes on the grid
2324
gridContainer.appendChild(box);
2425
box.addEventListener("click", () => click(box));
26+
box.oncontextmenu = event => {
27+
event.preventDefault();
28+
addFlag(box); // add flag on right click
29+
}
2530
}
2631
}
2732

2833
export const boxElements = gridContainer.children;
2934
export const width = 10; // width of one column
3035

36+
// method to add flags when right clicked on a box containing bomb.
37+
let addFlag = box => {
38+
if (isGameOver) return;
39+
if (!box.classList.contains("checked") && (flag < bombCount)) {
40+
if (!box.classList.contains("flag")) {
41+
box.classList.add("flag");
42+
box.innerHTML = "🚩";
43+
flag++;
44+
gameWin();
45+
} else {
46+
box.classList.remove("flag");
47+
box.innerHTML = "";
48+
flag--;
49+
}
50+
}
51+
}
52+
3153
// method to add number of neighbouring bombs to the each valid (non-bomb) box.
3254
let addNumbersToBoard = () => {
3355
for (let box of boxElements) {
@@ -75,6 +97,7 @@ export let click = box => {
7597
box.classList.add("checked");
7698
}
7799

100+
// method to declare game Over when player clicks on bomb element.
78101
let gameOver = box => {
79102
alert("game over");
80103
isGameOver = true;
@@ -83,8 +106,24 @@ let gameOver = box => {
83106
}
84107
}
85108

109+
// method to declare game Win when the number of flags matches the bomb count.
110+
let gameWin = () => {
111+
let flagBombMatchCount = 0;
112+
for (let box of boxElements) {
113+
if (box.classList.contains("flag") && box.classList.contains("bomb")) {
114+
flagBombMatchCount++;
115+
}
116+
if (flagBombMatchCount === bombCount) {
117+
alert("you won");
118+
isGameOver = true;
119+
}
120+
}
121+
}
122+
86123
/* Learnings:
87124
1. fill() method changes all elements in an array to the specified static value.
88125
2. parseInt() used to convert the given value to Integer.
89126
3. contains() to check if the element has the specified value.
127+
4. getAttribute() used to access the attribute specified for the given element.
128+
5. onContextMenu property fires when the player clicks the right mouse button, opening the context menu.
90129
*/

0 commit comments

Comments
 (0)