aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAiden Woodruff <aiden.woodruff@gmail.com>2018-06-29 18:08:10 -0500
committerAiden Woodruff <aiden.woodruff@gmail.com>2018-06-29 18:08:10 -0500
commit636977dc6093f65154fd50848e86e92c2ccaa1d0 (patch)
tree0e34b711bc7daae6ca03782a772a06d7e250226b
parentc8d0e69e0407b8638d888d3efb6742ddc5a3ed0c (diff)
downloadsweeper-636977dc6093f65154fd50848e86e92c2ccaa1d0.tar.gz
sweeper-636977dc6093f65154fd50848e86e92c2ccaa1d0.tar.bz2
sweeper-636977dc6093f65154fd50848e86e92c2ccaa1d0.zip
Fixed issue where onscreen Mine count didn't reincrease if flag was clicked
Added function unflagged that updates screen value. Signed-off-by: Aiden Woodruff <aiden.woodruff@gmail.com>
-rw-r--r--ChangeLog7
-rw-r--r--sweeper.js18
2 files changed, 18 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index d171fb8..13454de 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
12018-06-29 Aiden Woodruff <aiden.woodruff@gmail.com>
2
3 * sweeper.js (unflagged): Function added. Scans and updates Mine count on screen.
4 (flag): Calls unflagged()
5 (reveal): Calls unflagged() if item is flagged.
6 (populate_board): Don't do anything with unflagged.
7
12018-06-28 Aiden Woodruff <aiden.woodruff@gmail.com> 82018-06-28 Aiden Woodruff <aiden.woodruff@gmail.com>
2 9
3 * index.html: Fixed meta with no close bracket. 10 * index.html: Fixed meta with no close bracket.
diff --git a/sweeper.js b/sweeper.js
index 1a67b54..966a533 100644
--- a/sweeper.js
+++ b/sweeper.js
@@ -1,7 +1,6 @@
1// Global variables 1// Global variables
2var boardmap = Array(); 2var boardmap = Array();
3var mines = Array(); 3var mines = Array();
4var unflagged = 0;
5var table = ""; 4var table = "";
6var height = 0; 5var height = 0;
7var width = 0; 6var width = 0;
@@ -83,6 +82,12 @@ function get_cell (x, y) {
83 return document.getElementById( (x.toString() + "," + y.toString() ) ); 82 return document.getElementById( (x.toString() + "," + y.toString() ) );
84} 83}
85 84
85// Updates unflagged (or really, unrevealed) mine count
86function unflagged () {
87 document.getElementById("minecount").innerHTML = "Mines: " + (total_mines - Array.concat.apply([], boardmap).filter(x => x.status === "F").length).toString();
88 return true;
89}
90
86// Return x y dict from cell object 91// Return x y dict from cell object
87function get_cell_xy (object) { 92function get_cell_xy (object) {
88 coord = new Coordinates(); 93 coord = new Coordinates();
@@ -221,8 +226,11 @@ function reveal (e) {
221 coord = get_cell_xy(object); 226 coord = get_cell_xy(object);
222 if (boardmap[coord.y][coord.x].status === "R") { 227 if (boardmap[coord.y][coord.x].status === "R") {
223 return; 228 return;
224 } else if (boardmap[coord.y][coord.x].status === "U" || boardmap[coord.y][coord.x].status === "F") { 229 } else if (boardmap[coord.y][coord.x].status === "U") {
225 boardmap[coord.y][coord.x].status = "R"; 230 boardmap[coord.y][coord.x].status = "R";
231 } else if (boardmap[coord.y][coord.x].status === "F") {
232 boardmap[coord.y][coord.x].status = "R";
233 unflagged();
226 } 234 }
227 object.classList.remove("flagged"); 235 object.classList.remove("flagged");
228 object.classList.remove("unrevealed"); 236 object.classList.remove("unrevealed");
@@ -295,15 +303,13 @@ function flag (e) {
295 boardmap[coord.y][coord.x].status = "U"; 303 boardmap[coord.y][coord.x].status = "U";
296 object.classList.remove("flagged"); 304 object.classList.remove("flagged");
297 object.classList.add("unrevealed"); 305 object.classList.add("unrevealed");
298 unflagged++;
299 } else if (boardmap[coord.y][coord.x].status === "U") { 306 } else if (boardmap[coord.y][coord.x].status === "U") {
300 boardmap[coord.y][coord.x].status = "F"; 307 boardmap[coord.y][coord.x].status = "F";
301 object.classList.remove("unrevealed"); 308 object.classList.remove("unrevealed");
302 object.classList.add("flagged"); 309 object.classList.add("flagged");
303 unflagged--;
304 } else if (boardmap[coord.y][coord.x].status === "R") { 310 } else if (boardmap[coord.y][coord.x].status === "R") {
305 } 311 }
306 document.getElementById("minecount").innerHTML = "Mines: " + unflagged.toString(); 312 unflagged();
307} 313}
308 314
309function update_leaderboard () { 315function update_leaderboard () {
@@ -401,8 +407,6 @@ function populate_board (e) {
401 mines = mines.filter(value => { return ! value.equals(disclude); }); 407 mines = mines.filter(value => { return ! value.equals(disclude); });
402 } 408 }
403 409
404 unflagged = mines.length;
405
406 // Place mines 410 // Place mines
407 for (var i = 0; i < mines.length; i++) { 411 for (var i = 0; i < mines.length; i++) {
408 boardmap[mines[i].y][mines[i].x].value = "M"; 412 boardmap[mines[i].y][mines[i].x].value = "M";