Commits

Anonymous committed 63c18f2

Glue columns and end-of-game detection

  • Participants
  • Parent commits c7e0b5e

Comments (0)

Files changed (1)

     return pack;
 }
 
-var DEBUG=0;
 function push_down(row, col) {
     while ((row < NUM_ROWS - 1) && cell_color(row + 1, col) == BLANK) {
         cell_color(row + 1, col, cell_color(row, col));
     }
 }
 
+function is_empty_col(col) {
+    for (var row = 0; row < NUM_ROWS; ++row) {
+        if (cell_color(row, col) != BLANK) {
+            return 0;
+        }
+    }
+
+    return 1;
+}
+
+function glue_cols() {
+    for (var col = 1; col < NUM_COLS -1; ++col) {
+        if (is_empty_col(col)) {
+            for (var row = 0; row < NUM_ROWS; ++row) {
+                cell_color(row, col, cell_color(row, col + 1));
+                cell_color(row, col + 1, BLANK);
+            }
+        }
+    }
+}
+
+function is_end_of_game() {
+    for (var row = 0; row < NUM_ROWS; ++row) {
+        for (var col = 0; col < NUM_COLS; ++col) {
+            if ((cell_color(row, col) != BLANK) &&
+                (same_color_pack(row, col).length > 1)) {
+                return 0;
+            }
+        }
+    }
+
+    return 1;
+}
+
 function on_click(row, col) {
     if (cell_color(row, col) == BLANK) {
         return;
         }
     );
     collapse();
+    glue_cols();
+
+    if (is_end_of_game()) {
+        alert("END OF GAME");
+    }
 }
 
 function make_handler(row, col) {
 }
 
 function update_score(count) {
-    SCORE = SCORE + count;
+    SCORE = SCORE + (count * count);
     $('#score').html(SCORE);
 }