Commits

Anonymous committed 62ead31

No OO

  • Participants
  • Parent commits 2a92a9a

Comments (0)

Files changed (1)

 var COLORS = ['blue', 'green', 'orange', 'red', 'yellow'];
 var NUM_ROWS = 10;
 var NUM_COLS = 15;
-var BOARD = [];
-/*
-var NUM_ROWS = 5;
-var NUM_COLS = 5;
-*/
 var SCORE = 0;
 var BLANK = 'blank.png';
+var BOARD = [];
 
 function random_color() {
-    return COLORS[parseInt(Math.random() * COLORS.length)];
+    return COLORS[parseInt(Math.random() * COLORS.length)] + '.png';
 }
 
-function make_coords(row, col) {
-    return {
-        row: row,
-        col: col
-    };
+function cell_color(row, col, color) {
+    var img = BOARD[row][col];
+    if (color == undefined) {
+        return img.attr('src');
+    }
+    else {
+        img.attr('src', color);
+    }
 }
 
-function is_coords_equal(c1, c2) {
-    return (c1.row == c2.row) && (c1.col == c2.col);
+function is_in_board(cell) {
+    var row = cell[0];
+    var col = cell[1];
+    return (row >= 0) && (row < NUM_ROWS) &&
+           (col >= 0) && (col < NUM_COLS);
 }
 
-function img_at(coords) {
-    /* jQuery indexes are 1 based */
-    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) {
+    return $.grep([[row - 1, col], [row + 1, col], 
+                   [row, col - 1], [row, col + 1]],
+                   is_in_board);
 }
 
-function cell_neighbours(coords) {
-    var neighbours = [];
-    if (coords.row > 0) {
-        neighbours.push(make_coords(coords.row - 1, coords.col));
-    }
-    if (coords.row < NUM_ROWS - 1) {
-        neighbours.push(make_coords(coords.row + 1, coords.col));
-    }
-    if (coords.col > 0) {
-        neighbours.push(make_coords(coords.row, coords.col - 1));
-    }
-    if (coords.col < NUM_COLS - 1) {
-        neighbours.push(make_coords(coords.row, coords.col + 1));
+function is_cell_equal(c1, c2) {
+    return (c1[0] == c2[0]) && (c1[1] == c2[1]);
+}
+
+function is_in(cell, list_of_cell) {
+    var pred = function(c) {
+        return is_cell_equal(c, cell);
     }
 
-    return neighbours;
+    return $.grep(list_of_cell, pred).length > 0;
 }
 
-function is_in(coords, list_of_coords) {
-    var pred = function(c) {
-        return is_coords_equal(c, coords);
-    }
+function same_color_pack(row, col) {
+    var pack = [];
+    var waiting = [[row, col]];
+    var color = cell_color(row, col);
 
-    return $.grep(list_of_coords, pred).length > 0;
-}
-
-function same_color_pack(coords) {
-    var pack = [];
-    var waiting = [coords];
-    var color = BOARD[coords.row][coords.col];
-
-    var is_same_color = function(coords) {
-        return BOARD[coords.row][coords.col] == color;
+    var is_same_color = function(cell) {
+        return cell_color(cell[0], cell[1]) == color;
     };
 
     while (waiting.length) {
-        var coords = waiting.pop();
-        pack.push(coords);
-        var neighbours = cell_neighbours(coords);
+        var cell = waiting.pop();
+        pack.push(cell);
+        var neighbours = cell_neighbours(cell[0], cell[1]);
         var same = $.grep(neighbours, is_same_color);
-        same = $.grep(same, function(c) { return !is_in(c, pack);});
+        same = $.grep(same, function(c) { 
+                return !(is_in(c, pack) || is_in(c, waiting));
+            }
+        );
         waiting = waiting.concat(same);
     }
 
     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;
+function push_down(row, col) {
+    while (row > NUM_ROWS) {
+        if (cell_color(row +1, col) == BLANK) {
+            cell_color(row +1, col, cell_color(row, col));
+            cell_color(row, col, BLANK);
+            row = 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));
+    for (var col = 0; col < NUM_COLS; ++col) {
+        for (var row = NUM_ROWS - 2; row >= 0; --row) {
+            if (cell_color(row + 1, col) == BLANK) {
+                push_down(row, col);
+            }
         }
     }
 }
 
-function on_click(coords) {
-    var pack = same_color_pack(coords);
+function on_click(row, col) {
+    var pack = same_color_pack(row, col);
     if (pack.length < 2) {
         return;
     }
     update_score(pack.length);
+    
     $.each(pack, function () {
-            img_at(this).attr('src', BLANK);
+            cell_color(this[0], this[1], BLANK);
         }
     );
     collapse();
 }
 
 function make_handler(row, col) {
-    var coords = make_coords(row, col);
     return function() {
-        on_click(coords);
+        on_click(row, col);
     }
 }
 
 }
 
 function initialize_board() {
+    var table = $('#board');
     for (var row = 0; row < NUM_ROWS; ++row) {
-        var colors = [];
+        var images = [];
+        var tr = $('<tr />');
         for (var col = 0; col < NUM_COLS; ++col) {
-            colors.push(random_color());
+            var td = $('<td></td>');
+            var img = $('<img />').attr('src', BLANK);
+            img.click(make_handler(row, col));
+            td.append(img);
+            tr.append(td);
+            images.push(img);
         }
-        BOARD.push(colors);
+        table.append(tr);
+        BOARD.push(images);
     }
 }
 
-
-function initialize_ui() {
-    var table = $('#board');
+function restart() {
+    update_score(-SCORE);
     for (var row = 0; row < NUM_ROWS; ++row) {
-        var tr = $('<tr />');
         for (var col = 0; col < NUM_COLS; ++col) {
-            var td = $('<td></td>');
-            var img = $('<img />');
-            img.attr('src', BOARD[row][col] + '.png');
-            img.click(make_handler(row, col));
-            td.append(img);
-            tr.append(td);
+            cell_color(row, col, random_color());
         }
-        table.append(tr);
     }
 }
 
+
 function on_ready()
 {
     initialize_board();
-    initialize_ui();
-    $('#restart').click(
-        function() {
-            update_score(-SCORE);
-            $.each($('#board img'), function() {
-                $(this).attr('src', random_color() + '.png');
-            });
-        }
-    );
+    restart();
+    $('#restart').click(restart);
 }
 
 $(document).ready(on_ready);