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>
|
<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>
|
||||||
|
|||||||
@@ -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
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user