forked from nsevindi87/minesweeper-game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
192 lines (165 loc) · 5.33 KB
/
script.js
File metadata and controls
192 lines (165 loc) · 5.33 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Set this constant to true to debug the placement of bombs without
// having to click on all cells to reveal them.
const CHEAT_REVEAL_ALL = false;
const ROWS_COUNT = 10;
const COLS_COUNT = 10;
var defeat = false;
var victory = false;
// Cell constructor
function Cell() {
this.discovered = false;
this.isBomb = false;
this.hasBeenFlagged = false;
}
// Initialize cells
var cells = Array(ROWS_COUNT);
for (var row = 0; row < ROWS_COUNT; row++) {
cells[row] = Array(COLS_COUNT);
for (var col = 0; col < COLS_COUNT; col++) {
cells[row][col] = new Cell();
}
}
//
// TODO: Task 1 - add some bombs at fixed positions.
// cells[0][1].isBomb = true;
// cells[5][4].isBomb = true;
// cells[9][9].isBomb = true;
//
// TODO: Task 2 - Comment out the code of task 1. Instead of adding bombs in fixed places, add 10 of them in random places.
// Add a BOMBS_COUNT constant so that you can easily change the amount of bombs placed. Put it next to the
// other constants.
//
// Once the game has been initialized, we "render" it.
render();
//
// Game functions definitions
//
function discoverCell(row, col) {
//
// TODO: Task 5 - Reveal cells when clicked.
//
//
// TODO: Task 6 - Discover neighbor cells recursively, as long as there are no adjacent bombs to the current cell.
//
//
// TODO: Task 8 - Implement defeat. If the player "discovers" a bomb (clicks on it without holding shift), set the variable defeat to true.
//
}
function flagCell(row, col) {
//
// TODO: Task 7 - Implement flags. Flags allow the player to mark cells that they think contain a bomb.
// When clicking a cell and holding shift, function flagCell() will be called for you.
//
}
// This function is called once for each cell when rendering the game. The row and col of the current cell is
// passed to the functionn
function countAdjacentBombs(row, col) {
//
// TODO: Task 4 - Adjacent bombs are bombs in cells touching our cell (also diagonally). Implement this function
// so that it returns the count of adjacent cells with bombs in them.
//
return 1;
}
function getBombsCount() {
//
// TODO: Task 9 - Implement stats: the counters currently always display 0, calculate and return the relevant values.
//
return 0;
}
function getClearedCells() {
//
// TODO: Task 9 - Implement stats: the counters currently always display 0, calculate and return the relevant values.
//
return 0;
}
function getTotalCellsToClear() {
//
// TODO: Task 9 - Implement stats: the counters currently always display 0, calculate and return the relevant values.
//
return 0;
}
function checkForVictory() {
//
// TODO: Task 10 - Implement victory. If the player has revealed as many cells as they must (every cell that isn't a
// bomb), set variable victory to true.
//
return 0;
}
//
// Rendering functions
//
function getMessage() {
if (victory == true) {
return "Well done! 👏🏼<br><br>Refresh the page to start again.";
} else if (defeat) {
return "Boom! 💥<br><br>Refresh the page to try again.";
}
return "";
}
// "Render" the game. Update the content of the page to reflect any changes to the game state.
function render() {
var playfield = document.getElementById("playfield");
var html = "";
for (var row = 0; row < ROWS_COUNT; row++) {
html += '<div class="row">';
for (var col = 0; col < COLS_COUNT; col++) {
var cell = cells[row][col];
var cellText = "";
var cssClass = "";
var textColor = "";
if (cell.discovered || CHEAT_REVEAL_ALL || defeat) {
cssClass = "discovered";
if (cell.isBomb) {
cellText = "💣";
} else {
var adjBombs = countAdjacentBombs(row, col);
if (adjBombs > 0) {
cellText = adjBombs.toString();
if (adjBombs == 1) {
textColor = "blue";
} else if (adjBombs == 2) {
textColor = "green";
} else if (adjBombs == 3) {
textColor = "red";
} else if (adjBombs == 4) {
textColor = "black";
}
}
}
} else {
if (cell.hasBeenFlagged) {
cellText = "🚩"
}
}
html += `<div class="cell ${cssClass}" style="color:${textColor}" onclick="onCellClicked(${row}, ${col}, event)">${cellText}</div>`;
}
html += "</div>"
}
playfield.innerHTML = html;
// Defeat screen
var body = document.getElementsByTagName("body")[0];
if (defeat) {
body.classList.add("defeat")
}
// Victory screen
if (victory) {
body.classList.add("victory")
}
// Update stats
document.getElementById("bombs-count").innerText = getBombsCount().toString();
document.getElementById("cleared-cells-count").innerText = getClearedCells().toString();
document.getElementById("total-cells-to-clear").innerText = getTotalCellsToClear().toString();
// Update message
document.getElementById("message").innerHTML = getMessage();
}
// This function gets called each time a cell is clicked. Arguments "row" and "col" will be set to the relevant
// values. Argument "event" is used to check if the shift key was held during the click.
function onCellClicked(row, col, event) {
if (event.shiftKey) {
flagCell(row, col);
} else {
discoverCell(row, col);
}
checkForVictory();
render();
}