This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- - - - - - - - - - - - - - - | |
|0,0| | | | | |7,0| | |
- - - - - - - - - - - - - - - | |
| | | | | | | | | |
- - - - - - - - - - - - - - - | |
| | | | | | | | | |
- - - - - - - - - - - - - - - | |
| | | | | | | | | |
- - - - - - - - - - - - - - - | |
| | | | | | | | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function move(state) { | |
console.log(state); | |
return { move: 'right' } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"game": { | |
"id": "game-id-string" | |
}, | |
"turn": 1, | |
"board": { | |
"height": 11, | |
"width": 11, | |
"food": [{ | |
"x": 1, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function move(state) { | |
const head = state.you.body[0]; | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function moveAsCoord(move, head) { | |
switch (move) { | |
case 'up': | |
return {x: head.x, y: head.y-1}; | |
case 'down': | |
return {x: head.x, y: head.y+1}; | |
case 'left': | |
return {x: head.x-1, y: head.y}; | |
case 'right': | |
return {x: head.x+1, y: head.y}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function move(state) { | |
const head = state.you.body[0]; | |
const moves = ['up', 'down', 'left', 'right']; | |
for (const move of moves) { | |
const coord = moveAsCoord(move, head); | |
// We now know where this is going to be! | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function offBoard(state, coord) { | |
if (coord.x < 0) return true; | |
if (coord.y < 0) return true; | |
if (coord.y >= state.board.height) return true; | |
if (coord.x >= state.board.height) return true; | |
return false; // If it makes it here we are ok. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function move(state) { | |
const head = state.you.body[0]; | |
const moves = ['up', 'down', 'left', 'right']; | |
for (const move of moves) { | |
const coord = moveAsCoord(move, head); | |
if (!offBoard(state, coord)) { | |
return {move: move}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function coordEqual(a, b) { | |
return a.x === b.x && a.y === b.y | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function move(state) { | |
const head = state.you.body[0]; | |
const neck = state.you.body[1]; | |
const moves = ['up', 'down', 'left', 'right']; | |
for (const move of moves) { | |
const coord = moveAsCoord(move, head); | |
if (!offBoard(state, coord) && !coordEqual(coord, neck)) { | |
return {move: move}; | |
} |
OlderNewer