snake: minor adjustment to trimming logic

This commit is contained in:
Trianta 2024-11-05 20:59:27 -06:00
parent acc1e2b042
commit 727e13a1c9

View File

@ -1,7 +1,7 @@
enum BoardState {
SNAKE = 0,
FOOD = 1,
CHECKED = 2, // For search algorithms
CHECKED = 2 // For search algorithms
}
// Bit functions for ease
@ -174,7 +174,7 @@ class Bot {
var search: Point[] = [g_snake.head];
while (search.length !== 0) {
let current: Point = search.shift() as Point;
if (isBitSet(g_snake.board[current.y][current.x], 2))
if (isBitSet(g_snake.board[current.y][current.x], BoardState.CHECKED))
continue;
this.pathUntrimmed.push(current);
let locals: Point[] = new Array(4);
@ -207,17 +207,18 @@ class Bot {
let reachedSnake = false;
this.path.push(this.pathUntrimmed.pop() as Point); // Push food location
while (this.pathUntrimmed.length !== 0) {
if (!reachedSnake) {
let location: Point = new Point;
location.copy(this.pathUntrimmed[this.pathUntrimmed.length - 1]);
if (isBitSet(g_snake.board[location.y][location.x], BoardState.SNAKE))
reachedSnake = true;
var delta = new Point;
delta = location.subtract(this.path[this.path.length - 1]);
if ((Math.abs(delta.x) + Math.abs(delta.y)) === 1)
this.path.push(location);
let location: Point = this.pathUntrimmed.pop();
if (reachedSnake)
continue;
if (isBitSet(g_snake.board[location.y][location.x], BoardState.SNAKE)) {
reachedSnake = true;
this.path.push(location);
continue;
}
this.pathUntrimmed.pop();
var delta = new Point;
delta = location.subtract(this.path[this.path.length - 1]);
if ((Math.abs(delta.x) + Math.abs(delta.y)) === 1)
this.path.push(location);
}
}