forked from gabrielecirulli/2048
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameTransitions.js
136 lines (111 loc) · 3.55 KB
/
GameTransitions.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
var Directions = require('./Directions');
var Grid = require('./Grid'); // Merely to get some constants.
var GameTransitions = {
move: function(currentState, direction) {
var newState = [0, 0];
// should iterate toward the origin
var backwards =
direction === Directions.UP || direction === Directions.RIGHT;
var isVerticalMove =
direction === Directions.UP || direction === Directions.DOWN;
var dj = backwards ? -1 : 1;
// Loop in direction perpendicular to "direction". Can be in any order
for (var i = 0; i < Grid.SIZE; ++i) {
var lastPos = backwards ? Grid.SIZE : -1;
var lastPosValue = null; // not used yet
for (var j = 0; j < Grid.SIZE; ++j) {
var x, y;
if (isVerticalMove) {
x = i;
y = backwards ? Grid.SIZE - 1 - j : j;
} else {
y = i;
x = backwards ? Grid.SIZE - 1 - j : j;
}
var valueAtPos = GameTransitions.getValue(currentState, x, y);
if (valueAtPos) {
if (valueAtPos === lastPosValue) {
valueAtPos = valueAtPos + 1;
lastPosValue = null;
} else {
lastPos += dj;
lastPosValue = valueAtPos;
}
if (isVerticalMove) {
GameTransitions.setTile(newState, valueAtPos, x, lastPos)
} else {
GameTransitions.setTile(newState, valueAtPos, lastPos, y);
}
}
}
}
return newState;
},
// Insert a tile at position x, y
// Mutates given state.
setTile: function(state, newValue, x, y) {
if (newValue <= 0) {
throw 'Cannot insert tile with value ' + newValue;
}
var currentValue = GameTransitions.getValue(state, x, y);
if (newValue > Grid.MAX_CELL_VAL) {
throw 'Tile ' + currentValue +
' is too large; can\'t insert tile ' + newValue;
}
var difference = newValue - currentValue
var offset = GameTransitions._getOffset(x, y);
if (offset >= Grid.CELLS_PER_STATE) {
state[0] += difference * Math.pow(
Grid.MAX_CELL_VAL + 1,
offset - Grid.CELLS_PER_STATE
);
} else {
state[1] += difference * Math.pow(Grid.MAX_CELL_VAL + 1, offset);
}
},
_getOffset: function(x, y) {
return y * Grid.SIZE + x;
},
getValue: function(state, xpos, ypos) {
var offset = GameTransitions._getOffset(xpos, ypos);
return (state[offset >= Grid.CELLS_PER_STATE ? 0 : 1]
>> (offset * Grid.OFFSET_PER_TILE)) & 0xF;
},
// A move is invalid if the state after moving is the same as
// before moving
isMoveInvalid: function(state, move) {
var movedState = GameTransitions.move(state, move);
return (movedState[0] == state[0] && movedState[1] == state[1]);
},
// Return the positions of cells that are empty
// ie: [[x1, y1], [x2, y2]]
getEmptyCells: function(state) {
var emptyCells = [];
for (var x = 0; x < Grid.SIZE; x++) {
for (var y = 0; y < Grid.SIZE; y++) {
if (GameTransitions.getValue(state, x, y) == 0) {
emptyCells.push([x, y]);
}
}
}
return emptyCells;
},
// Mainly for testing, debugging purposes
visualize: function(state) {
var chart = [];
for (var y = 0; y < Grid.SIZE; ++y) {
var row = [];
for (var x = 0; x < Grid.SIZE; ++x) {
row[x] = GameTransitions.getValue(state, x, y).toString(16);
}
chart[Grid.SIZE - 1 - y] = row.join(' ');
}
return chart.join("\n");
},
// [val, chance]
potentialRandomTiles: [
[1, .9],
[2, .1]
]
};
module.exports = GameTransitions;