forked from BattlesnakeOfficial/starter-snake-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (81 loc) · 3.22 KB
/
index.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
// Welcome to
// __________ __ __ .__ __
// \______ \_____ _/ |__/ |_| | ____ ______ ____ _____ | | __ ____
// | | _/\__ \\ __\ __\ | _/ __ \ / ___// \\__ \ | |/ // __ \
// | | \ / __ \| | | | | |_\ ___/ \___ \| | \/ __ \| <\ ___/
// |________/(______/__| |__| |____/\_____>______>___|__(______/__|__\\_____>
//
// This file can be a nice home for your Battlesnake logic and helper functions.
//
// To get you started we've included code to prevent your Battlesnake from moving backwards.
// For more info see docs.battlesnake.com
import runServer from './server.js';
// info is called when you create your Battlesnake on play.battlesnake.com
// and controls your Battlesnake's appearance
// TIP: If you open your Battlesnake URL in a browser you should see this data
function info() {
console.log("INFO");
return {
apiversion: "1",
author: "", // TODO: Your Battlesnake Username
color: "#888888", // TODO: Choose color
head: "default", // TODO: Choose head
tail: "default", // TODO: Choose tail
};
}
// start is called when your Battlesnake begins a game
function start(gameState) {
console.log("GAME START");
}
// end is called when your Battlesnake finishes a game
function end(gameState) {
console.log("GAME OVER\n");
}
// move is called on every turn and returns your next move
// Valid moves are "up", "down", "left", or "right"
// See https://docs.battlesnake.com/api/example-move for available data
function move(gameState) {
let isMoveSafe = {
up: true,
down: true,
left: true,
right: true
};
// We've included code to prevent your Battlesnake from moving backwards
const myHead = gameState.you.body[0];
const myNeck = gameState.you.body[1];
if (myNeck.x < myHead.x) { // Neck is left of head, don't move left
isMoveSafe.left = false;
} else if (myNeck.x > myHead.x) { // Neck is right of head, don't move right
isMoveSafe.right = false;
} else if (myNeck.y < myHead.y) { // Neck is below head, don't move down
isMoveSafe.down = false;
} else if (myNeck.y > myHead.y) { // Neck is above head, don't move up
isMoveSafe.up = false;
}
// TODO: Step 1 - Prevent your Battlesnake from moving out of bounds
// boardWidth = gameState.board.width;
// boardHeight = gameState.board.height;
// TODO: Step 2 - Prevent your Battlesnake from colliding with itself
// myBody = gameState.you.body;
// TODO: Step 3 - Prevent your Battlesnake from colliding with other Battlesnakes
// opponents = gameState.board.snakes;
// Are there any safe moves left?
const safeMoves = Object.keys(isMoveSafe).filter(key => isMoveSafe[key]);
if (safeMoves.length == 0) {
console.log(`MOVE ${gameState.turn}: No safe moves detected! Moving down`);
return { move: "down" };
}
// Choose a random move from the safe moves
const nextMove = safeMoves[Math.floor(Math.random() * safeMoves.length)];
// TODO: Step 4 - Move towards food instead of random, to regain health and survive longer
// food = gameState.board.food;
console.log(`MOVE ${gameState.turn}: ${nextMove}`)
return { move: nextMove };
}
runServer({
info: info,
start: start,
move: move,
end: end
});