-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
396 lines (354 loc) · 16 KB
/
script.js
File metadata and controls
396 lines (354 loc) · 16 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// ---------- 游戏配置 ----------
const GRID_SIZE = 20;
const CELL_SIZE = 20;
const BASE_MOVE_INTERVAL = 300; // 基础间隔300ms,给模拟计算留足时间
// 游戏状态
let snake = []; // 蛇身坐标 {x, y}
let food = { x: 0, y: 0 };
let direction = 'right';
let nextDirection = 'right';
let score = 0;
let aiInterval = null;
let gameRunning = false;
let gameWinFlag = false; // 是否胜利结束
// DOM 元素
const gameEl = document.getElementById('game');
const startAIEl = document.getElementById('startAI');
const stopAIEl = document.getElementById('stopAI');
const resetEl = document.getElementById('reset');
const statusEl = document.getElementById('status');
// ---------- 辅助函数 ----------
function copySnake(snakeArr) {
return snakeArr.map(seg => ({ ...seg }));
}
// 动态步数: 根据蛇长和剩余空格决定前瞻深度 (范围 4~10)
function getDynamicSteps() {
const totalCells = GRID_SIZE * GRID_SIZE;
const emptyCells = totalCells - snake.length;
const len = snake.length;
if (emptyCells <= 15) return 10; // 快满了,深度模拟
if (emptyCells <= 30 || len > 300) return 8;
if (len > 200) return 7;
if (len > 100) return 6;
return 5; // 基础5步
}
// 洪水填充: 计算从 startPos 出发能到达的格子数 (不考虑尾巴释放,直接按当前蛇身障碍)
// snakeArr 是完整的蛇身数组(模拟时传入)
function getReachableArea(startPos, snakeArr) {
const snakeSet = new Set(snakeArr.map(s => `${s.x},${s.y}`));
const queue = [{ x: startPos.x, y: startPos.y }];
const visited = new Set([`${startPos.x},${startPos.y}`]);
while (queue.length) {
const { x, y } = queue.shift();
const neighbors = [
{ x, y: y-1 }, { x, y: y+1 },
{ x: x-1, y }, { x: x+1, y }
];
for (const n of neighbors) {
if (n.x < 0 || n.x >= GRID_SIZE || n.y < 0 || n.y >= GRID_SIZE) continue;
const key = `${n.x},${n.y}`;
if (visited.has(key)) continue;
if (snakeSet.has(key)) continue;
visited.add(key);
queue.push(n);
}
}
return visited.size;
}
// 在模拟中,给定蛇身和当前食物,重新生成一个不重叠的食物 (快速尝试)
function regenerateFoodForSimulation(snakeArr, currentFood) {
const total = GRID_SIZE * GRID_SIZE;
if (snakeArr.length >= total) return null; // 胜利无空位
// 随机尝试150次
for (let i = 0; i < 150; i++) {
const newFood = {
x: Math.floor(Math.random() * GRID_SIZE),
y: Math.floor(Math.random() * GRID_SIZE)
};
if (!snakeArr.some(seg => seg.x === newFood.x && seg.y === newFood.y)) {
return newFood;
}
}
// 降级:顺序查找
for (let y = 0; y < GRID_SIZE; y++) {
for (let x = 0; x < GRID_SIZE; x++) {
if (!snakeArr.some(seg => seg.x === x && seg.y === y)) {
return { x, y };
}
}
}
return null; // 全满
}
// 模拟在某个方向走 steps 步,返回评分(越高越好)
// 内部贪心决策:每一步选择安全的、最接近食物的方向(避免递归,轻量)
function simulateDirection(initialSnake, initialFood, startDir, steps) {
let simSnake = copySnake(initialSnake);
let simFood = { ...initialFood };
let simDir = startDir;
let reward = 0; // 吃到食物累计奖励
let ateCount = 0;
for (let step = 0; step < steps; step++) {
const head = simSnake[0];
let newHead = { ...head };
switch (simDir) {
case 'up': newHead.y--; break;
case 'down': newHead.y++; break;
case 'left': newHead.x--; break;
case 'right': newHead.x++; break;
}
// 碰撞检测
if (newHead.x < 0 || newHead.x >= GRID_SIZE || newHead.y < 0 || newHead.y >= GRID_SIZE) {
return -10000 + reward; // 死亡惩罚极大
}
if (simSnake.some(seg => seg.x === newHead.x && seg.y === newHead.y)) {
return -10000 + reward;
}
// 移动
simSnake.unshift(newHead);
const ate = (newHead.x === simFood.x && newHead.y === simFood.y);
if (ate) {
// 吃到食物奖励加大 (基础+200,每多吃一次额外累加)
reward += 250;
ateCount++;
// 重新生成食物
const newFood = regenerateFoodForSimulation(simSnake, simFood);
if (!newFood) {
// 模拟胜利,给予巨额奖励
return 100000 + reward;
}
simFood = newFood;
// 吃到食物不删尾巴,长度+1
} else {
simSnake.pop(); // 没吃到删尾巴
}
// 如果已经填满全场 => 胜利
if (simSnake.length === GRID_SIZE * GRID_SIZE) {
return 100000 + reward;
}
// ---------- 为下一步选择方向 (轻量贪心: 安全+距离优先) ----------
const opposite = { up:'down', down:'up', left:'right', right:'left' };
const possibleMoves = ['up', 'down', 'left', 'right'].filter(d => d !== opposite[simDir]);
let bestNextDir = null;
let bestDist = Infinity;
const curHead = simSnake[0];
for (const d of possibleMoves) {
let testHead = { ...curHead };
switch (d) {
case 'up': testHead.y--; break;
case 'down': testHead.y++; break;
case 'left': testHead.x--; break;
case 'right': testHead.x++; break;
}
// 边界或撞自身检查
if (testHead.x < 0 || testHead.x >= GRID_SIZE || testHead.y < 0 || testHead.y >= GRID_SIZE) continue;
if (simSnake.some(seg => seg.x === testHead.x && seg.y === testHead.y)) continue;
const dist = Math.abs(testHead.x - simFood.x) + Math.abs(testHead.y - simFood.y);
if (dist < bestDist) {
bestDist = dist;
bestNextDir = d;
}
}
if (bestNextDir) {
simDir = bestNextDir;
} else {
// 无路可走,即将死亡
return -8000 + reward;
}
}
// 模拟结束,评估最终状态的生存空间 + 距离食物的吸引力
const finalHead = simSnake[0];
const finalArea = getReachableArea(finalHead, simSnake);
const distToFood = Math.abs(finalHead.x - simFood.x) + Math.abs(finalHead.y - simFood.y);
// 评分权重: 生存空间 * 12 + 吃到食物奖励 - 距离 * 3
const finalScore = finalArea * 12 - distToFood * 3 + reward * 1.2;
return finalScore;
}
// ---------- AI 决策核心:前瞻评估每个方向 ----------
function aiDecideDirection() {
if (!gameRunning) return;
const head = snake[0];
const opposite = { up:'down', down:'up', left:'right', right:'left' };
const possibleDirs = ['up', 'down', 'left', 'right'].filter(d => d !== opposite[direction]);
let bestDir = null;
let bestScore = -Infinity;
const steps = getDynamicSteps(); // 动态步数
for (const dir of possibleDirs) {
// 快速过滤:第一步即撞墙/身的直接丢弃 (避免无效模拟)
let testHead = { ...head };
switch (dir) {
case 'up': testHead.y--; break;
case 'down': testHead.y++; break;
case 'left': testHead.x--; break;
case 'right': testHead.x++; break;
}
if (testHead.x < 0 || testHead.x >= GRID_SIZE || testHead.y < 0 || testHead.y >= GRID_SIZE) continue;
if (snake.some(seg => seg.x === testHead.x && seg.y === testHead.y)) continue;
const scoreVal = simulateDirection(snake, food, dir, steps);
if (scoreVal > bestScore) {
bestScore = scoreVal;
bestDir = dir;
}
}
if (bestDir) {
nextDirection = bestDir;
} else {
// 保底:什么都不选就保持原方向(但这种情况极少,一般不会)
if (possibleDirs.length > 0) nextDirection = possibleDirs[0];
}
}
// ---------- 原有游戏逻辑(移动,碰撞,渲染)----------
function generateFood() {
if (snake.length >= GRID_SIZE * GRID_SIZE) {
gameWin();
return;
}
// 快速随机尝试
for (let i = 0; i < 1000; i++) {
const randX = Math.floor(Math.random() * GRID_SIZE);
const randY = Math.floor(Math.random() * GRID_SIZE);
if (!snake.some(seg => seg.x === randX && seg.y === randY)) {
food = { x: randX, y: randY };
return;
}
}
// 极端情况:遍历所有格子
for (let y = 0; y < GRID_SIZE; y++) {
for (let x = 0; x < GRID_SIZE; x++) {
if (!snake.some(seg => seg.x === x && seg.y === y)) {
food = { x, y };
return;
}
}
}
gameWin();
}
function renderGrid() {
gameEl.innerHTML = '';
gameEl.style.display = 'grid';
gameEl.style.gridTemplateColumns = `repeat(${GRID_SIZE}, ${CELL_SIZE}px)`;
gameEl.style.gridTemplateRows = `repeat(${GRID_SIZE}, ${CELL_SIZE}px)`;
gameEl.style.gap = '1px';
gameEl.style.width = `${GRID_SIZE * CELL_SIZE + GRID_SIZE - 1}px`;
for (let y = 0; y < GRID_SIZE; y++) {
for (let x = 0; x < GRID_SIZE; x++) {
const cell = document.createElement('div');
cell.style.width = `${CELL_SIZE}px`;
cell.style.height = `${CELL_SIZE}px`;
const isHead = (snake[0] && snake[0].x === x && snake[0].y === y);
const isSnakeBody = !isHead && snake.some(seg => seg.x === x && seg.y === y);
if (isHead) {
cell.className = 'snake-head';
} else if (isSnakeBody) {
cell.className = 'snake';
} else if (x === food.x && y === food.y) {
cell.className = 'food';
} else {
cell.className = 'empty';
cell.style.backgroundColor = '#f0f0f000';
}
gameEl.appendChild(cell);
}
}
}
function moveSnake() {
if (!gameRunning) return;
direction = nextDirection;
const head = { ...snake[0] };
switch (direction) {
case 'up': head.y--; break;
case 'down': head.y++; break;
case 'left': head.x--; break;
case 'right': head.x++; break;
default: break;
}
// 碰撞边界或自身
if (head.x < 0 || head.x >= GRID_SIZE || head.y < 0 || head.y >= GRID_SIZE ||
snake.some(seg => seg.x === head.x && seg.y === head.y)) {
gameOver();
return;
}
snake.unshift(head);
const ate = (head.x === food.x && head.y === food.y);
if (ate) {
score += 10;
generateFood(); // 可能会触发胜利
if (!gameRunning) return;
} else {
snake.pop();
}
renderGrid();
updateStatus();
// 胜利检测 (generateFood 里可能触发胜利,但二次确认)
if (snake.length === GRID_SIZE * GRID_SIZE && !gameWinFlag) {
gameWin();
}
}
function gameOver() {
if (!gameRunning) return;
gameRunning = false;
gameWinFlag = false;
stopAI();
statusEl.textContent = `状态:💀 游戏结束 | 得分:${score} | 蛇长:${snake.length}`;
alert(`游戏结束!最终得分:${score}`);
}
function gameWin() {
if (!gameRunning) return;
gameRunning = false;
gameWinFlag = true;
stopAI();
statusEl.textContent = `状态:🏆 胜利!满分通关 | 得分:${score} | 蛇长:${snake.length}`;
alert(`🎉 恭喜获胜!完美通关! 得分:${score}`);
}
function updateStatus() {
const aiStatus = aiInterval ? '🤖 AI运行中' : '⏸️ 未运行';
statusEl.innerHTML = `状态:${aiStatus} | 得分:${score} | 蛇长:${snake.length} | 前瞻步数: ${getDynamicSteps()}`;
}
// ---------- 重置 / 初始化 ----------
function initGame() {
stopAI();
// 初始蛇
snake = [
{ x: 5, y: 10 },
{ x: 4, y: 10 },
{ x: 3, y: 10 }
];
direction = 'right';
nextDirection = 'right';
score = 0;
gameRunning = true;
gameWinFlag = false;
generateFood();
renderGrid();
updateStatus();
}
// AI 移动驱动
function aiMove() {
if (!gameRunning) return;
aiDecideDirection();
moveSnake();
}
function startAI() {
if (!gameRunning) {
// 游戏未运行或已结束,重置后再启动
initGame();
}
if (aiInterval) clearInterval(aiInterval);
aiInterval = setInterval(aiMove, BASE_MOVE_INTERVAL);
updateStatus();
}
function stopAI() {
if (aiInterval) {
clearInterval(aiInterval);
aiInterval = null;
updateStatus();
}
}
// 事件绑定
startAIEl.addEventListener('click', startAI);
stopAIEl.addEventListener('click', stopAI);
resetEl.addEventListener('click', () => {
stopAI();
initGame();
});
// 启动游戏初始
initGame();