Repository navigation
Expand file tree
/
Copy pathai-init.js
More file actions
58 lines (54 loc) · 1.64 KB
/
Copy pathai-init.js
File metadata and controls
58 lines (54 loc) · 1.64 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
const mySnakeBoard = new SNAKE.Board({
boardContainer: "game-area",
fullScreen: true,
premoveOnPause: false,
moveSnakeWithAI: ({
grid,
snakeHead,
currentDirection,
isFirstGameMove,
setDirection,
}) => {
/*
Direction:
0
3 1
2
*/
// This is NOT a real hamiltonian cycle. It misses some values, I'm just including this here as an example of
// a look-up type table that you could do.
const hamiltonianCycleGrid = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0],
[0, 0, 2, 3, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 0],
[0, 0, 2, 0, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0],
[0, 0, 2, 0, 2, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0],
[0, 0, 3, 0, 3, 3, 3, 3, 0, 3, 0, 3, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
console.log(JSON.parse(JSON.stringify(grid)))
console.log(snakeHead, currentDirection)
const newDirection = hamiltonianCycleGrid[snakeHead.row][snakeHead.col];
console.log(newDirection);
setDirection(newDirection);
},
onLengthUpdate: (length) => {
console.log(`Length: ${length}`);
},
onPauseToggle: (isPaused) => {
console.log(`Is paused: ${isPaused}`);
},
onInit: (params) => {
console.log("init!");
console.log(params);
params.startAIGame();
},
onWin: (params) => {
console.log("win!");
//params.startAIGame();
},
onDeath: (params) => {
console.log("dead!");
//params.startAIGame();
},
});