Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
patorjk
GitHub Repository: patorjk/JavaScript-Snake
Path: blob/main/src/js/ai-init.js
163 views
1
const mySnakeBoard = new SNAKE.Board({
2
boardContainer: "game-area",
3
fullScreen: true,
4
premoveOnPause: false,
5
moveSnakeWithAI: ({
6
grid,
7
snakeHead,
8
currentDirection,
9
isFirstGameMove,
10
setDirection,
11
}) => {
12
13
/*
14
Direction:
15
0
16
3 1
17
2
18
*/
19
20
// This is NOT a real hamiltonian cycle. It misses some values, I'm just including this here as an example of
21
// a look-up type table that you could do.
22
const hamiltonianCycleGrid = [
23
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
24
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0],
25
[0, 0, 2, 3, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 0],
26
[0, 0, 2, 0, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0],
27
[0, 0, 2, 0, 2, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0],
28
[0, 0, 3, 0, 3, 3, 3, 3, 0, 3, 0, 3, 0, 3, 0],
29
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
30
]
31
32
console.log(JSON.parse(JSON.stringify(grid)))
33
console.log(snakeHead, currentDirection)
34
35
const newDirection = hamiltonianCycleGrid[snakeHead.row][snakeHead.col];
36
console.log(newDirection);
37
setDirection(newDirection);
38
},
39
onLengthUpdate: (length) => {
40
console.log(`Length: ${length}`);
41
},
42
onPauseToggle: (isPaused) => {
43
console.log(`Is paused: ${isPaused}`);
44
},
45
onInit: (params) => {
46
console.log("init!");
47
console.log(params);
48
params.startAIGame();
49
},
50
onWin: (params) => {
51
console.log("win!");
52
//params.startAIGame();
53
},
54
onDeath: (params) => {
55
console.log("dead!");
56
//params.startAIGame();
57
},
58
});
59
60