-
Notifications
You must be signed in to change notification settings - Fork 2
/
move.js
131 lines (115 loc) · 5.65 KB
/
move.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
var anyMovement;
document.onkeydown = event=>{
el("swipe").currentTime = 0.6;
el("swipe").volume = 0.05;
el("swipe").play();
el("success").volume = 0;
anyMovement = false;
if(event.key == "ArrowUp"){
tiles.filter(i=>i.x==0).sort((a,b)=>a.y-b.y).forEach((item, index, all)=>{item.yN=index+2; checkSame(item,index,all,"y",-1);});
tiles.filter(i=>i.x==1).sort((a,b)=>a.y-b.y).forEach((item, index, all)=>{item.yN=index+1; checkSame(item,index,all,"y",-1);});
tiles.filter(i=>i.x==2).sort((a,b)=>a.y-b.y).forEach((item, index, all)=>{item.yN=index ; checkSame(item,index,all,"y",-1);});
tiles.filter(i=>i.x==3).sort((a,b)=>a.y-b.y).forEach((item, index, all)=>{item.yN=index ; checkSame(item,index,all,"y",-1);});
tiles.filter(i=>i.x==4).sort((a,b)=>a.y-b.y).forEach((item, index, all)=>{item.yN=index+1; checkSame(item,index,all,"y",-1);});
tiles.filter(i=>i.x==5).sort((a,b)=>a.y-b.y).forEach((item, index, all)=>{item.yN=index+2; checkSame(item,index,all,"y",-1);});
}
else if(event.key == "ArrowDown"){
tiles.filter(i=>i.x==0).sort((a,b)=>b.y-a.y).forEach((item, index, all)=>{item.yN=3-index; checkSame(item,index,all,"y",1);});
tiles.filter(i=>i.x==1).sort((a,b)=>b.y-a.y).forEach((item, index, all)=>{item.yN=4-index; checkSame(item,index,all,"y",1);});
tiles.filter(i=>i.x==2).sort((a,b)=>b.y-a.y).forEach((item, index, all)=>{item.yN=5-index; checkSame(item,index,all,"y",1);});
tiles.filter(i=>i.x==3).sort((a,b)=>b.y-a.y).forEach((item, index, all)=>{item.yN=5-index; checkSame(item,index,all,"y",1);});
tiles.filter(i=>i.x==4).sort((a,b)=>b.y-a.y).forEach((item, index, all)=>{item.yN=4-index; checkSame(item,index,all,"y",1);});
tiles.filter(i=>i.x==5).sort((a,b)=>b.y-a.y).forEach((item, index, all)=>{item.yN=3-index; checkSame(item,index,all,"y",1);});
}
else if(event.key == "ArrowRight"){
tiles.filter(i=>i.y==0).sort((a,b)=>b.x-a.x).forEach((item, index, all)=>{item.xN=3-index; checkSame(item,index,all,"x",1);});
tiles.filter(i=>i.y==1).sort((a,b)=>b.x-a.x).forEach((item, index, all)=>{item.xN=4-index; checkSame(item,index,all,"x",1);});
tiles.filter(i=>i.y==2).sort((a,b)=>b.x-a.x).forEach((item, index, all)=>{item.xN=5-index; checkSame(item,index,all,"x",1);});
tiles.filter(i=>i.y==3).sort((a,b)=>b.x-a.x).forEach((item, index, all)=>{item.xN=5-index; checkSame(item,index,all,"x",1);});
tiles.filter(i=>i.y==4).sort((a,b)=>b.x-a.x).forEach((item, index, all)=>{item.xN=4-index; checkSame(item,index,all,"x",1);});
tiles.filter(i=>i.y==5).sort((a,b)=>b.x-a.x).forEach((item, index, all)=>{item.xN=3-index; checkSame(item,index,all,"x",1);});
}
else if(event.key == "ArrowLeft"){
tiles.filter(i=>i.y==0).sort((a,b)=>a.x-b.x).forEach((item, index, all)=>{item.xN=index+2; checkSame(item,index,all,"x",-1);});
tiles.filter(i=>i.y==1).sort((a,b)=>a.x-b.x).forEach((item, index, all)=>{item.xN=index+1; checkSame(item,index,all,"x",-1);});
tiles.filter(i=>i.y==2).sort((a,b)=>a.x-b.x).forEach((item, index, all)=>{item.xN=index ; checkSame(item,index,all,"x",-1);});
tiles.filter(i=>i.y==3).sort((a,b)=>a.x-b.x).forEach((item, index, all)=>{item.xN=index ; checkSame(item,index,all,"x",-1);});
tiles.filter(i=>i.y==4).sort((a,b)=>a.x-b.x).forEach((item, index, all)=>{item.xN=index+1; checkSame(item,index,all,"x",-1);});
tiles.filter(i=>i.y==5).sort((a,b)=>a.x-b.x).forEach((item, index, all)=>{item.xN=index+2; checkSame(item,index,all,"x",-1);});
} else{
return;
}
tiles.forEach(tile=>{
tile.x += tile.xAdd;
tile.y += tile.yAdd;
tile.xAdd = 0;
tile.yAdd = 0;
});
if(anyMovement){
addNewRandomTile();
if(Math.random() < 0.1) addNewRandomTile();
}
tiles.filter(i=>i.delete).forEach(i=>{
setTimeout(()=>i.div.remove(), 100);
});
tiles.forEach(tile=>{
tile.div.innerHTML = tile.value;
if(!tile.div.style.backgroundColor){
setTimeout(()=>tile.div.style.backgroundColor = getCol(tile), 110);
}else{
tile.div.style.backgroundColor = getCol(tile);
}
tile.div.style.marginLeft = tile.x*90+10+"px";
tile.div.style.marginTop = tile.y*90+10+"px";
});
tiles = tiles.filter(i=>!i.delete);
localStorage.setItem("2048-circle",JSON.stringify(tiles.map(i=>{return {
value: i.value,
x: i.x,
y: i.y
}})));
}
function addNewRandomTile(){
var stop = false;
var preventInfinite = 0;
var outOfGrid = ["0,0","1,0","4,0","5,0","0,1","5,1","0,4","5,4","0,5","1,5","4,5","5,5"];
while(!stop && preventInfinite < 1000){
preventInfinite ++;
var newTile = {
value: 2**Math.floor(Math.random()*1.5+1),
x: Math.floor(Math.random()*6),
y: Math.floor(Math.random()*6)
};
if(tiles.filter(i=>i.x==newTile.x && i.y==newTile.y).length == 0 && !outOfGrid.includes(newTile.x+","+newTile.y)){
stop = true;
tiles.push(makeNewTile(newTile));
}
}
}
function checkSame(item, index, all, xOrY, plusOrMinus){
if(item[xOrY+"N"] != item[xOrY]) anyMovement = true;
item[xOrY] = item[xOrY+"N"];
if(index == 0) return;
var prev = all[index-1];
if(prev.value == item.value && !prev.delete){
anyMovement = true
prev.value *= 2;
item.delete = true;
item.x = prev.x;
item.y = prev.y;
item.div.style.zIndex = "1";
el("success").currentTime = 0;
el("success").volume = Math.max((Math.log2(prev.value)/12)**2, el("success").volume);
el("success").play();
if(prev.value == 2048){
setTimeout(()=>{
alert("Congratulations, you win! \n(>'-')> <('-'<) ^(' - ')^ <('-'<) (>'-')>(>'-')> <('-'<) ^(' - ')^ <('-'<) (>'-')>\n\nFeel free to continue playing and try to get 4096.")
}, 100);
}
all.forEach((item2, index2)=>{
if(index2 > index){
item2[xOrY+"Add"] += plusOrMinus;
}
});
}
}