aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAiden Woodruff <aiden.woodruff@gmail.com>2018-04-14 15:29:05 -0500
committerAiden Woodruff <aiden.woodruff@gmail.com>2018-04-14 15:29:05 -0500
commit8841a18cb69f9753307f78d89fbf8fc53be3bdc0 (patch)
tree7d0ba005d42a04574207261e303e3ec7a643e5aa
parentcb57c5d7b5adbd8364b80b55a23648b31668a78a (diff)
downloadsweeper-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--ChangeLog2
-rw-r--r--sweeper.js27
2 files changed, 24 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index ae3ea20..e8e18ec 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
372018-04-13 Aiden Woodruff <aiden.woodruff@gmail.com> 392018-04-13 Aiden Woodruff <aiden.woodruff@gmail.com>
38 40
diff --git a/sweeper.js b/sweeper.js
index 4d63e72..04a6623 100644
--- a/sweeper.js
+++ b/sweeper.js
@@ -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
191function onlyUnique(value, index, self) { 208function 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
196while (mines.length < dimensions["mines"]) { 213while (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
204unflagged = mines.length; 221unflagged = mines.length;