Compare commits
29 Commits
8e626448a2
...
v0.2.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 3bbc9701c5 | |||
| 3699ed465b | |||
| b6fe678367 | |||
| f59e7cf747 | |||
| 33d5c491c2 | |||
| de80157f2e | |||
| 86ef363d3b | |||
| 727e13a1c9 | |||
| acc1e2b042 | |||
| 2916b4e127 | |||
| 61f59208a0 | |||
| 7f23f4a40c | |||
| 457c7a22fa | |||
| 6667a2f79b | |||
| de5f4c5eaf | |||
| 2967663a82 | |||
| 477ce1461e | |||
| 3641cfd836 | |||
| bda79f51a6 | |||
| 98149cae07 | |||
| e0c09898ae | |||
| 0e34226b43 | |||
| 4f6fbc4196 | |||
| b8198afd25 | |||
| 08645727a8 | |||
| c317e7ab08 | |||
| 4f41b247ba | |||
| 10db37262c | |||
| ceeb1f552b |
+23
@@ -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
@@ -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
@@ -3,7 +3,7 @@
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>Trianta</title>
|
||||
<link rel="stylesheet" href="src/home.css" />
|
||||
<link rel="stylesheet" href="public/home.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="navigation">
|
||||
@@ -15,7 +15,7 @@
|
||||
<div class="card">
|
||||
<h2 class="cardTop">Pong</h2>
|
||||
<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">
|
||||
<h1>Game Over</h1>
|
||||
</div>
|
||||
@@ -29,7 +29,7 @@
|
||||
<div class="card">
|
||||
<h2 class="cardTop">Snake</h2>
|
||||
<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>
|
||||
<a class="navItem" href="https://lab.trianta.dev/Trianta/trianta.dev/src/branch/main/src/snake.ts">View Code</a>
|
||||
</div>
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
"description": "My personal website",
|
||||
"main": "index.html",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
|
||||
+84
-89
@@ -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 {
|
||||
@@ -68,12 +67,8 @@ class SnakeCore {
|
||||
this.width = 25;
|
||||
this.height = 15;
|
||||
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));
|
||||
for (let j = 0; j < this.width; j++) {
|
||||
this.board[i][j] = 0;
|
||||
}
|
||||
}
|
||||
this.body = [];
|
||||
this.gameover = false;
|
||||
this.foodAte = true;
|
||||
@@ -86,30 +81,37 @@ class SnakeCore {
|
||||
}
|
||||
|
||||
reset() {
|
||||
snake.gameover = false;
|
||||
snake.foodAte = true;
|
||||
console.debug("[TRACE] Reset was triggered");
|
||||
this.gameover = false;
|
||||
this.foodAte = true;
|
||||
for (let i = 0; i < this.height; i++) {
|
||||
for (let j = 0; j < this.width; j++) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
while (this.body.length > 0)
|
||||
this.body.pop();
|
||||
while (g_snakebot.path.length > 0)
|
||||
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() {
|
||||
if (!this.foodAte)
|
||||
return;
|
||||
console.debug("[TRACE] Food reset was triggered");
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -117,46 +119,45 @@ class SnakeCore {
|
||||
// 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;
|
||||
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(snake.board[next.y][next.x], 0) && (snake.body.length > 1)) {
|
||||
snake.gameover = true; // Game should end (Snake touching snake)
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
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], 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], BoardState.SNAKE);
|
||||
} 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();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
// Draw game to canvas
|
||||
draw() {
|
||||
// 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
|
||||
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";
|
||||
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], BoardState.SNAKE))
|
||||
g_snake.context.fillStyle = "green";
|
||||
else if (isBitSet(g_snake.board[i][j], BoardState.FOOD))
|
||||
g_snake.context.fillStyle = "red";
|
||||
else
|
||||
snake.context.fillStyle = "black";
|
||||
snake.context.fillRect(j * snake.grid, i * snake.grid, snake.grid, snake.grid);
|
||||
g_snake.context.fillStyle = "black";
|
||||
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() {
|
||||
var search: Point[] = [snake.head];
|
||||
var search: Point[] = [g_snake.head];
|
||||
while (search.length !== 0) {
|
||||
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;
|
||||
this.pathUntrimmed.push(current);
|
||||
let locals: Point[] = new Array(4);
|
||||
@@ -188,21 +189,18 @@ class Bot {
|
||||
locals[2].y -= 1;
|
||||
locals[3].x -= 1;
|
||||
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;
|
||||
}
|
||||
let value = snake.board[local.y][local.x];
|
||||
if (isBitSet(value, 1)) {
|
||||
let value = g_snake.board[local.y][local.x];
|
||||
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);
|
||||
}
|
||||
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;
|
||||
this.path.push(this.pathUntrimmed.pop() as Point); // Push food location
|
||||
while (this.pathUntrimmed.length !== 0) {
|
||||
if (!reachedSnake) {
|
||||
let location: Point = new Point;
|
||||
location.copy(this.pathUntrimmed[this.pathUntrimmed.length - 1]);
|
||||
if (isBitSet(snake.board[location.y][location.x], 0))
|
||||
reachedSnake = true;
|
||||
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);
|
||||
let location: Point = this.pathUntrimmed.pop();
|
||||
if (reachedSnake)
|
||||
continue;
|
||||
if (isBitSet(g_snake.board[location.y][location.x], BoardState.SNAKE)) {
|
||||
reachedSnake = true;
|
||||
continue;
|
||||
}
|
||||
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() {
|
||||
for (let i = 0; i < snake.height; i++) {
|
||||
for (let j = 0; j < snake.width; j++) {
|
||||
snake.board[i][j] = bitClear(snake.board[i][j], 2);
|
||||
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], BoardState.CHECKED);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -241,51 +239,48 @@ class Bot {
|
||||
4 = down
|
||||
*/
|
||||
nextMove() {
|
||||
// Get new path to food
|
||||
if (this.path.length === 0)
|
||||
this.pathRefresh();
|
||||
var next: Point = new Point;
|
||||
next.copy(this.path.pop());
|
||||
var delta = new Point;
|
||||
delta = next.subtract(snake.head);
|
||||
if (delta.x > 1) {
|
||||
delta = next.subtract(g_snake.head);
|
||||
if (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");
|
||||
}
|
||||
if (delta.y > 1) {
|
||||
if (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");
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
autoplay() {
|
||||
if (this.path.length > 0)
|
||||
return;
|
||||
pathRefresh() {
|
||||
this.bfs();
|
||||
this.trim();
|
||||
this.unvisit();
|
||||
}
|
||||
}
|
||||
|
||||
const snake: SnakeCore = new SnakeCore(); // Singleton for snake game
|
||||
const bot: Bot = new Bot(); // Singleton for bot playing
|
||||
const g_snake: SnakeCore = new SnakeCore();
|
||||
const g_snakebot: Bot = new Bot();
|
||||
|
||||
// game loop
|
||||
function snakeloop() {
|
||||
// Reset of needed
|
||||
if (snake.gameover)
|
||||
snake.reset();
|
||||
|
||||
// Input
|
||||
bot.autoplay();
|
||||
if (g_snake.gameover)
|
||||
g_snake.reset();
|
||||
|
||||
snake.simulate();
|
||||
// Simulate movement of snake
|
||||
g_snake.simulate();
|
||||
|
||||
// Regenerate food if needed
|
||||
snake.foodRegen();
|
||||
g_snake.foodRegen();
|
||||
|
||||
snake.draw();
|
||||
g_snake.draw();
|
||||
}
|
||||
|
||||
// start the game
|
||||
setInterval(snakeloop, snake.timeout);
|
||||
setInterval(snakeloop, g_snake.timeout);
|
||||
|
||||
Reference in New Issue
Block a user