Commits

Anonymous committed 8b0b387

Use LEVELS and save current level

Comments (0)

Files changed (1)

 var COLORS = ['blue', 'green', 'orange', 'red', 'yellow'];
-var NUM_ROWS = 10;
-var NUM_COLS = 15;
-var NUM_COLORS = COLORS.length;
+var LEVELS = {
+    easy: {
+        rows: 6,
+        cols: 6,
+        colors: 3,
+    },
+    medium: {
+        rows: 9,
+        cols: 9,
+        colors: 4
+    },
+    difficult: {
+        rows: 10,
+        cols: 15,
+        colors: COLORS.length
+    }
+};
+
+var LEVEL = null;
 var SCORE = 0;
 var BLANK = 'blank.png';
 /* We cache the images in the board since calling 'cell_color' with jQuery
 */
 var BOARD = [];
 
-var LEVELS = {
-    easy: {
-        num_rows: 6,
-        num_cols: 6,
-        num_colors: 3,
-    },
-    medium: {
-        num_rows: 9,
-        num_cols: 9,
-        num_colors: 4
-    },
-    difficult: {
-        num_rows: NUM_ROWS,
-        num_cols: NUM_COLS,
-        num_colors: NUM_COLORS
-    }
-};
-
 function random_color() {
-    return COLORS[parseInt(Math.random() * NUM_COLORS)] + '.png';
+    return COLORS[parseInt(Math.random() * LEVEL.colors)] + '.png';
 }
 
 function cell_color(row, col, color) {
 function is_in_board(cell) {
     var row = cell[0];
     var col = cell[1];
-    return (row >= 0) && (row < NUM_ROWS) &&
-           (col >= 0) && (col < NUM_COLS);
+    return (row >= 0) && (row < LEVEL.rows) &&
+           (col >= 0) && (col < LEVEL.cols);
 }
 
 function cell_neighbours(row, col) {
 }
 
 function push_down(row, col) {
-    while ((row < NUM_ROWS - 1) && cell_color(row + 1, col) == BLANK) {
+    while ((row < LEVEL.rows - 1) && cell_color(row + 1, col) == BLANK) {
         cell_color(row + 1, col, cell_color(row, col));
         cell_color(row, col, BLANK);
         ++row;
 }
 
 function collapse() {
-    for (var col = 0; col < NUM_COLS; ++col) {
-        for (var row = NUM_ROWS - 1; row >= 0; --row) {
+    for (var col = 0; col < LEVEL.cols; ++col) {
+        for (var row = LEVEL.rows - 1; row >= 0; --row) {
             push_down(row, col);
         }
     }
 }
 
 function is_empty_col(col) {
-    for (var row = 0; row < NUM_ROWS; ++row) {
+    for (var row = 0; row < LEVEL.rows; ++row) {
         if (cell_color(row, col) != BLANK) {
             return 0;
         }
 
 function one_glue() {
     var moved = 0;
-    for (var col = 1; col < NUM_COLS -1; ++col) {
+    for (var col = 1; col < LEVEL.cols -1; ++col) {
         if (is_empty_col(col)) {
-            for (var row = 0; row < NUM_ROWS; ++row) {
+            for (var row = 0; row < LEVEL.rows; ++row) {
                 var color = cell_color(row, col + 1);
                 moved = (color != BLANK);
                 cell_color(row, col, color);
 }
 
 function is_end_of_game() {
-    for (var row = 0; row < NUM_ROWS; ++row) {
-        for (var col = 0; col < NUM_COLS; ++col) {
+    for (var row = 0; row < LEVEL.rows; ++row) {
+        for (var col = 0; col < LEVEL.cols; ++col) {
             if ((cell_color(row, col) != BLANK) &&
                 (same_color_pack(row, col).length > 1)) {
                 return 0;
 }
 
 function clear_pack_hint() {
-    for (var row = 0; row < NUM_ROWS; ++row) {
-        for (var col = 0; col < NUM_COLS; ++col) {
+    for (var row = 0; row < LEVEL.rows; ++row) {
+        for (var col = 0; col < LEVEL.cols; ++col) {
             BOARD[row][col].removeClass('pack');
         }
     }
 function initialize_board() {
     BOARD = [];
     var table = $('#board');
-    for (var row = 0; row < NUM_ROWS; ++row) {
+    for (var row = 0; row < LEVEL.rows; ++row) {
         var images = [];
         var tr = $('<tr />');
-        for (var col = 0; col < NUM_COLS; ++col) {
+        for (var col = 0; col < LEVEL.cols; ++col) {
             var td = $('<td></td>');
             var img = $('<img />').attr('src', BLANK);
             img.click(make_click_handler(row, col));
 function restart() {
     $('#message').hide();
     set_score(0);
-    for (var row = 0; row < NUM_ROWS; ++row) {
-        for (var col = 0; col < NUM_COLS; ++col) {
+    for (var row = 0; row < LEVEL.rows; ++row) {
+        for (var col = 0; col < LEVEL.cols; ++col) {
             cell_color(row, col, random_color());
         }
     }
 }
 
 function change_level(level) {
-    NUM_ROWS = LEVELS[level]['num_rows'];
-    NUM_COLS = LEVELS[level]['num_cols'];
-    NUM_COLORS = LEVELS[level]['num_colors'];
+    LEVEL = LEVELS[level];
 
     $('#board').empty();
     initialize_board();
 function get_last_level() {
     var level = $.cookie(LEVEL_COOKIE);
     if (level == null) {
-        level = 'difficult';
+        level = 'medium';
     }
 
     return level;
 }
 
 function save_settings() {
+    $.cookie(LEVEL_COOKIE, $('#level').val());
 }
 
 
             $(this).fadeOut('slow');
         }
     );
-    var default_level = 'difficult';
-    $('#level').val(default_level);
     $('#level').change(
         function() {
             change_level(this.value);
         }
     );
-    change_level(default_level);
     $('#restart').click(restart);
+    $(window).unload(save_settings);
+
+    var level = get_last_level();
+    $('#level').val(level);
+    change_level(level);
 }
 
 $(document).ready(on_ready);