core: add Snake using Typescript #7

Merged
Trianta merged 35 commits from snake into main 2024-11-09 01:26:09 -06:00
Showing only changes of commit acc1e2b042 - Show all commits

View File

@ -1,9 +1,8 @@
/* enum BoardState {
bits: SNAKE = 0,
0 = snake FOOD = 1,
1 = food CHECKED = 2, // For search algorithms
2 = checked (for searching algorithms) }
*/
// Bit functions for ease // Bit functions for ease
function isBitSet(value: number, bit: number): boolean { function isBitSet(value: number, bit: number): boolean {
@ -108,9 +107,9 @@ class SnakeCore {
this.foodAte = false; this.foodAte = false;
while (true) { while (true) {
let tmp = new Point(Math.floor(Math.random() * this.width), Math.floor(Math.random() * this.height)); let tmp = new Point(Math.floor(Math.random() * this.width), Math.floor(Math.random() * this.height));
if (isBitSet(this.board[tmp.y][tmp.x], 0)) if (isBitSet(this.board[tmp.y][tmp.x], BoardState.SNAKE))
continue; continue;
this.board[tmp.y][tmp.x] = bitSet(this.board[tmp.y][tmp.x], 1); this.board[tmp.y][tmp.x] = bitSet(this.board[tmp.y][tmp.x], BoardState.FOOD);
return; return;
} }
} }
@ -118,25 +117,24 @@ class SnakeCore {
// Simulate game logic // Simulate game logic
simulate() { simulate() {
// Move snake // Move snake
// TODO: Fix teleporting snake
let next: Point = new Point; let next: Point = new Point;
next.copy(g_snakebot.nextMove()); next.copy(g_snakebot.nextMove());
if (next.x < 0 || next.x > g_snake.width || next.y < 0 || next.y > g_snake.height) { if (next.x < 0 || next.x > g_snake.width || next.y < 0 || next.y > g_snake.height) {
g_snake.gameover = true; g_snake.gameover = true;
return; return;
} }
if (isBitSet(g_snake.board[next.y][next.x], 0) && (g_snake.body.length > 1)) { if (isBitSet(g_snake.board[next.y][next.x], BoardState.SNAKE) && (g_snake.body.length > 1)) {
g_snake.gameover = true; // Game should end (Snake touching snake) g_snake.gameover = true; // Game should end (Snake touching snake)
return; return;
} }
g_snake.board[next.y][next.x] = bitSet(g_snake.board[next.y][next.x], 0); g_snake.board[next.y][next.x] = bitSet(g_snake.board[next.y][next.x], BoardState.SNAKE);
g_snake.body.push(next); g_snake.body.push(next);
if (!isBitSet(g_snake.board[next.y][next.x], 1)) { if (!isBitSet(g_snake.board[next.y][next.x], BoardState.FOOD)) {
let old: Point = g_snake.body.shift() as Point; 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); g_snake.board[old.y][old.x] = bitClear(g_snake.board[old.y][old.x], BoardState.SNAKE);
} else { } else {
g_snake.board[next.y][next.x] = bitClear(g_snake.board[next.y][next.x], 1); g_snake.board[next.y][next.x] = bitClear(g_snake.board[next.y][next.x], BoardState.FOOD);
g_snake.foodAte = true; g_snake.foodAte = true;
while (g_snakebot.path.length > 0) while (g_snakebot.path.length > 0)
g_snakebot.path.pop(); g_snakebot.path.pop();
@ -151,9 +149,9 @@ class SnakeCore {
// Draw game // Draw game
for (let i = 0; i < g_snake.height; i++) { for (let i = 0; i < g_snake.height; i++) {
for (let j = 0; j < g_snake.width; j++) { for (let j = 0; j < g_snake.width; j++) {
if (isBitSet(g_snake.board[i][j], 0)) if (isBitSet(g_snake.board[i][j], BoardState.SNAKE))
g_snake.context.fillStyle = "green"; g_snake.context.fillStyle = "green";
else if (isBitSet(g_snake.board[i][j], 1)) else if (isBitSet(g_snake.board[i][j], BoardState.FOOD))
g_snake.context.fillStyle = "red"; g_snake.context.fillStyle = "red";
else else
g_snake.context.fillStyle = "black"; g_snake.context.fillStyle = "black";
@ -189,21 +187,18 @@ class Bot {
locals[2].y -= 1; locals[2].y -= 1;
locals[3].x -= 1; locals[3].x -= 1;
for (const local of locals) { for (const local of locals) {
if (local.x < 0 || local.x > g_snake.width - 1 || local.y < 0 || local.y > g_snake.height - 1) { if (local.x < 0 || local.x > g_snake.width - 1 || local.y < 0 || local.y > g_snake.height - 1)
continue; continue;
}
let value = g_snake.board[local.y][local.x]; let value = g_snake.board[local.y][local.x];
if (isBitSet(value, 1)) { if (isBitSet(value, BoardState.FOOD)) {
this.pathUntrimmed.push(local); this.pathUntrimmed.push(local);
return; return;
} }
if (isBitSet(value, 2)) if (isBitSet(value, BoardState.CHECKED) || isBitSet(value, BoardState.SNAKE))
continue;
if (isBitSet(value, 0))
continue; continue;
search.push(local); search.push(local);
} }
g_snake.board[current.y][current.x] = bitSet(g_snake.board[current.y][current.x], 2); g_snake.board[current.y][current.x] = bitSet(g_snake.board[current.y][current.x], BoardState.CHECKED);
} }
} }
@ -215,7 +210,7 @@ class Bot {
if (!reachedSnake) { if (!reachedSnake) {
let location: Point = new Point; let location: Point = new Point;
location.copy(this.pathUntrimmed[this.pathUntrimmed.length - 1]); location.copy(this.pathUntrimmed[this.pathUntrimmed.length - 1]);
if (isBitSet(g_snake.board[location.y][location.x], 0)) if (isBitSet(g_snake.board[location.y][location.x], BoardState.SNAKE))
reachedSnake = true; reachedSnake = true;
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]);
@ -229,7 +224,7 @@ class Bot {
unvisit() { unvisit() {
for (let i = 0; i < g_snake.height; i++) { for (let i = 0; i < g_snake.height; i++) {
for (let j = 0; j < g_snake.width; j++) { for (let j = 0; j < g_snake.width; j++) {
g_snake.board[i][j] = bitClear(g_snake.board[i][j], 2); g_snake.board[i][j] = bitClear(g_snake.board[i][j], BoardState.CHECKED);
} }
} }
} }