Skip to content

Commit

Permalink
refactor grid, gamemanager, tile
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielecirulli committed Mar 22, 2014
1 parent 4d294cf commit c48b926
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 53 deletions.
55 changes: 28 additions & 27 deletions js/game_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,24 @@ GameManager.prototype.isGameTerminated = function () {

// 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
Expand Down Expand Up @@ -82,7 +83,7 @@ GameManager.prototype.actuate = function () {
this.storageManager.setBestScore(this.score);
}

this.storageManager.setGameState(this.serializeGameState());
this.storageManager.setGameState(this.serialize());

this.actuator.actuate(this.grid, {
score: this.score,
Expand All @@ -94,16 +95,16 @@ GameManager.prototype.actuate = function () {

};

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 () {
Expand Down
39 changes: 22 additions & 17 deletions js/grid.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
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] = [];

for (var y = 0; y < this.size; y++) {
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 x = 0; x < this.size; x++) {
var row = cells[x] = [];

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 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
Expand Down Expand Up @@ -95,18 +99,19 @@ Grid.prototype.withinBounds = function (position) {
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
};
};
18 changes: 9 additions & 9 deletions js/tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ Tile.prototype.updatePosition = function (position) {
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
};
};

0 comments on commit c48b926

Please sign in to comment.