-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2048_game.js
194 lines (175 loc) · 5.01 KB
/
2048_game.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var game = {};
game.left = [0, -1];
game.right = [0, 1];
game.down = [1, 0];
game.up = [-1, 0];
game.allMoves = [game.left, game.right, game.down, game.up];
game.bounds = function(row, col) {
return row >= 0 && row < 4 && col >=0 && col < 4;
}
game.empty = function() {
return new Position([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0);
};
/**
* @constructor
*/
function Position(arr, score) {
this.arr = arr;
this.score = score;
};
Position.prototype.index = function(row, col) {
return row * 4 + col;
};
Position.prototype.direct = function(index) {
return this.arr[index]
};
Position.prototype.at = function(row, col) {
return this.arr[row * 4 + col];
};
Position.prototype.set = function(row, col, val) {
this.arr[row * 4 + col] = val;
};
Position.prototype.moves = function() {
var allMoves = [ game.left, game.right, game.down, game.up ];
var moves = [];
for (var i = 0; i < allMoves.length; i++) {
var move = allMoves[i];
var p = this.makeMove(move);
if (p != null) {
moves.push(move);
}
}
return moves;
};
Position.prototype.isValid = function(move) {
return true;
};
Position.prototype.makeMove = function(move) {
var p = game.empty();
p.score = this.score;
if (move[0] == 0) {
var dir = move[1];
var positive = dir > 0;
var start = positive ? 3 : 0;
var end = positive ? -1 : 4;
for (var x = 0; x != 4; x++) {
var reduced = [];
for (var y = start; y != end; y-= dir) {
var cursor = this.at(x, y);
if (cursor != 0) {
reduced.push(cursor);
}
}
var i = 0;
for (var y = start; y != end; y-= dir) {
if (i < reduced.length) {
var cursor = reduced[i];
i++;
if (i < reduced.length) {
var pcursor = reduced[i];
if (cursor == pcursor) {
var newValue = cursor + pcursor;
p.set(x, y, newValue);
p.score += newValue;
i++;
} else {
p.set(x, y, cursor);
}
} else {
p.set(x, y, cursor);
}
} else {
break;
}
}
}
} else {
var dir = move[0];
var positive = dir > 0;
var start = positive ? 3 : 0;
var end = positive ? -1 : 4;
for (var y = 0; y != 4; y++) {
var reduced = [];
for (var x = start; x != end; x-= dir) {
var cursor = this.at(x, y);
if (cursor != 0) {
reduced.push(cursor);
}
}
var i = 0;
for (var x = start; x != end; x-= dir) {
if (i < reduced.length) {
var cursor = reduced[i];
i++;
if (i < reduced.length) {
var pcursor = reduced[i];
if (cursor == pcursor) {
var newValue = cursor + pcursor;
p.set(x, y, newValue);
p.score += newValue;
i++;
} else {
p.set(x, y, cursor);
}
} else {
p.set(x, y, cursor);
}
} else {
break;
}
}
}
}
var validChange = false;
for (var r = 0; r < 4; r++) {
for (var c = 0; c < 4; c++) {
if (this.at(r, c) != p.at(r, c)) {
validChange = true;
return p;
}
}
}
return null;
};
Position.prototype.evolve = function() {
var choices = [];
for (var i = 0; i < 16; i++) {
if (this.arr[i] == 0) {
choices.push(i);
}
}
if (choices.length == 0) {
// Game over!!!
return null;
}
var index = randInt(0, choices.length);
var loc = choices[index];
var newTerm = Math.random() < .9 ? 2 : 4;
var p = new Position(this.arr.slice(0), this.score);
p.set(Math.floor(loc/4), loc%4, newTerm);
return p;
// return this;
};
Position.prototype.gameOver = function() {
return this.moves().length == 0;
};
function randInt(low, high) {
var x = low + Math.floor(Math.random() * (high - low));
if (x >= high) {
x = high - 1;
}
return x;
}
game.random = function() {
// return game.empty();
var p = game.empty();
var x = randInt(0, 16);
var y = randInt(0, 15);
if (y >= x) {
y = y + 1;
}
p.set(Math.floor(x / 4), x % 4, 2);
p.set(Math.floor(y / 4), y % 4, 2);
console.log(x, y);
return p;
}