Anonymous avatar Anonymous committed d61e33e

* Using "coords" object - need a better name
* Find "pack" of same color (very slow)
* Hiding on click

Comments (0)

Files changed (1)

     return COLORS[parseInt(Math.random() * COLORS.length)];
 }
 
-function img_at(row, col) {
+function make_coords(row, col) {
+    return {
+        row: row,
+        col: col
+    };
+}
+
+function is_coords_equal(c1, c2) {
+    return (c1.row == c2.row) && (c1.col == c2.col);
+}
+
+function img_at(coords) {
     /* jQuery indexes are 1 based */
-    row = row + 1;
-    col = col + 1;
+    var row = coords.row + 1;
+    var col = coords.col + 1;
     return $('#board tr:nth-child(' + row + ') td:nth-child(' + col + ') img');
 }
 
-function cell_neighbours(row, col) {
+function cell_neighbours(coords) {
     var neighbours = [];
-    if (row > 0) {
-        neighbours.push([row - 1, col]);
+    if (coords.row > 0) {
+        neighbours.push(make_coords(coords.row - 1, coords.col));
     }
-    if (row < NUM_ROWS - 1) {
-        neighbours.push([row + 1, col]);
+    if (coords.row < NUM_ROWS - 1) {
+        neighbours.push(make_coords(coords.row + 1, coords.col));
     }
-    if (col > 0) {
-        neighbours.push([row, col - 1]);
+    if (coords.col > 0) {
+        neighbours.push(make_coords(coords.row, coords.col - 1));
     }
-    if (col < NUM_COLS - 1) {
-        neighbours.push([row, col + 1]);
+    if (coords.col < NUM_COLS - 1) {
+        neighbours.push(make_coords(coords.row, coords.col + 1));
     }
 
     return neighbours;
 }
 
-function same_color_pack(row, col) {
-    var same = cell_neighbours(row, col);
-    same.push([row, col]);
-    var src = img_at(row, col).attr('src');
-    return $.grep(same, 
-        function(coords) {
-            var img = img_at(coords[0], coords[1]);
-            return img.attr('src') == src;
-    });
+function is_in(coords, list_of_coords) {
+    var pred = function(c) {
+        return is_coords_equal(c, coords);
+    }
+
+    return $.grep(list_of_coords, pred).length > 0;
 }
 
-function on_click(row, col) {
-    var same_color = same_color_pack(row, col);
+function same_color_pack(coords) {
+    var pack = [];
+    var waiting = [coords];
+    var src = img_at(coords).attr('src');
+
+    var is_same_color = function(coords) {
+        return img_at(coords).attr('src') == src;
+    };
+
+
+    while (waiting.length) {
+        var coords = waiting.pop();
+        pack.push(coords);
+        var neighbours = cell_neighbours(coords);
+        var same = $.grep(neighbours, is_same_color);
+        same = $.grep(same, function(c) { return !is_in(c, pack);});
+        waiting = waiting.concat(same);
+    }
+
+    return pack;
+}
+
+function on_click(coords) {
+    var same_color = same_color_pack(coords);
     $.each(same_color, function () {
-            img_at(this[0], this[1]).fadeOut('slow').fadeIn('slow');
+            img_at(this).attr('src', 'blank.png');
         }
     );
 }
 
 function make_handler(row, col) {
+    var coords = make_coords(row, col);
     return function() {
-        on_click(row, col);
+        on_click(coords);
     }
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.