snake: rename global variables
This commit is contained in:
parent
7f23f4a40c
commit
61f59208a0
84
src/snake.ts
84
src/snake.ts
@ -118,44 +118,44 @@ class SnakeCore {
|
|||||||
// Move snake
|
// Move snake
|
||||||
// TODO: Fix teleporting snake
|
// TODO: Fix teleporting snake
|
||||||
let next: Point = new Point;
|
let next: Point = new Point;
|
||||||
next.copy(bot.nextMove());
|
next.copy(g_snakebot.nextMove());
|
||||||
if (next.x < 0 || next.x > snake.width || next.y < 0 || next.y > snake.height) {
|
if (next.x < 0 || next.x > g_snake.width || next.y < 0 || next.y > g_snake.height) {
|
||||||
snake.gameover = true;
|
g_snake.gameover = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (isBitSet(snake.board[next.y][next.x], 0) && (snake.body.length > 1)) {
|
if (isBitSet(g_snake.board[next.y][next.x], 0) && (g_snake.body.length > 1)) {
|
||||||
snake.gameover = true; // Game should end (Snake touching snake)
|
g_snake.gameover = true; // Game should end (Snake touching snake)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
snake.board[next.y][next.x] = bitSet(snake.board[next.y][next.x], 0);
|
g_snake.board[next.y][next.x] = bitSet(g_snake.board[next.y][next.x], 0);
|
||||||
snake.body.push(next);
|
g_snake.body.push(next);
|
||||||
if (!isBitSet(snake.board[next.y][next.x], 1)) {
|
if (!isBitSet(g_snake.board[next.y][next.x], 1)) {
|
||||||
let old: Point = snake.body.shift() as Point;
|
let old: Point = g_snake.body.shift() as Point;
|
||||||
snake.board[old.y][old.x] = bitClear(snake.board[old.y][old.x], 0);
|
g_snake.board[old.y][old.x] = bitClear(g_snake.board[old.y][old.x], 0);
|
||||||
} else {
|
} else {
|
||||||
snake.board[next.y][next.x] = bitClear(snake.board[next.y][next.x], 1);
|
g_snake.board[next.y][next.x] = bitClear(g_snake.board[next.y][next.x], 1);
|
||||||
snake.foodAte = true;
|
g_snake.foodAte = true;
|
||||||
while (bot.path.length > 0)
|
while (g_snakebot.path.length > 0)
|
||||||
bot.path.pop();
|
g_snakebot.path.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw game to canvas
|
// Draw game to canvas
|
||||||
draw() {
|
draw() {
|
||||||
// Clear the screen
|
// 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
|
// Draw game
|
||||||
for (let i = 0; i < snake.height; i++) {
|
for (let i = 0; i < g_snake.height; i++) {
|
||||||
for (let j = 0; j < snake.width; j++) {
|
for (let j = 0; j < g_snake.width; j++) {
|
||||||
if (isBitSet(snake.board[i][j], 0))
|
if (isBitSet(g_snake.board[i][j], 0))
|
||||||
snake.context.fillStyle = "green";
|
g_snake.context.fillStyle = "green";
|
||||||
else if (isBitSet(snake.board[i][j], 1))
|
else if (isBitSet(g_snake.board[i][j], 1))
|
||||||
snake.context.fillStyle = "red";
|
g_snake.context.fillStyle = "red";
|
||||||
else
|
else
|
||||||
snake.context.fillStyle = "black";
|
g_snake.context.fillStyle = "black";
|
||||||
snake.context.fillRect(j * snake.grid, i * snake.grid, snake.grid, snake.grid);
|
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() {
|
bfs() {
|
||||||
var search: Point[] = [snake.head];
|
var search: Point[] = [g_snake.head];
|
||||||
while (search.length !== 0) {
|
while (search.length !== 0) {
|
||||||
let current: Point = search.shift() as Point;
|
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;
|
continue;
|
||||||
this.pathUntrimmed.push(current);
|
this.pathUntrimmed.push(current);
|
||||||
let locals: Point[] = new Array(4);
|
let locals: Point[] = new Array(4);
|
||||||
@ -187,10 +187,10 @@ 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 > 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;
|
continue;
|
||||||
}
|
}
|
||||||
let value = snake.board[local.y][local.x];
|
let value = g_snake.board[local.y][local.x];
|
||||||
if (isBitSet(value, 1)) {
|
if (isBitSet(value, 1)) {
|
||||||
this.pathUntrimmed.push(local);
|
this.pathUntrimmed.push(local);
|
||||||
return;
|
return;
|
||||||
@ -201,7 +201,7 @@ class Bot {
|
|||||||
continue;
|
continue;
|
||||||
search.push(local);
|
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) {
|
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(snake.board[location.y][location.x], 0))
|
if (isBitSet(g_snake.board[location.y][location.x], 0))
|
||||||
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]);
|
||||||
@ -225,9 +225,9 @@ class Bot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
unvisit() {
|
unvisit() {
|
||||||
for (let i = 0; i < snake.height; i++) {
|
for (let i = 0; i < g_snake.height; i++) {
|
||||||
for (let j = 0; j < snake.width; j++) {
|
for (let j = 0; j < g_snake.width; j++) {
|
||||||
snake.board[i][j] = bitClear(snake.board[i][j], 2);
|
g_snake.board[i][j] = bitClear(g_snake.board[i][j], 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -243,7 +243,7 @@ class Bot {
|
|||||||
var next: Point = new Point;
|
var next: Point = new Point;
|
||||||
next.copy(this.path.pop());
|
next.copy(this.path.pop());
|
||||||
var delta = new Point;
|
var delta = new Point;
|
||||||
delta = next.subtract(snake.head);
|
delta = next.subtract(g_snake.head);
|
||||||
if (delta.x > 1) {
|
if (delta.x > 1) {
|
||||||
console.log("[ERR] delta.x > 1");
|
console.log("[ERR] delta.x > 1");
|
||||||
} else if (delta.x < -1) {
|
} else if (delta.x < -1) {
|
||||||
@ -266,25 +266,25 @@ class Bot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const snake: SnakeCore = new SnakeCore(); // Singleton for snake game
|
const g_snake: SnakeCore = new SnakeCore();
|
||||||
const bot: Bot = new Bot(); // Singleton for bot playing
|
const g_snakebot: Bot = new Bot();
|
||||||
|
|
||||||
// game loop
|
// game loop
|
||||||
function snakeloop() {
|
function snakeloop() {
|
||||||
// Reset of needed
|
// Reset of needed
|
||||||
if (snake.gameover)
|
if (g_snake.gameover)
|
||||||
snake.reset();
|
g_snake.reset();
|
||||||
|
|
||||||
// Input
|
// Input
|
||||||
bot.autoplay();
|
g_snakebot.autoplay();
|
||||||
|
|
||||||
snake.simulate();
|
g_snake.simulate();
|
||||||
|
|
||||||
// Regenerate food if needed
|
// Regenerate food if needed
|
||||||
snake.foodRegen();
|
g_snake.foodRegen();
|
||||||
|
|
||||||
snake.draw();
|
g_snake.draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
// start the game
|
// start the game
|
||||||
setInterval(snakeloop, snake.timeout);
|
setInterval(snakeloop, g_snake.timeout);
|
||||||
|
Loading…
Reference in New Issue
Block a user