I have been programming a Battlesnake (https://play.battlesnake.com/) and I need to re-declare my move commands (like move="left"
and move="right"
for example) command but every time it goes over the code it uses the move command once and won’t use it again. It also only uses the first one it can and won’t use any other ones above it. Loops won’t work because to my understanding the Battlesnake servers call the handleMove
function each turn and if the Battlesnake servers don’t get a response within 500 milliseconds the game will move for you. The code starts at the top of the handleMove
and ends at the response.status
thing. I know the code goes through the functions because I tracked it with console.log
. The code goes through everything as intended, because I set console.log
on all of the functions and inside all the if commands, and it runs through the move strings inside the if commands, but ignores all move commands it reaches after the code gets to response.status
. That is where I am confused. A lot of the data is hosted on the Battlesnake servers, so if something is not declared in my code it probably came from the Battlesnake servers.
How can I fix this?
const express = require('express')
const PORT = process.env.PORT || 3000
const app = express()
app.use(bodyParser.json())
app.get('/', handleIndex)
app.post('/start', handleStart)
app.post('/move', handleMove)
app.post('/end', handleEnd)
app.listen(PORT, () => console.log(`Battlesnake Server listening at http://127.0.0.1:${PORT}`))
function handleIndex(request, response) {
var battlesnakeInfo = {
apiversion: '1',
author: '',
color: '#1c607d',
head: 'caffeine',
tail: 'leaf'
}
response.status(200).json(battlesnakeInfo)
}
function handleStart(request, response) {
var gameData = request.body
console.log('START')
response.status(200).send('ok')
}
function handleMove(request, response) {
var gameData = request.body
var head = gameData.you.head
var boardWidth = gameData.board.width
var boardHeight = gameData.board.height
var food = gameData.board.food[0]
// code i wrote
function moveUpB() {
if (head.x < 1) {
move = "up"
//refuses to execute at all??
}
}
function moveRightB() {
if (head.y > (boardHeight - 2)) {
move = "right"
//only used once
}
}
function moveDownB() {
if (head.x > (boardWidth - 2)) {
move = "down"
//only used once
}
}
function moveLeftB() {
if (head.y < 1) {
move = "left"
//only used once
}
}
moveUpB()
moveRightB()
moveDownB()
moveLeftB()
//aafter here it should go back to moveUpB but it doesent?
//rest of the code needed for the snake to move
console.log('MOVE: ' + move)
response.status(200).send({
move: move
})
}
function handleEnd(request, response) {
var gameData = request.body
console.log('END')
response.status(200).send('ok')
}