-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.js
More file actions
164 lines (146 loc) · 4.06 KB
/
snake.js
File metadata and controls
164 lines (146 loc) · 4.06 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
const astar = require('./astar.js');
const Graph = require('./graph.js').Graph;
class Snake {
constructor(game) {
this.game = game;
}
findPath(board, from, to) {
const graph = new Graph(board);
return astar.search(graph.nodes, graph.nodes[from.y][from.x], graph.nodes[to.y][to.x]);
}
getSnakeHead(board) {
let head = {};
board.forEach((row, rowIdx) => {
row.forEach((cell, colIdx) => {
if (cell && cell.player && cell.player === this.game.you && cell.head) {
head = { y: rowIdx, x: colIdx };
}
})
});
return head;
}
getSnakeDirection(board) {
let direction = '';
board.forEach(row => {
row.forEach(cell => {
if (cell && cell.player && cell.player === this.game.you && cell.head) {
direction = cell.head;
}
})
});
return direction;
}
getApple(board) {
let apple = {};
board.forEach((row, rowIdx) => {
row.forEach((cell, colIdx) => {
if (cell === '🍎') {
apple = { y: rowIdx, x: colIdx };
}
})
});
return apple;
}
getBoardArray(board) {
return board.map(row => {
return row.map(cell => {
// empty for apple
if (cell === '🍎') {
return 0;
}
// empty for snake head
if (cell && cell.player && cell.player === this.game.you && cell.head) {
return 0;
}
// empty/wall
return cell ? 1 : 0;
})
});
}
checkAvailable(pos) {
return this.getBoardArray(this.state.board)[pos.y][pos.x] === 0;
}
moveTo(from, to, snakeDirection) {
const [direction] = [
{
direction: 'W',
value: Math.max(0, from.x - to.x)
},
{
direction: 'E',
value: Math.max(0, to.x - from.x)
},
{
direction: 'N',
value: Math.max(0, from.y - to.y)
},
{
direction: 'S',
value: Math.max(0, to.y - from.y)
},
].filter(pos => pos.value)
.sort((alpha, bravo) => bravo.value - alpha.value)
.map(x => x.direction);
switch (`${snakeDirection}, ${direction}`) {
case 'N, E': return 'R';
case 'E, S': return 'R';
case 'S, W': return 'R';
case 'W, N': return 'R';
case 'N, W': return 'L';
case 'W, S': return 'L';
case 'S, E': return 'L';
case 'E, N': return 'L';
// case 'N, S':
// return ["L", "R"][Math.floor(Math.random() * 2)];
// case 'E, W':
// return ["L", "R"][Math.floor(Math.random() * 2)];
// case 'S, N':
// return ["L", "R"][Math.floor(Math.random() * 2)];
// case 'W, E':
// return ["L", "R"][Math.floor(Math.random() * 2)];
case 'N, S':
if (this.checkAvailable({y: from.y - 1, x: from.x})) {
return 'L';
}
if (this.checkAvailable({y: from.y + 1, x: from.x})) {
return 'R';
}
return undefined;
case 'E, W':
if (this.checkAvailable({y: from.y, x: from.x - 1})) {
return 'L';
}
if (this.checkAvailable({y: from.y, x: from.x + 1})) {
return 'R';
}
return undefined;
case 'S, N':
if (this.checkAvailable({y: from.y + 1, x: from.x})) {
return 'L';
}
if (this.checkAvailable({y: from.y - 1, x: from.x})) {
return 'R';
}
return undefined;
case 'W, E':
if (this.checkAvailable({y: from.y, x: from.x + 1})) {
return 'L';
}
if (this.checkAvailable({y: from.y, x: from.x - 1})) {
return 'R';
}
return undefined;
}
}
getMove(state) {
this.state = state;
const boardArray = this.getBoardArray(state.board);
const headPos = this.getSnakeHead(state.board);
const snakeDir = this.getSnakeDirection(state.board);
const applePos = this.getApple(state.board);
const path = this.findPath(boardArray, headPos, applePos);
const nextStep = { y: path[0].x, x: path[0].y };
return this.moveTo(headPos, nextStep, snakeDir);
}
}
module.exports = Snake;