diff options
| author | Aiden Woodruff <aiden.woodruff@gmail.com> | 2018-04-15 10:50:47 -0500 |
|---|---|---|
| committer | Aiden Woodruff <aiden.woodruff@gmail.com> | 2018-04-15 10:50:47 -0500 |
| commit | 3bd16385a7d8df9feeb328a3bb5c689c4b458919 (patch) | |
| tree | 010a3c03d69467527f33ca71f9202a93a3823293 | |
| parent | 4bb6fc44460ba3053080ee2c45da196f55af4060 (diff) | |
| download | sweeper-3bd16385a7d8df9feeb328a3bb5c689c4b458919.tar.gz sweeper-3bd16385a7d8df9feeb328a3bb5c689c4b458919.tar.bz2 sweeper-3bd16385a7d8df9feeb328a3bb5c689c4b458919.zip | |
Fixed issue where you could win by flagging every tile
Now you must flag all mines and only those tiles
Updated constructor for Coordinates class
Signed-off-by: Aiden Woodruff <aiden.woodruff@gmail.com>
| -rw-r--r-- | ChangeLog | 8 | ||||
| -rw-r--r-- | sweeper.js | 36 |
2 files changed, 36 insertions, 8 deletions
| @@ -1,3 +1,11 @@ | |||
| 1 | 2018-04-15 <aiden.woodruff@gmail.com> | ||
| 2 | |||
| 3 | * sweeper.js (Coordinates.constructor): Added constructor to autoconvert string x and y. | ||
| 4 | Added option to make new object from old object. | ||
| 5 | (Coordinates.copy): Returns new object. | ||
| 6 | (flag): Update minecount even on win. | ||
| 7 | Don't win unless only mines are flagged. | ||
| 8 | |||
| 1 | 2018-04-14 Aiden Woodruff <aiden.woodruff@gmail.com> | 9 | 2018-04-14 Aiden Woodruff <aiden.woodruff@gmail.com> |
| 2 | 10 | ||
| 3 | * flag.png: Flag image added. | 11 | * flag.png: Flag image added. |
| @@ -127,11 +127,25 @@ function flag (e) { | |||
| 127 | } else if (boardmap[coord.y][coord.x].state === "R") { | 127 | } else if (boardmap[coord.y][coord.x].state === "R") { |
| 128 | } | 128 | } |
| 129 | var allgone = true; | 129 | var allgone = true; |
| 130 | for (var i = 0; i < dimensions["mines"]; i++) { | 130 | var flags = Array(); |
| 131 | if (boardmap[mines[i].y][mines[i].x].state !== "F") { | 131 | for (var y = 0; y < dimensions["height"]; y++) { |
| 132 | allgone = false; | 132 | for (var x = 0; x < dimensions["width"]; x++) { |
| 133 | if (boardmap[y][x].state === "F") { | ||
| 134 | flags.push(new Coordinates(x, y)); | ||
| 135 | } | ||
| 133 | } | 136 | } |
| 134 | } | 137 | } |
| 138 | if (Math.sign(flags.length - mines.length)) { | ||
| 139 | allgone = false; | ||
| 140 | } else { | ||
| 141 | for (var i = 0; i < flags.length; i++) { | ||
| 142 | if (! flags[i].in_arr(mines)){ | ||
| 143 | allgone = false; | ||
| 144 | break; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | document.getElementById("minecount").innerHTML = "Mines: " + unflagged.toString(); | ||
| 135 | if (allgone) { | 149 | if (allgone) { |
| 136 | for (var y = 0; y < dimensions["height"]; y++) { | 150 | for (var y = 0; y < dimensions["height"]; y++) { |
| 137 | for (var x = 0; x < dimensions["width"]; x++) { | 151 | for (var x = 0; x < dimensions["width"]; x++) { |
| @@ -143,8 +157,6 @@ function flag (e) { | |||
| 143 | } | 157 | } |
| 144 | document.getElementById("board").innerHTML += ""; | 158 | document.getElementById("board").innerHTML += ""; |
| 145 | document.getElementById("end").innerHTML = "<br><h3>Congratulations! You win! :)</br>"; | 159 | document.getElementById("end").innerHTML = "<br><h3>Congratulations! You win! :)</br>"; |
| 146 | } else { | ||
| 147 | document.getElementById("minecount").innerHTML = "Mines: " + unflagged.toString(); | ||
| 148 | } | 160 | } |
| 149 | } | 161 | } |
| 150 | 162 | ||
| @@ -162,8 +174,13 @@ if (!dimensions["mines"] || parseInt(dimensions["mines"], 10) > parseInt(dimensi | |||
| 162 | 174 | ||
| 163 | class Coordinates { | 175 | class Coordinates { |
| 164 | constructor (x, y) { | 176 | constructor (x, y) { |
| 165 | this.x = x; | 177 | if (x instanceof Coordinates) { |
| 166 | this.y = y; | 178 | this.x = x.x; |
| 179 | this.y = x.y; | ||
| 180 | } else { | ||
| 181 | this.x = typeof x === "string" ? parseInt(x, 10) : x; | ||
| 182 | this.y = typeof y === "string" ? parseInt(y, 10) : y; | ||
| 183 | } | ||
| 167 | } | 184 | } |
| 168 | equals (other) { | 185 | equals (other) { |
| 169 | if (other instanceof Coordinates) { | 186 | if (other instanceof Coordinates) { |
| @@ -182,7 +199,6 @@ class Coordinates { | |||
| 182 | } | 199 | } |
| 183 | return false; | 200 | return false; |
| 184 | } | 201 | } |
| 185 | |||
| 186 | find (array) { | 202 | find (array) { |
| 187 | for (var i = 0; i < array.length; i++) { | 203 | for (var i = 0; i < array.length; i++) { |
| 188 | if (array[i].equals(this)) { | 204 | if (array[i].equals(this)) { |
| @@ -191,6 +207,10 @@ class Coordinates { | |||
| 191 | } | 207 | } |
| 192 | return -1; | 208 | return -1; |
| 193 | } | 209 | } |
| 210 | copy () { | ||
| 211 | a = new Coordinates(this.x, this.y); | ||
| 212 | return a; | ||
| 213 | } | ||
| 194 | } | 214 | } |
| 195 | 215 | ||
| 196 | // Initialize Board array | 216 | // Initialize Board array |
