From 61f59208a07fccc512e64eb894f19dae2fc7eb4c Mon Sep 17 00:00:00 2001 From: Trianta <56975502+Trimutex@users.noreply.github.com> Date: Tue, 5 Nov 2024 18:17:44 -0600 Subject: [PATCH] snake: rename global variables --- src/snake.ts | 84 ++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/src/snake.ts b/src/snake.ts index 39055e0..0cd4283 100644 --- a/src/snake.ts +++ b/src/snake.ts @@ -118,44 +118,44 @@ class SnakeCore { // Move snake // TODO: Fix teleporting snake let next: Point = new Point; - next.copy(bot.nextMove()); - if (next.x < 0 || next.x > snake.width || next.y < 0 || next.y > snake.height) { - snake.gameover = true; + next.copy(g_snakebot.nextMove()); + if (next.x < 0 || next.x > g_snake.width || next.y < 0 || next.y > g_snake.height) { + g_snake.gameover = true; return; } - if (isBitSet(snake.board[next.y][next.x], 0) && (snake.body.length > 1)) { - snake.gameover = true; // Game should end (Snake touching snake) + if (isBitSet(g_snake.board[next.y][next.x], 0) && (g_snake.body.length > 1)) { + g_snake.gameover = true; // Game should end (Snake touching snake) return; } - snake.board[next.y][next.x] = bitSet(snake.board[next.y][next.x], 0); - snake.body.push(next); - if (!isBitSet(snake.board[next.y][next.x], 1)) { - let old: Point = snake.body.shift() as Point; - snake.board[old.y][old.x] = bitClear(snake.board[old.y][old.x], 0); + g_snake.board[next.y][next.x] = bitSet(g_snake.board[next.y][next.x], 0); + g_snake.body.push(next); + if (!isBitSet(g_snake.board[next.y][next.x], 1)) { + let old: Point = g_snake.body.shift() as Point; + g_snake.board[old.y][old.x] = bitClear(g_snake.board[old.y][old.x], 0); } else { - snake.board[next.y][next.x] = bitClear(snake.board[next.y][next.x], 1); - snake.foodAte = true; - while (bot.path.length > 0) - bot.path.pop(); + g_snake.board[next.y][next.x] = bitClear(g_snake.board[next.y][next.x], 1); + g_snake.foodAte = true; + while (g_snakebot.path.length > 0) + g_snakebot.path.pop(); } } // Draw game to canvas draw() { // Clear the screen - snake.context.clearRect(0, 0, snake.canvas.width, snake.canvas.height); + g_snake.context.clearRect(0, 0, g_snake.canvas.width, g_snake.canvas.height); // Draw game - for (let i = 0; i < snake.height; i++) { - for (let j = 0; j < snake.width; j++) { - if (isBitSet(snake.board[i][j], 0)) - snake.context.fillStyle = "green"; - else if (isBitSet(snake.board[i][j], 1)) - snake.context.fillStyle = "red"; + for (let i = 0; i < g_snake.height; i++) { + for (let j = 0; j < g_snake.width; j++) { + if (isBitSet(g_snake.board[i][j], 0)) + g_snake.context.fillStyle = "green"; + else if (isBitSet(g_snake.board[i][j], 1)) + g_snake.context.fillStyle = "red"; else - snake.context.fillStyle = "black"; - snake.context.fillRect(j * snake.grid, i * snake.grid, snake.grid, snake.grid); + g_snake.context.fillStyle = "black"; + g_snake.context.fillRect(j * g_snake.grid, i * g_snake.grid, g_snake.grid, g_snake.grid); } } } @@ -171,10 +171,10 @@ class Bot { } bfs() { - var search: Point[] = [snake.head]; + var search: Point[] = [g_snake.head]; while (search.length !== 0) { let current: Point = search.shift() as Point; - if (isBitSet(snake.board[current.y][current.x], 2)) + if (isBitSet(g_snake.board[current.y][current.x], 2)) continue; this.pathUntrimmed.push(current); let locals: Point[] = new Array(4); @@ -187,10 +187,10 @@ class Bot { locals[2].y -= 1; locals[3].x -= 1; for (const local of locals) { - if (local.x < 0 || local.x > snake.width - 1 || local.y < 0 || local.y > snake.height - 1) { + if (local.x < 0 || local.x > g_snake.width - 1 || local.y < 0 || local.y > g_snake.height - 1) { continue; } - let value = snake.board[local.y][local.x]; + let value = g_snake.board[local.y][local.x]; if (isBitSet(value, 1)) { this.pathUntrimmed.push(local); return; @@ -201,7 +201,7 @@ class Bot { continue; search.push(local); } - snake.board[current.y][current.x] = bitSet(snake.board[current.y][current.x], 2); + g_snake.board[current.y][current.x] = bitSet(g_snake.board[current.y][current.x], 2); } } @@ -213,7 +213,7 @@ class Bot { if (!reachedSnake) { let location: Point = new Point; location.copy(this.pathUntrimmed[this.pathUntrimmed.length - 1]); - if (isBitSet(snake.board[location.y][location.x], 0)) + if (isBitSet(g_snake.board[location.y][location.x], 0)) reachedSnake = true; var delta = new Point; delta = location.subtract(this.path[this.path.length - 1]); @@ -225,9 +225,9 @@ class Bot { } unvisit() { - for (let i = 0; i < snake.height; i++) { - for (let j = 0; j < snake.width; j++) { - snake.board[i][j] = bitClear(snake.board[i][j], 2); + for (let i = 0; i < g_snake.height; i++) { + for (let j = 0; j < g_snake.width; j++) { + g_snake.board[i][j] = bitClear(g_snake.board[i][j], 2); } } } @@ -243,7 +243,7 @@ class Bot { var next: Point = new Point; next.copy(this.path.pop()); var delta = new Point; - delta = next.subtract(snake.head); + delta = next.subtract(g_snake.head); if (delta.x > 1) { console.log("[ERR] delta.x > 1"); } else if (delta.x < -1) { @@ -266,25 +266,25 @@ class Bot { } } -const snake: SnakeCore = new SnakeCore(); // Singleton for snake game -const bot: Bot = new Bot(); // Singleton for bot playing +const g_snake: SnakeCore = new SnakeCore(); +const g_snakebot: Bot = new Bot(); // game loop function snakeloop() { // Reset of needed - if (snake.gameover) - snake.reset(); + if (g_snake.gameover) + g_snake.reset(); // Input - bot.autoplay(); + g_snakebot.autoplay(); - snake.simulate(); + g_snake.simulate(); // Regenerate food if needed - snake.foodRegen(); + g_snake.foodRegen(); - snake.draw(); + g_snake.draw(); } // start the game -setInterval(snakeloop, snake.timeout); +setInterval(snakeloop, g_snake.timeout);