Commits

Anonymous committed 2a92a9a

Staring with local array for speedup

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';
 
 function same_color_pack(coords) {
     var pack = [];
     var waiting = [coords];
-    var src = img_at(coords).attr('src');
+    var color = BOARD[coords.row][coords.col];
 
     var is_same_color = function(coords) {
-        return img_at(coords).attr('src') == src;
+        return BOARD[coords.row][coords.col] == color;
     };
 
-
     while (waiting.length) {
         var coords = waiting.pop();
         pack.push(coords);
     $('#score').html(SCORE);
 }
 
+function initialize_board() {
+    for (var row = 0; row < NUM_ROWS; ++row) {
+        var colors = [];
+        for (var col = 0; col < NUM_COLS; ++col) {
+            colors.push(random_color());
+        }
+        BOARD.push(colors);
+    }
+}
 
-function create_board() {
+
+function initialize_ui() {
     var table = $('#board');
     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', random_color() + '.png');
+            img.attr('src', BOARD[row][col] + '.png');
             img.click(make_handler(row, col));
-
             td.append(img);
             tr.append(td);
         }
 
 function on_ready()
 {
-    create_board();
+    initialize_board();
+    initialize_ui();
     $('#restart').click(
         function() {
             update_score(-SCORE);