-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2048.js
204 lines (176 loc) · 4.42 KB
/
2048.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
195
196
197
198
199
200
201
202
203
204
module.exports = class Game2048{
constructor(size){
this.size = size;
this.layout = new Array(size);
for(let i=0; i<size; i++){
this.layout[i] = new Array(size);
}
//assert(this.size=this.layout.length)
for(let i=0; i<size; i++){
for(let j=0; j<size; j++){
this.layout[i][j] = 0;
}
}
this.__update_layout();
this.__update_layout();
//for test:
// this.layout[0] = [0,0,0,0];
// this.layout[1] = [0,0,2,0];
// this.layout[2] = [0,0,0,4];
// this.layout[3] = [0,0,0,0];
}
//show the current layout
show(){
for(let i=0; i<this.size; i++){
console.log(this.layout[i]);
for(let j=0; j<this.size; j++){
//console.log("%10c",this.layout[i][j])
}
}
//console.log(this.layout)
}
__move_left(){
let movable = 0;
for(let i = 0; i < this.size; i++){
let compressed = [];
let meetblank = 0;
for(let j = 0; j < this.size; j++){
if(this.layout[i][j]){
compressed.push(this.layout[i][j]);
if(meetblank)
movable = 1;
}
else
meetblank = 1;
}
for(let k = 0; k + 1 < compressed.length; k++){
if(compressed[k] && compressed[k] === compressed[k+1]){
movable = 1;
compressed[k] *= 2;
compressed = compressed.slice(0,k+1).concat(compressed.slice(k+2))
}
}
this.layout[i] = compressed.concat(new Array(this.size-compressed.length).fill(0));
}
if(movable === 0){
return false;
}else{
return true;
}
}
__move_right(){
for(let i = 0; i < this.size; i++){
this.layout[i].reverse();
}
let result = this.__move_left();
for(let i = 0; i < this.size; i++){
this.layout[i].reverse();
}
return result;
}
__move_up(){
let transpose = a => a[0].map((_, c) => a.map(r => r[c]));
this.layout = transpose(this.layout);
let result = this.__move_left();
this.layout = transpose(this.layout);
return result;
}
__move_down(){
let transpose = a => a[0].map((_, c) => a.map(r => r[c]));
this.layout = transpose(this.layout);
let result = this.__move_right();
this.layout = transpose(this.layout);
return result;
}
__move(direction){
if(direction === "left")
return this.__move_left();
else if(direction === "right")
return this.__move_right();
else if(direction === "up")
return this.__move_up();
else if(direction === "down")
return this.__move_down();
}
move(direction){
if(this.__move(direction)){
if(this.__check_win()){
console.log("You win!");
return "win";
}else{
this.__update_layout();
if(this.__check_movable()){
return "continue";
}else{
console.log("You lost!");
return "lost";
}
}
}
console.log("not movable");
return "continue";
}
left(){
return this.move("left");
}
right(){
return this.move("right");
}
down(){
return this.move("down");
}
up(){
return this.move("up");
}
//judgut if already win
__check_win(){
for(let i=0; i<this.size; i++){
for(let j=0; j<this.size; j++){
if(this.layout[i][j]===2048){
return true;
}
}
}
return false;
}
__new_w(){
//in the ratio of 1:4 to generate 4:2
return (Math.floor(5*Math.random())>0)?2:4;
}
//add one weight into the current layout
__update_layout(){
let empty_blocks = [];
for(let i=0; i<this.size; i++){
for(let j=0; j<this.size; j++){
if(this.layout[i][j] === 0){
empty_blocks.push([i,j]);
}
}
}
let insert_n = Math.floor(Math.random()*empty_blocks.length);
var [insert_i,insert_j] = [empty_blocks[insert_n][0],empty_blocks[insert_n][1]];
this.layout[insert_i][insert_j] = this.__new_w();
}
__check_movable(){
for(let i=0; i<this.size; i++){
for(let j=0; j<this.size; j++){
if(this.layout[i][j] === 0){
return true;
}
}
}
for(let i=0; i<this.size; i++){
for(let j=0; j<this.size-1; j++){
if(this.layout[i][j] === this.layout[i][j+1])
return true;
}
}
for(let j=0; j<this.size; j++){
for(let i=0; i<this.size-1; i++){
if(this.layout[i][j] === this.layout[i+1][j])
return true;
}
}
return false;
}
}