-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
139 lines (130 loc) · 3.13 KB
/
script.js
File metadata and controls
139 lines (130 loc) · 3.13 KB
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
import Grid from './grid.js'
import Tile from './Tile.js'
let gameboard=document.querySelector('.game-board')
let grid=new Grid(gameboard)
grid.randomEmptyCell().tile=new Tile(gameboard)
grid.randomEmptyCell().tile=new Tile(gameboard)
const control=document.querySelector('.control')
function setInput(){
control.addEventListener('click',handleinput,{once:true})
}
setInput()
async function handleinput(e){
const elem=e.target.parentNode
let quit=false;
/*elem.classList.contains('moveup') ? await moveup() : elem.classList.contains('movedown') ? await movedown() : elem.classList.contains('moveright') ? await moveright() :elem.classList.contains('moveleft') ? await moveleft() : quit=true*/
if(elem.classList.contains('moveup')){
if(!canmoveup()){
setInput()
return
}
await moveup()
}
else if(elem.classList.contains('movedown')){
if(!canmovedown()){
setInput()
return
}
await movedown()
}
else if(elem.classList.contains('moveright')){
if(!canmoveright()){
setInput()
return
}
await moveright()
}
else if(elem.classList.contains('moveleft')){
if(!canmoveleft()){
setInput()
return
}
await moveleft()
}
else{
quit=true
}
if(quit){
setInput()
return
};
grid.cells.forEach(cell=>cell.mergeTiles())
let newone=new Tile(gameboard)
grid.randomEmptyCell().tile=newone
if(!canmoveup() && !canmovedown() && !canmoveleft() && !canmoveright()){
newone.waitfortrans(true).then(()=>{alert('you lose')
window.location='./'
})
}
else{
setInput()}
}
function moveup(){
return slideTiles(grid.cellsbyColumn)
}
function movedown(){
return slideTiles(grid.cellsbyColumn.map((grp)=>{
return [...grp.reverse()]
}))
}
function moveleft(){
return slideTiles(grid.cellsbyRow)
}
function moveright(){
return slideTiles(grid.cellsbyRow.map((grp)=>{
return [...grp.reverse()]
}))
}
function slideTiles(cells){
return Promise.all(
cells.flatMap((group)=>{
const promises=[]
for(let i=1;i<group.length;i++){
let cell=group[i]
if(cell.tile ==null || cell.tile== undefined ) continue
let lastvalid;
for(let j=i-1;j>=0;j--){
let movetocell=group[j]
if(!movetocell.canaccept(cell.tile)) break
lastvalid=movetocell
}
if(lastvalid !=null){
promises.push(cell.tile.waitfortrans())
if(lastvalid.tile != null){
lastvalid.mergetile =cell.tile
}
else{
lastvalid.tile=cell.tile
}
cell.tile= null
}
}
return promises
}))
}
function canmoveup(){
return canmove(grid.cellsbyColumn)
}
function canmovedown(){
return canmove(grid.cellsbyColumn.map((grp)=>{
return [...grp.reverse()]
}))
}
function canmoveleft(){
return canmove(grid.cellsbyRow)
}
function canmoveright(){
return canmove(grid.cellsbyRow.map((grp)=>{
return [...grp.reverse()]
}))
}
function canmove(arr){
return arr.some((grps)=>{
return grps.some((cell,index)=>{
if(index==0)return false
if(cell.tile == null) return false
const moveto=grps[index-1]
return moveto.canaccept(cell.tile)
})
})
}