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 f975b0a045 - Show all commits

View File

@ -27,6 +27,11 @@ class Point {
this.y = y; this.y = y;
} }
copy(other: Point) {
this.x = other.x;
this.y = other.y;
}
add(other: Point) { add(other: Point) {
var result = new Point; var result = new Point;
result.x = this.x + other.x; result.x = this.x + other.x;
@ -46,7 +51,7 @@ class SnakeCore {
canvas: HTMLCanvasElement; canvas: HTMLCanvasElement;
context: CanvasRenderingContext2D; context: CanvasRenderingContext2D;
grid: number; grid: number;
speed: number; timeout: number;
width: number; width: number;
height: number; height: number;
board: number[][]; board: number[][];
@ -59,7 +64,7 @@ class SnakeCore {
this.canvas = document.getElementById('snake') as HTMLCanvasElement; this.canvas = document.getElementById('snake') as HTMLCanvasElement;
this.context = this.canvas.getContext('2d') as CanvasRenderingContext2D; this.context = this.canvas.getContext('2d') as CanvasRenderingContext2D;
this.grid = 25; // size of grid squares this.grid = 25; // size of grid squares
this.speed = 10; // speed in ms this.timeout = 100; // speed in ms
this.width = 25; this.width = 25;
this.height = 15; this.height = 15;
this.board = []; this.board = [];
@ -88,6 +93,12 @@ class SnakeCore {
this.board[i][j] = 0; this.board[i][j] = 0;
} }
} }
snake.context.fillStyle = "black";
for (let i = 0; i < snake.height; i++) {
for (let j = 0; j < snake.width; j++) {
snake.context.fillRect(j * snake.grid, i * snake.grid, snake.grid, snake.grid);
}
}
} }
foodRegen() { foodRegen() {
@ -102,6 +113,53 @@ class SnakeCore {
return; return;
} }
} }
// Simulate game logic
simulate() {
// 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;
return;
}
if (isBitSet(snake.board[next.y][next.x], 0) && (snake.body.length > 1)) {
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);
} 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();
}
}
// Draw game to canvas
draw() {
// Clear the screen
snake.context.clearRect(0, 0, snake.canvas.width, 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";
else
snake.context.fillStyle = "black";
snake.context.fillRect(j * snake.grid, i * snake.grid, snake.grid, snake.grid);
}
}
}
} }
class Bot { class Bot {
@ -120,9 +178,11 @@ class Bot {
if (isBitSet(snake.board[current.y][current.x], 2)) if (isBitSet(snake.board[current.y][current.x], 2))
continue; continue;
this.pathUntrimmed.push(current); this.pathUntrimmed.push(current);
let locals: Point[] = new Array[4]; let locals: Point[] = new Array(4);
for (var i = 0; i < 4; i++) for (var i = 0; i < 4; i++) {
locals[i] = new Point(current.x, current.y); locals[i] = new Point;
locals[i].copy(current);
}
locals[0].y += 1; locals[0].y += 1;
locals[1].x += 1; locals[1].x += 1;
locals[2].y -= 1; locals[2].y -= 1;
@ -152,13 +212,12 @@ class Bot {
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) { if (!reachedSnake) {
let location = this.pathUntrimmed[this.pathUntrimmed.length - 1]; let location: Point = new Point;
location.copy(this.pathUntrimmed[this.pathUntrimmed.length - 1]);
if (isBitSet(snake.board[location.y][location.x], 0)) if (isBitSet(snake.board[location.y][location.x], 0))
reachedSnake = true; reachedSnake = true;
var delta = new Point; var delta = new Point;
let connection = this.path[this.path.length - 1]; delta = location.subtract(this.path[this.path.length - 1]);
delta.x = location.x - connection.x;
delta.y = location.y - connection.y;
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);
} }
@ -182,95 +241,51 @@ class Bot {
4 = down 4 = down
*/ */
nextMove() { nextMove() {
var next: Point = this.path.pop() as Point; var next: Point = new Point;
var head: Point = snake.head; next.copy(this.path.pop());
var delta = new Point(0, 0); var delta = new Point;
delta = next.subtract(head); delta = next.subtract(snake.head);
if (delta.x > 1) { if (delta.x > 1) {
delta.x = 1; console.log("[ERR] delta.x > 1");
console.log("[ERR] delta.x > 1, clamping");
} else if (delta.x < -1) { } else if (delta.x < -1) {
delta.x = -1; console.log("[ERR] delta.x < 1");
console.log("[ERR] delta.x < 1, clamping");
} }
if (delta.y > 1) { if (delta.y > 1) {
delta.y = 1; console.log("[ERR] delta.y > 1");
console.log("[ERR] delta.y > 1, clamping");
} else if (delta.y < -1) { } else if (delta.y < -1) {
delta.y = -1; console.log("[ERR] delta.y < 1");
console.log("[ERR] delta.y < 1, clamping");
} }
return delta; return next;
} }
autoplay() { autoplay() {
if (this.path.length === 0) if (this.path.length === 0) {
this.bfs(); this.bfs();
this.trim(); this.trim();
this.unvisit(); this.unvisit();
}
return this.nextMove(); return this.nextMove();
/*
let delta = this.headPos - this.path.pop();
if (delta.x > 0)
return 0;
if (delta.y > 0)
return 1;
if (delta.x < 0)
return 2;
if (delta.y < 0)
return 4;
return 0; // Safety guard if above doesn't return
*/
} }
} }
const snake: SnakeCore = new SnakeCore(); // Singleton for snake game const snake: SnakeCore = new SnakeCore(); // Singleton for snake game
const bot: Bot = new Bot(); // Singleton for bot playing const bot: Bot = new Bot(); // Singleton for bot playing
// game loop
function snakeloop() { function snakeloop() {
snake.context.clearRect(0, 0, snake.canvas.width, snake.canvas.height);
// Reset of needed // Reset of needed
if (snake.gameover) if (snake.gameover)
snake.reset(); snake.reset();
// Input // Input
bot.autoplay(); bot.autoplay();
// Move snake
// TODO: Fix teleporting snake
let next = bot.nextMove().add(snake.head);
if (next.x < 0 || next.x > snake.width || next.y < 0 || next.y > snake.height)
snake.gameover = true;
if (isBitSet(snake.board[next.y][next.x], 0) && (snake.body.length > 1))
snake.gameover = true; // Game should end (Snake touching snake)
snake.board[next.y][next.x] = bitSet(snake.board[next.y][next.x], 0); snake.simulate();
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);
} 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();
}
// Regenerate food if needed // Regenerate food if needed
snake.foodRegen(); snake.foodRegen();
// Draw game snake.draw();
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";
else
snake.context.fillStyle = "black";
snake.context.fillRect(j * snake.grid, i * snake.grid, snake.grid, snake.grid);
}
}
} }
// start the game // start the game
setInterval(snakeloop, snake.speed); setInterval(snakeloop, snake.timeout);