diff options
| author | Aiden Woodruff <aiden.woodruff@gmail.com> | 2018-04-26 10:48:38 -0500 |
|---|---|---|
| committer | Aiden Woodruff <aiden.woodruff@gmail.com> | 2018-04-26 10:48:38 -0500 |
| commit | 9b1faf1e60e1e36b8c54105ff491449e4237200e (patch) | |
| tree | 8878f4420dd130e237bfff31960fc5eb18ec3781 | |
| parent | 0b7110220f9f2a2f886898b6cb95f28d5edacb02 (diff) | |
| download | sweeper-9b1faf1e60e1e36b8c54105ff491449e4237200e.tar.gz sweeper-9b1faf1e60e1e36b8c54105ff491449e4237200e.tar.bz2 sweeper-9b1faf1e60e1e36b8c54105ff491449e4237200e.zip | |
First clicked cannot be a mine
Added function to populate board
get_cell can be a Coordinates object
Initialize boardmap in populate_board
Signed-off-by: Aiden Woodruff <aiden.woodruff@gmail.com>
| -rw-r--r-- | sweeper.js | 269 |
1 files changed, 156 insertions, 113 deletions
| @@ -11,8 +11,53 @@ var timer = undefined; | |||
| 11 | var clicks = 0; | 11 | var clicks = 0; |
| 12 | var rclicks = 0; | 12 | var rclicks = 0; |
| 13 | 13 | ||
| 14 | class Coordinates { | ||
| 15 | constructor (x, y) { | ||
| 16 | if (x instanceof Coordinates) { | ||
| 17 | this.x = x.x; | ||
| 18 | this.y = x.y; | ||
| 19 | } else { | ||
| 20 | this.x = typeof x === "string" ? parseInt(x, 10) : x; | ||
| 21 | this.y = typeof y === "string" ? parseInt(y, 10) : y; | ||
| 22 | } | ||
| 23 | } | ||
| 24 | equals (other) { | ||
| 25 | if (other instanceof Coordinates) { | ||
| 26 | if (this.x === other.x && this.y === other.y) { | ||
| 27 | return true; | ||
| 28 | } | ||
| 29 | } else { | ||
| 30 | return false; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | in_arr (array) { | ||
| 34 | for (var i = 0; i < array.length; i++) { | ||
| 35 | if (array[i].equals(this)) { | ||
| 36 | return true; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | return false; | ||
| 40 | } | ||
| 41 | find (array) { | ||
| 42 | for (var i = 0; i < array.length; i++) { | ||
| 43 | if (array[i].equals(this)) { | ||
| 44 | return i; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | return -1; | ||
| 48 | } | ||
| 49 | copy () { | ||
| 50 | a = new Coordinates(this.x, this.y); | ||
| 51 | return a; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 14 | // Return cell object from x and y coordinates | 55 | // Return cell object from x and y coordinates |
| 15 | function get_cell (x, y) { | 56 | function get_cell (x, y) { |
| 57 | if (x instanceof Coordinates) { | ||
| 58 | y = x.y; | ||
| 59 | x = x.x; | ||
| 60 | } | ||
| 16 | return document.getElementById( (x.toString() + "," + y.toString() ) ); | 61 | return document.getElementById( (x.toString() + "," + y.toString() ) ); |
| 17 | } | 62 | } |
| 18 | 63 | ||
| @@ -47,15 +92,22 @@ function lose () { | |||
| 47 | 92 | ||
| 48 | // Reveal a cell | 93 | // Reveal a cell |
| 49 | function reveal (e) { | 94 | function reveal (e) { |
| 95 | var coord; | ||
| 96 | var object; | ||
| 50 | if (e instanceof HTMLTableCellElement) { | 97 | if (e instanceof HTMLTableCellElement) { |
| 51 | var object = e; | 98 | object = e; |
| 99 | coord = get_cell_xy(object); | ||
| 52 | } else if (e instanceof MouseEvent) { | 100 | } else if (e instanceof MouseEvent) { |
| 53 | var object = e.target; | 101 | object = e.target; |
| 102 | coord = get_cell_xy(object); | ||
| 54 | clicks++; | 103 | clicks++; |
| 104 | } else if (e instanceof Coordinates) { | ||
| 105 | object = get_cell(e); | ||
| 106 | coord = e; | ||
| 55 | } else { | 107 | } else { |
| 56 | return false; | 108 | return false; |
| 57 | } | 109 | } |
| 58 | var coord = get_cell_xy(object); | 110 | coord = get_cell_xy(object); |
| 59 | if (boardmap[coord.y][coord.x].state === "R") { | 111 | if (boardmap[coord.y][coord.x].state === "R") { |
| 60 | return; | 112 | return; |
| 61 | } else if (boardmap[coord.y][coord.x].state === "U" || boardmap[coord.y][coord.x].state === "F") { | 113 | } else if (boardmap[coord.y][coord.x].state === "U" || boardmap[coord.y][coord.x].state === "F") { |
| @@ -216,125 +268,110 @@ if (!dimensions["mines"] || parseInt(dimensions["mines"], 10) > (width * height) | |||
| 216 | total_mines = parseInt(dimensions["mines"], 10); | 268 | total_mines = parseInt(dimensions["mines"], 10); |
| 217 | } | 269 | } |
| 218 | 270 | ||
| 219 | class Coordinates { | 271 | // Filter method |
| 220 | constructor (x, y) { | 272 | function onlyUniqueCoord(value, index, self) { |
| 221 | if (x instanceof Coordinates) { | 273 | return value.find(self) === index; |
| 222 | this.x = x.x; | 274 | } |
| 223 | this.y = x.y; | 275 | function populate_board (e) { |
| 224 | } else { | 276 | var disclude; |
| 225 | this.x = typeof x === "string" ? parseInt(x, 10) : x; | 277 | if (e instanceof Coordinates) { |
| 226 | this.y = typeof y === "string" ? parseInt(y, 10) : y; | 278 | // How it should be |
| 227 | } | 279 | disclude = e; |
| 228 | } | 280 | } else if (e instanceof HTMLTableCellElement) { |
| 229 | equals (other) { | 281 | disclude = get_cell_xy(disclude); |
| 230 | if (other instanceof Coordinates) { | 282 | } else if (e instanceof MouseEvent) { |
| 231 | if (this.x === other.x && this.y === other.y) { | 283 | disclude = get_cell_xy(e.target); |
| 232 | return true; | 284 | } else { |
| 233 | } | ||
| 234 | } else { | ||
| 235 | return false; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | in_arr (array) { | ||
| 239 | for (var i = 0; i < array.length; i++) { | ||
| 240 | if (array[i].equals(this)) { | ||
| 241 | return true; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | return false; | 285 | return false; |
| 245 | } | 286 | } |
| 246 | find (array) { | 287 | |
| 247 | for (var i = 0; i < array.length; i++) { | 288 | // Initialize Board array |
| 248 | if (array[i].equals(this)) { | 289 | boardmap = Array(); |
| 249 | return i; | 290 | for (var y = 0; y < height; y++) { |
| 291 | boardmap[y] = Array(); | ||
| 292 | for (x = 0; x < width; x++) { | ||
| 293 | boardmap[y][x] = { | ||
| 294 | value: "0", | ||
| 295 | state: "U" | ||
| 250 | } | 296 | } |
| 251 | } | 297 | } |
| 252 | return -1; | ||
| 253 | } | ||
| 254 | copy () { | ||
| 255 | a = new Coordinates(this.x, this.y); | ||
| 256 | return a; | ||
| 257 | } | 298 | } |
| 258 | } | ||
| 259 | 299 | ||
| 260 | // Initialize Board array | 300 | // Get random mine values |
| 261 | for (var y = 0; y < height; y++) { | 301 | while (mines.length < total_mines) { |
| 262 | boardmap[y] = Array(); | 302 | mines.push(new Coordinates( |
| 263 | for (x = 0; x < width; x++) { | 303 | Math.floor(Math.random() * width), |
| 264 | boardmap[y][x] = { | 304 | Math.floor(Math.random() * height) |
| 265 | value: "0", | 305 | )); |
| 266 | state: "U" | 306 | mines = mines.filter(onlyUniqueCoord); |
| 267 | } | 307 | mines = mines.filter(value => { return value !== disclude; }); |
| 268 | } | 308 | } |
| 269 | } | ||
| 270 | 309 | ||
| 271 | // Filter method | 310 | unflagged = mines.length; |
| 272 | function onlyUniqueCoord(value, index, self) { | ||
| 273 | return value.find(self) === index; | ||
| 274 | } | ||
| 275 | |||
| 276 | // Get random mine values | ||
| 277 | while (mines.length < total_mines) { | ||
| 278 | mines.push(new Coordinates( | ||
| 279 | Math.floor(Math.random() * width), | ||
| 280 | Math.floor(Math.random() * height) | ||
| 281 | )); | ||
| 282 | mines = mines.filter(onlyUniqueCoord); | ||
| 283 | } | ||
| 284 | 311 | ||
| 285 | unflagged = mines.length; | 312 | // Place mines |
| 286 | 313 | for (var i = 0; i < mines.length; i++) { | |
| 287 | // Place mines | 314 | boardmap[mines[i].y][mines[i].x].value = "M"; |
| 288 | for (var i = 0; i < mines.length; i++) { | 315 | } |
| 289 | boardmap[mines[i].y][mines[i].x].value = "M"; | 316 | console.log("Placed mines"); |
| 290 | } | ||
| 291 | 317 | ||
| 292 | // Assign numbers | 318 | // Assign numbers |
| 293 | for (var y = 0, count = 0, width = width, height = height; y < height; y++) { | 319 | for (var y = 0, count = 0; y < height; y++) { |
| 294 | for (var x = 0; x < width; x++, count = 0) { | 320 | for (var x = 0; x < width; x++, count = 0) { |
| 295 | if (boardmap[y][x].value === "M") { | 321 | if (boardmap[y][x].value === "M") { |
| 296 | continue; | 322 | continue; |
| 297 | } | 323 | } |
| 298 | if (x > 0 && boardmap[y][x - 1].value === "M") { | 324 | if (x > 0 && boardmap[y][x - 1].value === "M") { |
| 299 | count++; | 325 | count++; |
| 300 | } | 326 | } |
| 301 | if (y > 0 && boardmap[y - 1][x].value === "M") { | 327 | if (y > 0 && boardmap[y - 1][x].value === "M") { |
| 302 | count++; | 328 | count++; |
| 303 | } | 329 | } |
| 304 | if (x < width - 1 && boardmap[y][x + 1].value === "M") { | 330 | if (x < width - 1 && boardmap[y][x + 1].value === "M") { |
| 305 | count++; | 331 | count++; |
| 306 | } | 332 | } |
| 307 | if (y < height - 1 && boardmap[y+1][x].value === "M") { | 333 | if (y < height - 1 && boardmap[y+1][x].value === "M") { |
| 308 | count++; | 334 | count++; |
| 309 | } | 335 | } |
| 310 | if (x > 0 && y > 0 && boardmap[y-1][x - 1].value === "M") { | 336 | if (x > 0 && y > 0 && boardmap[y-1][x - 1].value === "M") { |
| 311 | count++; | 337 | count++; |
| 312 | } | 338 | } |
| 313 | if (x > 0 && y < height - 1 && boardmap[y+1][x - 1].value === "M") { | 339 | if (x > 0 && y < height - 1 && boardmap[y+1][x - 1].value === "M") { |
| 314 | count++; | 340 | count++; |
| 315 | } | 341 | } |
| 316 | if (x < width - 1 && y > 0 && boardmap[y-1][x + 1].value === "M") { | 342 | if (x < width - 1 && y > 0 && boardmap[y-1][x + 1].value === "M") { |
| 317 | count++; | 343 | count++; |
| 318 | } | 344 | } |
| 319 | if (x < width - 1 && y < height - 1 && boardmap[y + 1][x + 1].value === "M") { | 345 | if (x < width - 1 && y < height - 1 && boardmap[y + 1][x + 1].value === "M") { |
| 320 | count++; | 346 | count++; |
| 347 | } | ||
| 348 | boardmap[y][x].value = count.toString(); | ||
| 321 | } | 349 | } |
| 322 | boardmap[y][x].value = count.toString(); | ||
| 323 | } | 350 | } |
| 324 | } | ||
| 325 | 351 | ||
| 326 | for (var y = 0; y < height; y++) { | 352 | table = ""; |
| 327 | table += "<tr>"; | 353 | for (var y = 0; y < height; y++) { |
| 328 | for (x = 0; x < width; x++) { | 354 | table += "<tr>"; |
| 329 | if (boardmap[y][x].value !== "M") { | 355 | for (x = 0; x < width; x++) { |
| 330 | table += "<td class='unrevealed' n" + boardmap[y][x].value + "' id='" + x + "," + y + "'></td>"; | 356 | if (boardmap[y][x].value !== "M") { |
| 331 | } else if (boardmap[y][x].value === "M") { | 357 | table += "<td class='unrevealed' n" + boardmap[y][x].value + "' id='" + x + "," + y + "'></td>"; |
| 332 | table += "<td class='unrevealed mine' id='" + x + "," + y + "'></td>"; | 358 | } else if (boardmap[y][x].value === "M") { |
| 333 | } else { | 359 | table += "<td class='unrevealed mine' id='" + x + "," + y + "'></td>"; |
| 334 | table += "<td class='unrevealed null' id='" + x + "," + y + "'></td>"; | 360 | } else { |
| 361 | table += "<td class='unrevealed null' id='" + x + "," + y + "'></td>"; | ||
| 362 | } | ||
| 335 | } | 363 | } |
| 364 | table += "</tr>"; | ||
| 365 | } | ||
| 366 | |||
| 367 | // Write table to HTML and add event listeners | ||
| 368 | document.getElementById("board").innerHTML = table; | ||
| 369 | var tds = document.getElementsByTagName("td"); | ||
| 370 | for (var i = 0; i < tds.length; i++) { | ||
| 371 | tds[i].oncontextmenu = flag; | ||
| 372 | tds[i].onclick = reveal; | ||
| 336 | } | 373 | } |
| 337 | table += "</tr>"; | 374 | reveal(disclude); |
| 338 | } | 375 | } |
| 339 | 376 | ||
| 340 | window.onload = function () { | 377 | window.onload = function () { |
| @@ -343,17 +380,23 @@ window.onload = function () { | |||
| 343 | document.body.removeChild(document.getElementById("board")); | 380 | document.body.removeChild(document.getElementById("board")); |
| 344 | return; | 381 | return; |
| 345 | } | 382 | } |
| 383 | for (var y = 0; y < height; y++) { | ||
| 384 | table += "<tr>"; | ||
| 385 | for (x = 0; x < width; x++) { | ||
| 386 | table += "<td class='unrevealed' id='" + x + ',' + y + "'></td>"; | ||
| 387 | } | ||
| 388 | table += "</tr>"; | ||
| 389 | } | ||
| 346 | document.getElementById("board").innerHTML = table; | 390 | document.getElementById("board").innerHTML = table; |
| 391 | var tds = document.getElementsByTagName("td"); | ||
| 392 | for (var i = 0; i < tds.length; i++) { | ||
| 393 | tds[i].onclick = populate_board; | ||
| 394 | } | ||
| 347 | timer = window.setInterval( function () { | 395 | timer = window.setInterval( function () { |
| 348 | time++; | 396 | time++; |
| 349 | document.getElementById("timer").innerHTML = "Time - " + Math.floor(time/60).toString() + ":" + (time % 60 < 10 ? "0" : "") + (time % 60).toString(); | 397 | document.getElementById("timer").innerHTML = "Time - " + Math.floor(time/60).toString() + ":" + (time % 60 < 10 ? "0" : "") + (time % 60).toString(); |
| 350 | }, 1000); | 398 | }, 1000); |
| 351 | var tds = document.getElementsByTagName("td"); | 399 | document.getElementById("minecount").innerHTML = "Mines: " + total_mines.toString() |
| 352 | for (var i = 0; i < tds.length; i++) { | ||
| 353 | tds[i].oncontextmenu = flag; | ||
| 354 | tds[i].onclick = reveal; | ||
| 355 | } | ||
| 356 | document.getElementById("minecount").innerHTML = "Mines: " + unflagged.toString() | ||
| 357 | document.getElementsByName("height")[0].value = dimensions["height"] ? dimensions["height"] : ""; | 400 | document.getElementsByName("height")[0].value = dimensions["height"] ? dimensions["height"] : ""; |
| 358 | document.getElementsByName("width")[0].value = dimensions["width"] ? dimensions["width"] : ""; | 401 | document.getElementsByName("width")[0].value = dimensions["width"] ? dimensions["width"] : ""; |
| 359 | document.getElementsByName("mines")[0].value = dimensions["mines"] ? dimensions["mines"] : ""; | 402 | document.getElementsByName("mines")[0].value = dimensions["mines"] ? dimensions["mines"] : ""; |
