forked from BattlesnakeOfficial/starter-snake-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.js
More file actions
73 lines (62 loc) · 2.07 KB
/
Copy pathlogic.js
File metadata and controls
73 lines (62 loc) · 2.07 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
function info() {
console.log("INFO")
const response = {
apiversion: "1",
author: "",
color: "#888888",
head: "default",
tail: "default"
}
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 possibleMoves = {
up: true,
down: true,
left: true,
right: true
}
// Step 0: Don't let your Battlesnake move back on it's own neck
const myHead = gameState.you.head
const myNeck = gameState.you.body[1]
if (myNeck.x < myHead.x) {
possibleMoves.left = false
} else if (myNeck.x > myHead.x) {
possibleMoves.right = false
} else if (myNeck.y < myHead.y) {
possibleMoves.down = false
} else if (myNeck.y > myHead.y) {
possibleMoves.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
// const boardHeight = gameState.board.height
// 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.
// TODO: Step 4 - Find food.
// Use information in gameState to seek out and find food.
// 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(possibleMoves).filter(key => possibleMoves[key])
const response = {
move: safeMoves[Math.floor(Math.random() * safeMoves.length)],
}
console.log(`${gameState.game.id} MOVE ${gameState.turn}: ${response.move}`)
return response
}
module.exports = {
info: info,
start: start,
move: move,
end: end
}