Commits

Anonymous committed 359c277

Collapsing

  • Participants
  • Parent commits caf4b5c

Comments (0)

Files changed (1)

 var COLORS = ['blue', 'green', 'orange', 'red', 'yellow'];
+/*
 var NUM_ROWS = 10;
 var NUM_COLS = 15;
+*/
+var NUM_ROWS = 5;
+var NUM_COLS = 5;
 var SCORE = 0;
+var BLANK = 'blank.png';
 
 function random_color() {
     return COLORS[parseInt(Math.random() * COLORS.length)];
     return pack;
 }
 
+function push_down(coords) {
+    var curr = img_at(coords);
+    if (curr.attr('src') == BLANK) {
+        return;
+    }
+
+    while (coords.row < (NUM_ROWS - 1)) {
+        var below = img_at(make_coords(coords.row + 1, coords.col));
+        if (below.attr('src') == BLANK) {
+            below.attr('src', curr.attr('src'));
+            curr.attr('src', BLANK);
+            curr = below;
+            coords.row = coords.row + 1;
+        }
+        else {
+            break;
+        }
+    }
+}
+
+function collapse() {
+    for (var row = NUM_ROWS - 2; row >= 0; --row) { /* Skip last row */
+        for (var col = NUM_COLS - 1; col >= 0; --col) {
+            push_down(make_coords(row, col));
+        }
+    }
+}
+
 function on_click(coords) {
     var pack = same_color_pack(coords);
+    if (pack.length < 2) {
+        return;
+    }
+    update_score(pack.length);
     $.each(pack, function () {
-            img_at(this).attr('src', 'blank.png');
+            img_at(this).attr('src', BLANK);
         }
     );
-
-    update_score(pack.length);
+    collapse();
 }
 
 function make_handler(row, col) {
     $('#score').html(SCORE);
 }
 
+
 function create_board() {
     var table = $('#board');
     for (var row = 0; row < NUM_ROWS; ++row) {