Skip to content

Commit c48b926

Browse files
refactor grid, gamemanager, tile
1 parent 4d294cf commit c48b926

File tree

3 files changed

+59
-53
lines changed

3 files changed

+59
-53
lines changed

js/game_manager.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,24 @@ GameManager.prototype.isGameTerminated = function () {
3636

3737
// Set up the game
3838
GameManager.prototype.setup = function () {
39-
var previousGameState = this.storageManager.getGameState();
40-
41-
if (previousGameState) {
42-
this.grid = new Grid(previousGameState.grid.size, previousGameState.grid.cells);
43-
this.score = previousGameState.score;
44-
this.over = previousGameState.over;
45-
this.won = previousGameState.won;
46-
this.keepPlaying = previousGameState.keepPlaying;
39+
var previousState = this.storageManager.getGameState();
40+
41+
if (previousState) {
42+
this.grid = new Grid(previousState.grid.size,
43+
previousState.grid.cells); // Reload grid
44+
this.score = previousState.score;
45+
this.over = previousState.over;
46+
this.won = previousState.won;
47+
this.keepPlaying = previousState.keepPlaying;
4748
} else {
48-
this.grid = new Grid(this.size);
49-
this.score = 0;
50-
this.over = false;
51-
this.won = false;
52-
this.keepPlaying = false;
53-
54-
// Add the initial tiles
55-
this.addStartTiles();
49+
this.grid = new Grid(this.size);
50+
this.score = 0;
51+
this.over = false;
52+
this.won = false;
53+
this.keepPlaying = false;
54+
55+
// Add the initial tiles
56+
this.addStartTiles();
5657
}
5758

5859
// Update the actuator
@@ -82,7 +83,7 @@ GameManager.prototype.actuate = function () {
8283
this.storageManager.setBestScore(this.score);
8384
}
8485

85-
this.storageManager.setGameState(this.serializeGameState());
86+
this.storageManager.setGameState(this.serialize());
8687

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

9596
};
9697

97-
GameManager.prototype.serializeGameState = function () {
98-
return {
99-
size: this.size,
100-
grid: this.grid.gridState(),
101-
score: this.score,
102-
over: this.over,
103-
won: this.won,
104-
keepPlaying: this.keepPlaying
105-
};
106-
}
98+
GameManager.prototype.serialize = function () {
99+
return {
100+
size: this.size,
101+
grid: this.grid.serialize(),
102+
score: this.score,
103+
over: this.over,
104+
won: this.won,
105+
keepPlaying: this.keepPlaying
106+
};
107+
};
107108

108109
// Save all tile positions and remove merger info
109110
GameManager.prototype.prepareTiles = function () {

js/grid.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
1-
function Grid(size, previousCellState) {
1+
function Grid(size, previousState) {
22
this.size = size;
3-
this.cells = previousCellState ? this.buildFromPreviousState(previousCellState) : this.buildNew();
3+
this.cells = previousState ? this.fromState(previousState) : this.empty();
44
}
55

66
// Build a grid of the specified size
7-
Grid.prototype.buildNew = function () {
7+
Grid.prototype.empty = function () {
88
var cells = [];
9+
910
for (var x = 0; x < this.size; x++) {
1011
var row = cells[x] = [];
1112

1213
for (var y = 0; y < this.size; y++) {
1314
row.push(null);
1415
}
1516
}
17+
1618
return cells;
1719
};
1820

19-
Grid.prototype.buildFromPreviousState = function (state) {
20-
var cells = [];
21-
for (var x = 0; x < this.size; x++) {
22-
var row = cells[x] = [];
21+
Grid.prototype.fromState = function (state) {
22+
var cells = [];
23+
24+
for (var x = 0; x < this.size; x++) {
25+
var row = cells[x] = [];
2326

24-
for (var y = 0; y < this.size; y++) {
25-
var tileState = state[x][y];
26-
row.push(tileState ? new Tile(tileState.position, tileState.value) : null);
27-
}
27+
for (var y = 0; y < this.size; y++) {
28+
var tile = state[x][y];
29+
row.push(tile ? new Tile(tile.position, tile.value) : null);
2830
}
29-
return cells;
31+
}
32+
33+
return cells;
3034
};
3135

3236
// Find the first available random position
@@ -95,18 +99,19 @@ Grid.prototype.withinBounds = function (position) {
9599
position.y >= 0 && position.y < this.size;
96100
};
97101

98-
Grid.prototype.gridState = function () {
102+
Grid.prototype.serialize = function () {
99103
var cellState = [];
104+
100105
for (var x = 0; x < this.size; x++) {
101106
var row = cellState[x] = [];
102107

103108
for (var y = 0; y < this.size; y++) {
104-
row.push(this.cells[x][y] ? this.cells[x][y].tileState() : null);
109+
row.push(this.cells[x][y] ? this.cells[x][y].serialize() : null);
105110
}
106111
}
107112

108113
return {
109-
size: this.size,
110-
cells: cellState
111-
}
114+
size: this.size,
115+
cells: cellState
116+
};
112117
};

js/tile.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ Tile.prototype.updatePosition = function (position) {
1616
this.y = position.y;
1717
};
1818

19-
Tile.prototype.tileState = function () {
20-
return {
21-
position: {
22-
x: this.x,
23-
y: this.y
24-
},
25-
value: this.value
26-
};
27-
}
19+
Tile.prototype.serialize = function () {
20+
return {
21+
position: {
22+
x: this.x,
23+
y: this.y
24+
},
25+
value: this.value
26+
};
27+
};

0 commit comments

Comments
 (0)