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