core: add Snake using Typescript #7
45
src/snake.ts
45
src/snake.ts
@ -1,9 +1,8 @@
|
||||
/*
|
||||
bits:
|
||||
0 = snake
|
||||
1 = food
|
||||
2 = checked (for searching algorithms)
|
||||
*/
|
||||
enum BoardState {
|
||||
SNAKE = 0,
|
||||
FOOD = 1,
|
||||
CHECKED = 2, // For search algorithms
|
||||
}
|
||||
|
||||
// Bit functions for ease
|
||||
function isBitSet(value: number, bit: number): boolean {
|
||||
@ -108,9 +107,9 @@ class SnakeCore {
|
||||
this.foodAte = false;
|
||||
while (true) {
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -118,25 +117,24 @@ class SnakeCore {
|
||||
// Simulate game logic
|
||||
simulate() {
|
||||
// Move snake
|
||||
// TODO: Fix teleporting snake
|
||||
let next: Point = new Point;
|
||||
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(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)
|
||||
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);
|
||||
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;
|
||||
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 {
|
||||
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;
|
||||
while (g_snakebot.path.length > 0)
|
||||
g_snakebot.path.pop();
|
||||
@ -151,9 +149,9 @@ class SnakeCore {
|
||||
// Draw game
|
||||
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))
|
||||
if (isBitSet(g_snake.board[i][j], BoardState.SNAKE))
|
||||
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";
|
||||
else
|
||||
g_snake.context.fillStyle = "black";
|
||||
@ -189,21 +187,18 @@ class Bot {
|
||||
locals[2].y -= 1;
|
||||
locals[3].x -= 1;
|
||||
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;
|
||||
}
|
||||
let value = g_snake.board[local.y][local.x];
|
||||
if (isBitSet(value, 1)) {
|
||||
if (isBitSet(value, BoardState.FOOD)) {
|
||||
this.pathUntrimmed.push(local);
|
||||
return;
|
||||
}
|
||||
if (isBitSet(value, 2))
|
||||
continue;
|
||||
if (isBitSet(value, 0))
|
||||
if (isBitSet(value, BoardState.CHECKED) || isBitSet(value, BoardState.SNAKE))
|
||||
continue;
|
||||
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) {
|
||||
let location: Point = new Point;
|
||||
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;
|
||||
var delta = new Point;
|
||||
delta = location.subtract(this.path[this.path.length - 1]);
|
||||
@ -229,7 +224,7 @@ class Bot {
|
||||
unvisit() {
|
||||
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);
|
||||
g_snake.board[i][j] = bitClear(g_snake.board[i][j], BoardState.CHECKED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user