refactor grid, gamemanager, tile
refactor grid, gamemanager, tile

--- a/js/game_manager.js
+++ b/js/game_manager.js
@@ -36,23 +36,24 @@
 
 // Set up the game
 GameManager.prototype.setup = function () {
-  var previousGameState = this.storageManager.getGameState();
-
-  if (previousGameState) {
-      this.grid        = new Grid(previousGameState.grid.size, previousGameState.grid.cells);
-      this.score       = previousGameState.score;
-      this.over        = previousGameState.over;
-      this.won         = previousGameState.won;
-      this.keepPlaying = previousGameState.keepPlaying;
+  var previousState = this.storageManager.getGameState();
+
+  if (previousState) {
+    this.grid        = new Grid(previousState.grid.size,
+                                previousState.grid.cells); // Reload grid
+    this.score       = previousState.score;
+    this.over        = previousState.over;
+    this.won         = previousState.won;
+    this.keepPlaying = previousState.keepPlaying;
   } else {
-      this.grid        = new Grid(this.size);
-      this.score       = 0;
-      this.over        = false;
-      this.won         = false;
-      this.keepPlaying = false;
-
-      // Add the initial tiles
-      this.addStartTiles();
+    this.grid        = new Grid(this.size);
+    this.score       = 0;
+    this.over        = false;
+    this.won         = false;
+    this.keepPlaying = false;
+
+    // Add the initial tiles
+    this.addStartTiles();
   }
 
   // Update the actuator
@@ -82,7 +83,7 @@
     this.storageManager.setBestScore(this.score);
   }
 
-  this.storageManager.setGameState(this.serializeGameState());
+  this.storageManager.setGameState(this.serialize());
 
   this.actuator.actuate(this.grid, {
     score:      this.score,
@@ -94,16 +95,16 @@
 
 };
 
-GameManager.prototype.serializeGameState = function () {
-    return {
-        size:        this.size,
-        grid:        this.grid.gridState(),
-        score:       this.score,
-        over:        this.over,
-        won:         this.won,
-        keepPlaying: this.keepPlaying
-    };
-}
+GameManager.prototype.serialize = function () {
+  return {
+    size:        this.size,
+    grid:        this.grid.serialize(),
+    score:       this.score,
+    over:        this.over,
+    won:         this.won,
+    keepPlaying: this.keepPlaying
+  };
+};
 
 // Save all tile positions and remove merger info
 GameManager.prototype.prepareTiles = function () {

file:a/js/grid.js -> file:b/js/grid.js
--- a/js/grid.js
+++ b/js/grid.js
@@ -1,11 +1,12 @@
-function Grid(size, previousCellState) {
+function Grid(size, previousState) {
   this.size = size;
-  this.cells = previousCellState ? this.buildFromPreviousState(previousCellState) : this.buildNew();
+  this.cells = previousState ? this.fromState(previousState) : this.empty();
 }
 
 // Build a grid of the specified size
-Grid.prototype.buildNew = function () {
+Grid.prototype.empty = function () {
   var cells = [];
+
   for (var x = 0; x < this.size; x++) {
     var row = cells[x] = [];
 
@@ -13,20 +14,23 @@
       row.push(null);
     }
   }
+
   return cells;
 };
 
-Grid.prototype.buildFromPreviousState = function (state) {
-    var cells = [];
-    for (var x = 0; x < this.size; x++) {
-        var row = cells[x] = [];
+Grid.prototype.fromState = function (state) {
+  var cells = [];
 
-        for (var y = 0; y < this.size; y++) {
-            var tileState = state[x][y];
-            row.push(tileState ? new Tile(tileState.position, tileState.value) : null);
-        }
+  for (var x = 0; x < this.size; x++) {
+    var row = cells[x] = [];
+
+    for (var y = 0; y < this.size; y++) {
+      var tile = state[x][y];
+      row.push(tile ? new Tile(tile.position, tile.value) : null);
     }
-    return cells;
+  }
+
+  return cells;
 };
 
 // Find the first available random position
@@ -95,19 +99,20 @@
          position.y >= 0 && position.y < this.size;
 };
 
-Grid.prototype.gridState = function () {
+Grid.prototype.serialize = function () {
   var cellState = [];
+
   for (var x = 0; x < this.size; x++) {
     var row = cellState[x] = [];
 
     for (var y = 0; y < this.size; y++) {
-        row.push(this.cells[x][y] ? this.cells[x][y].tileState() : null);
+      row.push(this.cells[x][y] ? this.cells[x][y].serialize() : null);
     }
   }
 
   return {
-      size: this.size,
-      cells: cellState
-  }
+    size: this.size,
+    cells: cellState
+  };
 };
 

file:a/js/tile.js -> file:b/js/tile.js
--- a/js/tile.js
+++ b/js/tile.js
@@ -16,12 +16,13 @@
   this.y = position.y;
 };
 
-Tile.prototype.tileState = function () {
-    return {
-        position: {
-            x: this.x,
-            y: this.y
-        },
-        value: this.value
-    };
-}
+Tile.prototype.serialize = function () {
+  return {
+    position: {
+      x: this.x,
+      y: this.y
+    },
+    value: this.value
+  };
+};
+

comments