-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsokoban.core.js
212 lines (170 loc) · 4.56 KB
/
sokoban.core.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
205
206
207
208
209
210
211
212
'use strict';
// tags
// '#' = wall
// ' ' = floor
// '.' = goal square
// '@' = player
// '+' = player on a goal square
// '$' = box
// '*' = box on a goal square
const init = (level) => {
let top = Infinity;
let bottom = 0;
let left = Infinity;
let right = 0;
for (const i in level) {
for (const j in level[i]) {
if ('#.@+$*'.indexOf(level[i][j]) >= 0) {
top = Math.min(top, i);
bottom = Math.max(bottom, i);
left = Math.min(left, j);
right = Math.max(right, j);
}
}
}
const map = [];
for (let i = top; i <= bottom; i += 1) {
map.push([]);
for (let j = left; j <= right; j += 1) {
if ('# .@+$*'.indexOf(level[i][j]) >= 0) {
map[i].push(level[i][j]);
} else {
map[i].push(' ');
}
}
}
return map;
};
const win = (map) => {
let goal = false;
let box = false;
for (const i in map) {
for (const j in map[i]) {
goal = goal || map[i][j] === '.' || map[i][j] === '+';
box = box || map[i][j] === '$';
}
}
return !goal || !box;
};
const findPlayer = (map) => {
for (let i = 0; i < map.length; i += 1) {
for (let j = 0; j < map[i].length; j += 1) {
if (map[i][j] === '@' || map[i][j] === '+') {
return [i, j];
}
}
}
};
const isBox = (map, i, j) => {
return map[i][j] === '$' || map[i][j] === '*';
};
const pushPlayer = (map, i, j) => {
if (map[i][j] === ' ') {
map[i][j] = '@';
} else if (map[i][j] === '.') {
map[i][j] = '+';
}
};
const popPlayer = (map, i, j) => {
if (map[i][j] === '@') {
map[i][j] = ' ';
} else if (map[i][j] === '+') {
map[i][j] = '.';
}
};
const pushBox = (map, i, j) => {
if (map[i][j] === ' ') {
map[i][j] = '$';
} else if (map[i][j] === '.') {
map[i][j] = '*';
}
};
const popBox = (map, i, j) => {
if (map[i][j] === '$') {
map[i][j] = ' ';
} else if (map[i][j] === '*') {
map[i][j] = '.';
}
};
const findPath = (map, playerI, playerJ, targetI, targetJ) => {
const visited = {};
const dfs = (i, j) => {
if (visited[i + '_' + j]) {
return false;
}
visited[i + '_' + j] = true;
if (map[i][j] === ' ' || map[i][j] === '.') {
return i === targetI && j === targetJ
|| i - 1 >= 0 && dfs(i - 1, j)
|| i + 1 < map.length && dfs(i + 1, j)
|| j - 1 >= 0 && dfs(i, j - 1)
|| j + 1 < map[i].length && dfs(i, j + 1);
}
return false;
};
return dfs(playerI, playerJ);
};
const move = (map, targetI, targetJ) => {
const player = findPlayer(map);
const playerI = player[0];
const playerJ = player[1];
if (targetI === playerI && targetJ === playerJ) {
return false;
}
popPlayer(map, playerI, playerJ);
if (findPath(map, playerI, playerJ, targetI, targetJ)) {
pushPlayer(map, targetI, targetJ);
return true;
}
pushPlayer(map, playerI, playerJ);
return false;
};
const push = (map, boxI, boxJ, targetI, targetJ) => {
let deltaI = null;
let deltaJ = null;
if (targetJ === boxJ && targetI < boxI) {
deltaI = -1;
deltaJ = 0;
} else if (targetJ === boxJ && targetI > boxI) {
deltaI = 1;
deltaJ = 0;
} else if (targetI === boxI && targetJ < boxJ) {
deltaI = 0;
deltaJ = -1;
} else if (targetI === boxI && targetJ > boxJ) {
deltaI = 0;
deltaJ = 1;
} else {
return false;
}
const player = findPlayer(map);
const playerI = player[0];
const playerJ = player[1];
popPlayer(map, playerI, playerJ);
let scanI = boxI;
let scanJ = boxJ;
while (scanI !== targetI || scanJ !== targetJ) {
scanI += deltaI;
scanJ += deltaJ;
if (map[scanI][scanJ] !== ' ' && map[scanI][scanJ] !== '.') {
pushPlayer(map, playerI, playerJ);
return false;
}
}
if (findPath(map, playerI, playerJ, boxI - deltaI, boxJ - deltaJ)) {
popBox(map, boxI, boxJ);
pushBox(map, targetI, targetJ);
pushPlayer(map, targetI - deltaI, targetJ - deltaJ);
return true;
}
pushPlayer(map, playerI, playerJ);
return false;
};
module.exports = {
init: init,
win: win,
findPlayer: findPlayer,
isBox: isBox,
move: move,
push: push,
};