diff options
| author | Aiden Woodruff <aiden.woodruff@gmail.com> | 2018-04-14 15:29:05 -0500 |
|---|---|---|
| committer | Aiden Woodruff <aiden.woodruff@gmail.com> | 2018-04-14 15:29:05 -0500 |
| commit | 8841a18cb69f9753307f78d89fbf8fc53be3bdc0 (patch) | |
| tree | 7d0ba005d42a04574207261e303e3ec7a643e5aa | |
| parent | cb57c5d7b5adbd8364b80b55a23648b31668a78a (diff) | |
| download | sweeper-8841a18cb69f9753307f78d89fbf8fc53be3bdc0.tar.gz sweeper-8841a18cb69f9753307f78d89fbf8fc53be3bdc0.tar.bz2 sweeper-8841a18cb69f9753307f78d89fbf8fc53be3bdc0.zip | |
Fixed unique mine placement
Signed-off-by: Aiden Woodruff <aiden.woodruff@gmail.com>
| -rw-r--r-- | ChangeLog | 2 | ||||
| -rw-r--r-- | sweeper.js | 27 |
2 files changed, 24 insertions, 5 deletions
| @@ -21,6 +21,7 @@ | |||
| 21 | Assigns click and contextmenu listeners at page load. | 21 | Assigns click and contextmenu listeners at page load. |
| 22 | Functions moved to show order of execution. | 22 | Functions moved to show order of execution. |
| 23 | Corrected typo. | 23 | Corrected typo. |
| 24 | Fixed mine placement. | ||
| 24 | (reveal): Removed debug log at beginning of onclick handler. | 25 | (reveal): Removed debug log at beginning of onclick handler. |
| 25 | Changes behavior if argument is Event or HTML object. | 26 | Changes behavior if argument is Event or HTML object. |
| 26 | Removes right click action. | 27 | Removes right click action. |
| @@ -33,6 +34,7 @@ | |||
| 33 | No longer manually removes EventListeners, as editing innerHTML fixes it. | 34 | No longer manually removes EventListeners, as editing innerHTML fixes it. |
| 34 | (Coordinates): Class added for testing equality. | 35 | (Coordinates): Class added for testing equality. |
| 35 | Global variable declarations added to beginning | 36 | Global variable declarations added to beginning |
| 37 | Added find and in_arr functions to find Coordinate object in an array. | ||
| 36 | 38 | ||
| 37 | 2018-04-13 Aiden Woodruff <aiden.woodruff@gmail.com> | 39 | 2018-04-13 Aiden Woodruff <aiden.woodruff@gmail.com> |
| 38 | 40 | ||
| @@ -165,7 +165,7 @@ class Coordinates { | |||
| 165 | this.x = x; | 165 | this.x = x; |
| 166 | this.y = y; | 166 | this.y = y; |
| 167 | } | 167 | } |
| 168 | equals(other) { | 168 | equals (other) { |
| 169 | if (other instanceof Coordinates) { | 169 | if (other instanceof Coordinates) { |
| 170 | if (this.x === other.x && this.y === other.y) { | 170 | if (this.x === other.x && this.y === other.y) { |
| 171 | return true; | 171 | return true; |
| @@ -174,6 +174,23 @@ class Coordinates { | |||
| 174 | return false; | 174 | return false; |
| 175 | } | 175 | } |
| 176 | } | 176 | } |
| 177 | in_arr (array) { | ||
| 178 | for (var i = 0; i < array.length; i++) { | ||
| 179 | if (array[i].equals(this)) { | ||
| 180 | return true; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | return false; | ||
| 184 | } | ||
| 185 | |||
| 186 | find (array) { | ||
| 187 | for (var i = 0; i < array.length; i++) { | ||
| 188 | if (array[i].equals(this)) { | ||
| 189 | return i; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | return -1; | ||
| 193 | } | ||
| 177 | } | 194 | } |
| 178 | 195 | ||
| 179 | // Initialize Board array | 196 | // Initialize Board array |
| @@ -188,17 +205,17 @@ for (var y = 0; y < dimensions["height"]; y++) { | |||
| 188 | } | 205 | } |
| 189 | 206 | ||
| 190 | // Filter method | 207 | // Filter method |
| 191 | function onlyUnique(value, index, self) { | 208 | function onlyUniqueCoord(value, index, self) { |
| 192 | return self.indexOf(value) === index; | 209 | return value.find(self) === index; |
| 193 | } | 210 | } |
| 194 | 211 | ||
| 195 | // Get random mine values | 212 | // Get random mine values |
| 196 | while (mines.length < dimensions["mines"]) { | 213 | while (mines.length < parseInt(dimensions["mines"],10)) { |
| 197 | mines.push(new Coordinates( | 214 | mines.push(new Coordinates( |
| 198 | Math.floor(Math.random() * dimensions["width"]), | 215 | Math.floor(Math.random() * dimensions["width"]), |
| 199 | Math.floor(Math.random() * dimensions["height"]) | 216 | Math.floor(Math.random() * dimensions["height"]) |
| 200 | )); | 217 | )); |
| 201 | mines.filter(onlyUnique); | 218 | mines = mines.filter(onlyUniqueCoord); |
| 202 | } | 219 | } |
| 203 | 220 | ||
| 204 | unflagged = mines.length; | 221 | unflagged = mines.length; |
