29 Commits

Author SHA1 Message Date
trianta 3bbc9701c5 Merge pull request 'core: add Snake using Typescript' (#7) from snake into main
Reviewed-on: Trianta/trianta.dev#7
2024-11-09 01:26:09 -06:00
Trianta 3699ed465b snake: not adding last piece was correct, bool check was not 2024-11-09 01:23:11 -06:00
Trianta b6fe678367 snake: add snake, but ignore when counting length left 2024-11-09 01:20:05 -06:00
Trianta f59e7cf747 snake: don't add the snake itself to path 2024-11-09 01:10:24 -06:00
Trianta 33d5c491c2 snake: move extraneous logging to debug 2024-11-05 21:23:34 -06:00
Trianta de80157f2e snake: revert last commit and minor cleanups 2024-11-05 21:21:16 -06:00
Trianta 86ef363d3b snake: attempt to move path refresh to post-regen 2024-11-05 21:11:46 -06:00
Trianta 727e13a1c9 snake: minor adjustment to trimming logic 2024-11-05 20:59:27 -06:00
Trianta acc1e2b042 snake: add enums for bit states 2024-11-05 20:08:55 -06:00
Trianta 2916b4e127 snake: clear path on reset 2024-11-05 18:26:53 -06:00
Trianta 61f59208a0 snake: rename global variables 2024-11-05 18:17:44 -06:00
Trianta 7f23f4a40c snake: add stashed changes 2024-11-05 17:29:07 -06:00
Trianta 457c7a22fa jenkins: remove old container instead of just stopping if exists 2024-09-17 20:12:37 -05:00
Trianta 6667a2f79b jenkins: remove old container if exists 2024-09-17 20:11:37 -05:00
Trianta de5f4c5eaf docker: fix directories when in container 2024-09-17 20:03:26 -05:00
Trianta 2967663a82 docker: correctly set up image 2024-09-17 19:49:49 -05:00
Trianta 477ce1461e docker: build before deploy 2024-09-17 19:20:32 -05:00
Trianta 3641cfd836 docker: deploy website to nginx dir 2024-09-17 19:19:15 -05:00
Trianta bda79f51a6 jenkins: add name to container on deploy 2024-09-17 19:14:33 -05:00
Trianta 98149cae07 docker: use nginx internal port 2024-09-17 19:09:54 -05:00
Trianta e0c09898ae jenkins: detach container on deploy 2024-09-17 19:03:13 -05:00
Trianta 0e34226b43 docker: use base image with apt 2024-09-17 18:43:39 -05:00
Trianta 4f6fbc4196 docker: use npm for Typescript stuff 2024-09-17 18:40:58 -05:00
trianta b8198afd25 dockerfile: use nginx to host static site 2024-09-17 14:51:59 -05:00
trianta 08645727a8 docker: remove start script 2024-09-17 02:53:37 -05:00
trianta c317e7ab08 npm: add build script 2024-09-17 02:51:23 -05:00
trianta 4f41b247ba jenkins: fix deploy 2024-09-17 02:46:51 -05:00
trianta 10db37262c jenkins: add initial Jenkinsfile 2024-09-17 02:31:19 -05:00
trianta ceeb1f552b docker: add initial Dockerfile 2024-09-17 02:30:55 -05:00
5 changed files with 134 additions and 92 deletions
+23
View File
@@ -0,0 +1,23 @@
FROM nginx:latest
RUN apt-get update && apt-get install -y nodejs npm
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
WORKDIR /usr/share/nginx/html
COPY index.html .
RUN mkdir -p /usr/share/nginx/html/public
COPY src/*.css ./public
RUN cp /app/build/* ./public
EXPOSE 80
Vendored
+23
View File
@@ -0,0 +1,23 @@
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build') {
steps {
script {
docker.build('test.trianta.dev:latest')
}
}
}
stage('Deploy') {
steps {
sh 'docker stop test && docker rm test || exit 0'
sh 'docker run -d -p 3466:80 --name test test.trianta.dev:latest'
}
}
}
}
+3 -3
View File
@@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8"/> <meta charset="utf-8"/>
<title>Trianta</title> <title>Trianta</title>
<link rel="stylesheet" href="src/home.css" /> <link rel="stylesheet" href="public/home.css" />
</head> </head>
<body> <body>
<div id="navigation"> <div id="navigation">
@@ -15,7 +15,7 @@
<div class="card"> <div class="card">
<h2 class="cardTop">Pong</h2> <h2 class="cardTop">Pong</h2>
<canvas width="750" height="585" id="pong"></canvas> <canvas width="750" height="585" id="pong"></canvas>
<script src="build/pong.js"></script> <script src="public/pong.js"></script>
<div id="gameover" hidden="true"> <div id="gameover" hidden="true">
<h1>Game Over</h1> <h1>Game Over</h1>
</div> </div>
@@ -29,7 +29,7 @@
<div class="card"> <div class="card">
<h2 class="cardTop">Snake</h2> <h2 class="cardTop">Snake</h2>
<canvas width="625" height="375" id="snake"></canvas> <canvas width="625" height="375" id="snake"></canvas>
<script src="build/snake.js"></script> <script src="public/snake.js"></script>
<p class="cardStats">Bottom Text<span></span></p> <p class="cardStats">Bottom Text<span></span></p>
<a class="navItem" href="https://lab.trianta.dev/Trianta/trianta.dev/src/branch/main/src/snake.ts">View Code</a> <a class="navItem" href="https://lab.trianta.dev/Trianta/trianta.dev/src/branch/main/src/snake.ts">View Code</a>
</div> </div>
+1
View File
@@ -4,6 +4,7 @@
"description": "My personal website", "description": "My personal website",
"main": "index.html", "main": "index.html",
"scripts": { "scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": { "repository": {
+84 -89
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 {
@@ -68,12 +67,8 @@ class SnakeCore {
this.width = 25; this.width = 25;
this.height = 15; this.height = 15;
this.board = []; this.board = [];
for (let i = 0; i < this.height; i++) { for (let i = 0; i < this.height; i++)
this.board.push(new Array(this.width)); this.board.push(new Array(this.width));
for (let j = 0; j < this.width; j++) {
this.board[i][j] = 0;
}
}
this.body = []; this.body = [];
this.gameover = false; this.gameover = false;
this.foodAte = true; this.foodAte = true;
@@ -86,30 +81,37 @@ class SnakeCore {
} }
reset() { reset() {
snake.gameover = false; console.debug("[TRACE] Reset was triggered");
snake.foodAte = true; this.gameover = false;
this.foodAte = true;
for (let i = 0; i < this.height; i++) { for (let i = 0; i < this.height; i++) {
for (let j = 0; j < this.width; j++) { for (let j = 0; j < this.width; j++) {
this.board[i][j] = 0; this.board[i][j] = 0;
} }
} }
snake.context.fillStyle = "black"; while (this.body.length > 0)
for (let i = 0; i < snake.height; i++) { this.body.pop();
for (let j = 0; j < snake.width; j++) { while (g_snakebot.path.length > 0)
snake.context.fillRect(j * snake.grid, i * snake.grid, snake.grid, snake.grid); g_snakebot.path.pop();
} this.startRandom();
} this.foodRegen();
this.draw();
}
startRandom() {
this.body.push(new Point(Math.floor(Math.random() * this.width), Math.floor(Math.random() * this.height)));
} }
foodRegen() { foodRegen() {
if (!this.foodAte) if (!this.foodAte)
return; return;
console.debug("[TRACE] Food reset was triggered");
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;
} }
} }
@@ -117,46 +119,45 @@ 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(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], BoardState.SNAKE) && (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], BoardState.SNAKE);
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], BoardState.FOOD)) {
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], BoardState.SNAKE);
} 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], BoardState.FOOD);
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], BoardState.SNAKE))
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], BoardState.FOOD))
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);
} }
} }
} }
@@ -172,10 +173,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], BoardState.CHECKED))
continue; continue;
this.pathUntrimmed.push(current); this.pathUntrimmed.push(current);
let locals: Point[] = new Array(4); let locals: Point[] = new Array(4);
@@ -188,21 +189,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 > 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 = g_snake.board[local.y][local.x];
let value = snake.board[local.y][local.x]; if (isBitSet(value, BoardState.FOOD)) {
if (isBitSet(value, 1)) {
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);
} }
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], BoardState.CHECKED);
} }
} }
@@ -211,24 +209,24 @@ class Bot {
let reachedSnake = false; let reachedSnake = false;
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) { let location: Point = this.pathUntrimmed.pop();
let location: Point = new Point; if (reachedSnake)
location.copy(this.pathUntrimmed[this.pathUntrimmed.length - 1]); continue;
if (isBitSet(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; continue;
delta = location.subtract(this.path[this.path.length - 1]);
if ((Math.abs(delta.x) + Math.abs(delta.y)) === 1)
this.path.push(location);
} }
this.pathUntrimmed.pop(); var delta = new Point;
delta = location.subtract(this.path[this.path.length - 1]);
if ((Math.abs(delta.x) + Math.abs(delta.y)) === 1)
this.path.push(location);
} }
} }
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], BoardState.CHECKED);
} }
} }
} }
@@ -241,51 +239,48 @@ class Bot {
4 = down 4 = down
*/ */
nextMove() { nextMove() {
// Get new path to food
if (this.path.length === 0)
this.pathRefresh();
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)
console.log("[ERR] delta.x < 1"); console.log("[ERR] delta.x < 1");
} if (delta.y > 1)
if (delta.y > 1) {
console.log("[ERR] delta.y > 1"); console.log("[ERR] delta.y > 1");
} else if (delta.y < -1) { else if (delta.y < -1)
console.log("[ERR] delta.y < 1"); console.log("[ERR] delta.y < 1");
}
return next; return next;
} }
autoplay() { pathRefresh() {
if (this.path.length > 0)
return;
this.bfs(); this.bfs();
this.trim(); this.trim();
this.unvisit(); this.unvisit();
} }
} }
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
bot.autoplay();
snake.simulate(); // Simulate movement of snake
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);