Skip to content

Commit e64f677

Browse files
committed
Added Scores, Restart Btn, Styling & Design #Day48
1 parent 96a19b6 commit e64f677

File tree

6 files changed

+56
-17
lines changed

6 files changed

+56
-17
lines changed

minesweeper-game/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
## HTML
44

5-
1. Create <div> with grid class in the body of HTML.
5+
1. Create div with grid class in the body of HTML.
66
2. Elements will be dynamically added to this grid div using JS.
7+
3. Add a result heading to display Scores of player.
8+
4. Create a button Restart that will reload the game.
79

810
## CSS
911

1012
1. Create a grid layout with 10x10 boxes of 40px each, hence grid size will be 400x400px
1113
2. Specify the size of each box separately, using .grid div { height: 40px; width: 40px; }
14+
3. Align the headings & elements to the center.
15+
4. Make text as bold and use border for better visualization of the game's grid.
1216

1317
## JavaScript Code
1418

@@ -26,6 +30,8 @@
2630
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.
2731
14. Use onContextMenu property in createBoard(), that calls addFlag() on right click on a box with bomb element.
2832
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.
33+
16. Update the Scores on click of a valid box in the game.
34+
17. Enable the button restart when the player wins or looses the game.
2935

3036
## Resources
3137

Binary file not shown.

minesweeper-game/game.js

+28-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { checkBox } from "./recurse.js";
22

33
const gridContainer = document.querySelector(".grid");
4+
const result = document.querySelector(".result");
5+
const button = document.getElementById("btn");
6+
47
const GRID_WIDTH = 100;
58
let bombCount = 20;
69
let isGameOver = false;
710
let flag = 0;
11+
let resultCount = 1;
812

913
document.addEventListener("DOMContentLoaded", () => {
1014
createBoard();
@@ -22,7 +26,10 @@ let createBoard = () => {
2226
box.setAttribute("id", i);
2327
box.classList.add(shuffledArray[i]); // add bomb/valid classes to the boxes on the grid
2428
gridContainer.appendChild(box);
25-
box.addEventListener("click", () => click(box));
29+
box.addEventListener("click", () => {
30+
click(box);
31+
if (box.classList.contains("valid")) result.innerHTML = "Your Scores: " + resultCount++;
32+
});
2633
box.oncontextmenu = event => {
2734
event.preventDefault();
2835
addFlag(box); // add flag on right click
@@ -84,12 +91,18 @@ let addNumbersToBoard = () => {
8491
export let click = box => {
8592
if (isGameOver) return;
8693
if (box.classList.contains("checked") || box.classList.contains("flag")) return;
87-
if (box.classList.contains("bomb")) gameOver(box);
94+
if (box.classList.contains("bomb")) gameOver();
8895
if (box.classList.contains("valid")) {
8996
box.classList.add("checked");
9097
let total = box.getAttribute("total");
9198
if (total != 0) {
9299
box.innerHTML = total;
100+
if (total == 1) box.style.color = "blue";
101+
if (total == 2) box.style.color = "green";
102+
if (total == 3) box.style.color = "red";
103+
if (total == 4) box.style.color = "purple";
104+
if (total == 5) box.style.color = "maroon";
105+
if (total == 6) box.style.color = "darkgreen";
93106
return;
94107
}
95108
checkBox(box);
@@ -98,12 +111,17 @@ export let click = box => {
98111
}
99112

100113
// method to declare game Over when player clicks on bomb element.
101-
let gameOver = box => {
102-
alert("game over");
114+
let gameOver = () => {
115+
result.innerHTML = "GAME OVER!";
116+
result.style.color = "red";
103117
isGameOver = true;
104-
for (let box of boxElements) {
105-
if (box.classList.contains("bomb")) box.innerHTML = "💣";
106-
}
118+
for (let box of boxElements)
119+
if (box.classList.contains("bomb")) {
120+
box.innerHTML = "💣";
121+
box.classList.remove('bomb')
122+
box.classList.add('checked')
123+
}
124+
button.style.display = "block";
107125
}
108126

109127
// method to declare game Win when the number of flags matches the bomb count.
@@ -114,7 +132,9 @@ let gameWin = () => {
114132
flagBombMatchCount++;
115133
}
116134
if (flagBombMatchCount === bombCount) {
117-
alert("you won");
135+
result.innerHTML = "CONGRATULATIONS! YOU WON!";
136+
result.style.color = "purple";
137+
button.style.display = "block";
118138
isGameOver = true;
119139
}
120140
}

minesweeper-game/index.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
<head>
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<title>MineSweeper Game</title>
7+
<title>The MineSweeper Game</title>
88
<link rel="stylesheet" href="styles.css"/>
99
</head>
1010

1111
<body>
12+
<h1>The MineSweeper Game</h1>
1213
<div class="grid"></div>
14+
<h2 class="result"></h2>
15+
<button id="btn" onclick="window.location.reload()">Restart</button>
1316
<script src="game.js" type="module"></script>
1417
</body>
1518

minesweeper-game/recurse.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { click, boxElements, width } from "./game.js";
1+
import { click, width } from "./game.js";
22

33
// implementing recursion to display total value of neighbouring boxes of the clicked box
44
export let checkBox = box => {

minesweeper-game/styles.css

+16-6
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,38 @@ body {
44
align-items: center;
55
}
66

7+
h1,h2 {
8+
text-align: center;
9+
}
10+
711
.grid {
812
height: 400px;
913
width: 400px;
1014
display: flex;
11-
margin-top: 100px;
1215
flex-wrap: wrap;
13-
background-color: rosybrown;
16+
background-color: rgb(180, 194, 209);
1417
}
1518

1619
.grid div {
17-
height: 40px;
18-
width: 40px;
20+
border: 2px solid rgb(100, 100, 100);
21+
height: 36px;
22+
width: 36px;
23+
font-size: 25px;
1924
}
2025

2126
.bomb {
22-
background-color: purple;
2327
text-align: center;
2428
}
2529

2630
.checked {
27-
background-color: rgb(243, 215, 238);
31+
background-color: rgb(250, 234, 247);
2832
text-align: center;
33+
font-weight: bold;
34+
}
35+
36+
#btn {
37+
display: none;
38+
font-weight: bolder;
2939
}
3040

3141
/* each box to be of 40px, and grid has 10x10 boxes*/

0 commit comments

Comments
 (0)