-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgridccj.js
141 lines (116 loc) · 3.45 KB
/
gridccj.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
function Grid(size, previousState, value_jpg_refs) {
this.isCcj = 1;
this.size = size;
this.cells = previousState ? this.fromState(previousState) : this.empty();
if (value_jpg_refs) {
this.value_jpg_refs = value_jpg_refs
}
else {
this.value_jpg_refs = {}
this.value_jpg_refs[-12] = 978;
this.value_jpg_refs[-11] = 978;
this.value_jpg_refs[-2] = 988;
this.value_jpg_refs[-1] = 988;
this.value_jpg_refs[2] = 2 + Math.floor(Math.random() * 1);
this.value_jpg_refs[4] = 4 + Math.floor(Math.random() * 1);
this.value_jpg_refs[8] = 8 + Math.floor(Math.random() * 2);
this.value_jpg_refs[16] = 16 + Math.floor(Math.random() * 1);
this.value_jpg_refs[32] = 32;
this.value_jpg_refs[64] = 64 + Math.floor(Math.random() * 1);
this.value_jpg_refs[128] = 128 + Math.floor(Math.random() * 2);
this.value_jpg_refs[256] = 256 + Math.floor(Math.random() * 1);
this.value_jpg_refs[512] = 512;
this.value_jpg_refs[1024] = 1024;
this.value_jpg_refs[2048] = 2048;
this.value_jpg_refs[4096] = 4096;
}
}
// Build a grid of the specified size
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.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 tile = state[x][y];
row.push(tile ? new Tile(tile.position, tile.value) : null);
}
}
return cells;
};
// Find the first available random position
Grid.prototype.randomAvailableCell = function () {
var cells = this.availableCells();
if (cells.length) {
return cells[Math.floor(Math.random() * cells.length)];
}
};
Grid.prototype.availableCells = function () {
var cells = [];
this.eachCell(function (x, y, tile) {
if (!tile) {
cells.push({ x: x, y: y });
}
});
return cells;
};
// Call callback for every cell
Grid.prototype.eachCell = function (callback) {
for (var x = 0; x < this.size; x++) {
for (var y = 0; y < this.size; y++) {
callback(x, y, this.cells[x][y]);
}
}
};
// Check if there are any cells available
Grid.prototype.cellsAvailable = function () {
return !!this.availableCells().length;
};
// Check if the specified cell is taken
Grid.prototype.cellAvailable = function (cell) {
return !this.cellOccupied(cell);
};
Grid.prototype.cellOccupied = function (cell) {
return !!this.cellContent(cell);
};
Grid.prototype.cellContent = function (cell) {
if (this.withinBounds(cell)) {
return this.cells[cell.x][cell.y];
} else {
return null;
}
};
// Inserts a tile at its position
Grid.prototype.insertTile = function (tile) {
this.cells[tile.x][tile.y] = tile;
};
Grid.prototype.removeTile = function (tile) {
this.cells[tile.x][tile.y] = null;
};
Grid.prototype.withinBounds = function (position) {
return position.x >= 0 && position.x < this.size &&
position.y >= 0 && position.y < this.size;
};
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].serialize() : null);
}
}
return {
size: this.size,
cells: cellState,
value_jpg_refs: this.value_jpg_refs
};
};