-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
165 lines (150 loc) · 4.94 KB
/
logic.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
function info() {
console.log("INFO")
const response = {
apiversion: "1",
author: "",
color: "#C2B280",
head: "fang",
tail: "flame"
}
return response
}
function start(gameState) {
console.log(`${gameState.game.id} START`)
}
function end(gameState) {
console.log(`${gameState.game.id} END\n`)
}
function move(gameState) {
let validMoves = {
up: true,
down: true,
left: true,
right: true
}
let moveWeights = {
up: 0,
down: 0,
left: 0,
right: 0
}
// Step 0: Don't let your Battlesnake move back on its own neck
const myHead = gameState.you.head;
const myNeck = gameState.you.body[1];
if (myNeck.x < myHead.x) {
validMoves.left = false
} else if (myNeck.x > myHead.x) {
validMoves.right = false
} else if (myNeck.y < myHead.y) {
validMoves.down = false
} else if (myNeck.y > myHead.y) {
validMoves.up = false
}
// TODO: Step 1 - Don't hit walls.
// Use information in gameState to prevent your Battlesnake from moving beyond the boundaries of the board.
const boardWidth = gameState.board.width - 1;
const boardHeight = gameState.board.height - 1;
if (myHead.x == 0) {
validMoves.left = false
}
if (myHead.x == boardWidth) {
validMoves.right = false
}
if (myHead.y == 0) {
validMoves.down = false
}
if (myHead.y == boardHeight) {
validMoves.up = false
}
// TODO: Step 2 - Don't hit yourself.
// Use information in gameState to prevent your Battlesnake from colliding with itself.
// const mybody = gameState.you.body
// TODO: Step 3 - Don't collide with others.
// Use information in gameState to prevent your Battlesnake from colliding with others.
const heads = [];
const tails = [];
const bodies = [];
for(const snake of gameState.board.snakes) {
if(snake.id !== gameState.you.id)
heads.push([snake.head, snake.length])
tails.push(snake.body.pop())
bodies.push(...snake.body)
}
if(bodies.some(body => body.x == myHead.x - 1 && body.y == myHead.y)) {
validMoves.left = false
}
if (bodies.some(body => body.x == myHead.x + 1 && body.y == myHead.y)) {
validMoves.right = false
}
if (bodies.some(body => body.x == myHead.x && body.y == myHead.y - 1)) {
validMoves.down = false
}
if (bodies.some(body => body.x == myHead.x && body.y == myHead.y + 1)) {
validMoves.up = false
}
if(tails.some(tail => tail.x == myHead.x - 1 && tail.y == myHead.y)) {
moveWeights.left -= 100
}
if (tails.some(tail => tail.x == myHead.x + 1 && tail.y == myHead.y)) {
moveWeights.right -= 100
}
if (tails.some(tail => tail.x == myHead.x && tail.y == myHead.y - 1)) {
moveWeights.down -= 100
}
if (tails.some(tail => tail.x == myHead.x && tail.y == myHead.y + 1)) {
moveWeights.up -= 100
}
for(const snake of heads) {
const head = snake[0];
const length = snake[1];
if((head.x == myHead.x && head.y == myHead.y + 2) ||
(head.x == myHead.x + 1 && head.y == myHead.y + 1) ||
(head.x == myHead.x - 1 && head.y == myHead.y + 1)) {
moveWeights.up += gameState.you.length > length ? 20 : -500
}
if((head.x == myHead.x && head.y == myHead.y - 2) ||
(head.x == myHead.x + 1 && head.y == myHead.y - 1) ||
(head.x == myHead.x - 1 && head.y == myHead.y - 1)) {
moveWeights.down += gameState.you.length > length ? 20 : -500
}
if((head.x == myHead.x + 2 && head.y == myHead.y) ||
(head.x == myHead.x + 1 && head.y == myHead.y + 1) ||
(head.x == myHead.x + 1 && head.y == myHead.y - 1)) {
moveWeights.right += gameState.you.length > length ? 20 : -500
}
if((head.x == myHead.x - 2 && head.y == myHead.y) ||
(head.x == myHead.x - 1 && head.y == myHead.y + 1) ||
(head.x == myHead.x - 1 && head.y == myHead.y - 1)) {
moveWeights.left += gameState.you.length > length ? 20 : -500
}
}
// TODO: Step 4 - Find food.
// Use information in gameState to seek out and find food.
for(const food of gameState.board.food) {
if(food.x < myHead.x) {
moveWeights.left++;
} else if(food.x > myHead.x) {
moveWeights.right++;
} else if(food.y < myHead.y) {
moveWeights.down++;
} else if(food.y > myHead.y) {
moveWeights.up++;
}
}
// Finally, choose a move from the available safe moves.
// TODO: Step 5 - Select a move to make based on strategy, rather than random.
const safeMoves = Object.keys(validMoves).filter(key => validMoves[key])
moveWeights = Object.fromEntries(Object.entries(moveWeights).filter(entry => validMoves[entry[0]]))
const response = {
move: safeMoves.find(key => moveWeights[key] == Math.max(...Object.values(moveWeights))),
}
//console.log(`${myHead.x}/${boardWidth} ${myHead.y}/${boardHeight} ${safeMoves}`)
console.log(`${new Date().toISOString().slice(11, -1)} ${gameState.game.id} MOVE ${gameState.turn}: ${response.move}`)
return response
}
module.exports = {
info: info,
start: start,
move: move,
end: end
}