forked from pranjay-poddar/Dev-Geeks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper.html
139 lines (129 loc) · 3.87 KB
/
Minesweeper.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Minesweeper</title>
<style>
BODY {
background: black;
color: #DDDDDD;
font-family: courier new;
text-align: center;
}
H1 {
text-align: center;
font-size: 14pt;
font-weight: normal;
}
#grid {
margin-left: auto;
margin-right: auto;
}
#grid TR TD{
border:1px solid white;
background: #999999;
width: 16px;
height: 16px;
text-align: center;
}
#grid TR TD.clicked {
background: #333333;
}
#grid TR TD.mine {
background: #FF0000;
}
BUTTON {
margin: 12px;
}
</style>
</head>
<body>
<H1>Minesweeper</H1>
<table id=grid>
</table>
<BUTTON onclick="generateGrid();">Reset Grid</BUTTON>
</body>
<script>//minesweeper game by 101computing.net - www.101computing.et/minesweeper-in-javascript/
var grid = document.getElementById("grid");
var testMode = false; //Turn this variable to true to see where the mines are
generateGrid();
function generateGrid() {
//generate 10 by 10 grid
grid.innerHTML="";
for (var i=0; i<10; i++) {
row = grid.insertRow(i);
for (var j=0; j<10; j++) {
cell = row.insertCell(j);
cell.onclick = function() { clickCell(this); };
var mine = document.createAttribute("data-mine");
mine.value = "false";
cell.setAttributeNode(mine);
}
}
addMines();
}
function addMines() {
//Add mines randomly
for (var i=0; i<20; i++) {
var row = Math.floor(Math.random() * 10);
var col = Math.floor(Math.random() * 10);
var cell = grid.rows[row].cells[col];
cell.setAttribute("data-mine","true");
if (testMode) cell.innerHTML="X";
}
}
function revealMines() {
//Highlight all mines in red
for (var i=0; i<10; i++) {
for(var j=0; j<10; j++) {
var cell = grid.rows[i].cells[j];
if (cell.getAttribute("data-mine")=="true") cell.className="mine";
}
}
}
function checkLevelCompletion() {
var levelComplete = true;
for (var i=0; i<10; i++) {
for(var j=0; j<10; j++) {
if ((grid.rows[i].cells[j].getAttribute("data-mine")=="false") && (grid.rows[i].cells[j].innerHTML=="")) levelComplete=false;
}
}
if (levelComplete) {
alert("You Win!");
revealMines();
}
}
function clickCell(cell) {
//Check if the end-user clicked on a mine
if (cell.getAttribute("data-mine")=="true") {
revealMines();
alert("Game Over");
} else {
cell.className="clicked";
//Count and display the number of adjacent mines
var mineCount=0;
var cellRow = cell.parentNode.rowIndex;
var cellCol = cell.cellIndex;
//alert(cellRow + " " + cellCol);
for (var i=Math.max(cellRow-1,0); i<=Math.min(cellRow+1,9); i++) {
for(var j=Math.max(cellCol-1,0); j<=Math.min(cellCol+1,9); j++) {
if (grid.rows[i].cells[j].getAttribute("data-mine")=="true") mineCount++;
}
}
cell.innerHTML=mineCount;
if (mineCount==0) {
//Reveal all adjacent cells as they do not have a mine
for (var i=Math.max(cellRow-1,0); i<=Math.min(cellRow+1,9); i++) {
for(var j=Math.max(cellCol-1,0); j<=Math.min(cellCol+1,9); j++) {
//Recursive Call
if (grid.rows[i].cells[j].innerHTML=="") clickCell(grid.rows[i].cells[j]);
}
}
}
checkLevelCompletion();
}
}
</script>
</html>