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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const Game = {}
const MoveFunc = [
	(l,i)=>l+i*4,//U
	(l,i)=>l*4+i,//L
	(l,i)=>l+(3-i)*4,//D
	(l,i)=>l*4+(3-i)//R
]
MoveFunc.up = MoveFunc[0];
MoveFunc.down = MoveFunc[2];
MoveFunc.left = MoveFunc[1];
MoveFunc.right = MoveFunc[3];

function random() {
	with(Game){
		a |= 0; b |= 0; c |= 0; d |= 0;
		var t = a - (b << 27 | b >>> 5) | 0;
		a = b ^ (c << 17 | c >>> 15);
		b = c + d | 0;
		c = d + t | 0;
		d = a + t | 0;
		return (d >>> 0) / 4294967296;
	}
}

function cyrb128(str) {
	let h1 = 1779033703, h2 = 3144134277,
		h3 = 1013904242, h4 = 2773480762;
	for (let i = 0, k; i < str.length; i++) {
		k = str.charCodeAt(i);
		h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
		h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
		h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
		h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
	}
	h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
	h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
	h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
	h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
	return [(h1^h2^h3^h4)>>>0, (h2^h1)>>>0, (h3^h1)>>>0, (h4^h1)>>>0];
}

function fillChar(c,l){
	c = String(c);
	return c+' '.repeat(Math.max(l-c.length,0));
}

function consoleWrite(message=''){
	var s = '';
	for(var i=0; i<16; i+=4){
		s += Game.x.slice(i,i+4)
		.map( x=> x?x:'' )
		.map( x=>fillChar(x,5) )
		.join(',') + '\n'
	}
	console.log( s + 'step: ' + Game.s + '\n' + message )
}

function move(posFunc, simulate=false ){
	var available = false;
	for(var line = 0; line<4; line++ ){
		var map = [];
		var data = [];
		var dataI = 0;
		for(var i = 0; i<4; i++ ){
			var p=posFunc(line,i);
			map.push(p);
			var v = Game.x[p];
			if(v){
				if(data[dataI] == v){
					data[dataI] += v;
				} else {
					data.push(v);
					dataI = data.length -1;
				}
			}
		}
		available ||= data.length < 4 && map.some((s,i)=>Game.x[s]!==data[i]?data[i]:null);
		if(simulate) continue;
		for(var i = 0; i<4; i++ ){
			Game.x[map[i]] = data[i]?data[i]:null;
		}
	}
	return available;
}

function check(){
	var emptyCount = 0;
	Game.x.forEach( w=>{ if(!w) emptyCount++ } )
	if(!emptyCount) return null;
	return emptyCount;
}

function insert(){
	var emptyCount = check();
	if(!emptyCount) return null;
	
	var emptyIndex = Game.x.map((_,i)=>i).filter(i=>!Game.x[i])
	
	var insertIndex = Math.floor( emptyCount * random() );
	Game.x[emptyIndex[insertIndex]] = 2;
	return insertIndex;
}

Game.up = function(){
	if(Game.g){
		consoleWrite('Game Over');
		return;
	}
	var available = move( MoveFunc.up )
	if(!available){
		consoleWrite('Can\'t move');
		return;
	}
	
	insert()
	Game.s++;
	if(!check()&&!MoveFunc.some( f=>move(f,true) )){
		Game.g = true;
		consoleWrite('Game Over');
	} else {
		consoleWrite();
	}
	
}

Game.down = function(){
	if(Game.g){
		consoleWrite('Game Over');
		return;
	}
	var available = move( MoveFunc.down )
	if(!available){
		consoleWrite('Can\'t move');
		return;
	}
	
	insert()
	Game.s++;
	if(!check()&&!MoveFunc.some( f=>move(f,true) )){
		Game.g = true;
		consoleWrite('Game Over');
	} else {
		consoleWrite();
	}
}

Game.left = function(){
	if(Game.g){
		consoleWrite('Game Over');
		return;
	}
	var available = move( MoveFunc.left )
	if(!available){
		consoleWrite('Can\'t move');
		return;
	}
	
	insert()
	Game.s++;
	if(!check()&&!MoveFunc.some( f=>move(f,true) )){
		Game.g = true;
		consoleWrite('Game Over');
	} else {
		consoleWrite();
	}
}

Game.right = function(){
	if(Game.g){
		consoleWrite('Game Over');
		return;
	}
	var available = move( MoveFunc.right )
	if(!available){
		consoleWrite('Can\'t move');
		return;
	}
	
	insert()
	Game.s++;
	if(!check()&&!MoveFunc.some( f=>move(f,true) )){
		Game.g = true;
		consoleWrite('Game Over');
	} else {
		consoleWrite();
	}
}

Game.reset = function(seed){
	if(seed==''||!seed) seed = String(Math.random());
	var ss = cyrb128(seed);
	Game.a = ss[0];
	Game.b = ss[1];
	Game.c = ss[2];
	Game.d = ss[3];
	Game.s = 0; //step
	Game.x = [
		null,null,null,null,
		null,null,null,null,
		null,null,null,null,
		null,null,null,null
	]
	insert();
	insert();
	
	consoleWrite();
}

Game.save = function(){
	return [Game.s,...Game.x,Game.a,Game.b,Game.c,Game.b];
}

Game.load = function(data){
	Game.a = data[17];
	Game.b = data[18];
	Game.c = data[19];
	Game.d = data[20];
	Game.g = false; //Game Over
	Game.s = data[0]; //step
	Game.x = [
		data[1],data[2],data[3],data[4],
		data[5],data[6],data[7],data[8],
		data[9],data[10],data[11],data[12],
		data[13],data[14],data[15],data[16]
	]
}