From 727e13a1c9c147bc90ed8502cf6379410a571d4a Mon Sep 17 00:00:00 2001 From: Trianta <56975502+Trimutex@users.noreply.github.com> Date: Tue, 5 Nov 2024 20:59:27 -0600 Subject: [PATCH] snake: minor adjustment to trimming logic --- src/snake.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/snake.ts b/src/snake.ts index 2dff6b0..841b846 100644 --- a/src/snake.ts +++ b/src/snake.ts @@ -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); } }