made new snake and index and liberary improvements

This commit is contained in:
Jürg Hallenbarter
2026-06-01 11:33:23 +02:00
parent 3d21e42d50
commit ee0d819d33
190 changed files with 3553 additions and 65 deletions

View File

@@ -98,21 +98,21 @@ function buttonUpgradeAIPressed(){
break;
case "wallDetection":
AItype = "nearApple";
AItype = "snakeDetection";
UpgradeAIPrice = 12;
break;
case "snakeDetection":
AItype = "nearApple";
UpgradeAIPrice = 25;
break;
case "nearApple":
AItype = "appleBias";
UpgradeAIPrice = 25;
break;
case "appleBias":
AItype = "snakeDetection";
UpgradeAIPrice = 50;
break;
case "snakeDetection":
case "appleBias":
AItype = "smart";
UpgradeAIPrice = 120;
break;
@@ -121,6 +121,11 @@ function buttonUpgradeAIPressed(){
AItype = "survival";
UpgradeAIPrice = 300;
break;
case "survival":
AItype = "perfect";
UpgradeAIPrice = 1000;
break;
}
}
@@ -235,6 +240,18 @@ function runSnake(){
summonApple();
eatenApples++;
}
if (
snake.length >=
(gridColumns * gridRows) - appleAmount
){
if (AItype == "perfect"){
rewardPerfectAI();
}
restartGame();
return;
}
drawSnakeOnGrid();
}
@@ -349,6 +366,10 @@ function snakeAI(){
snakeAIwallDetection();
break;
case "snakeDetection":
snakeAIsnakeDetection();
break;
case "nearApple":
snakeAInearApple();
break;
@@ -357,10 +378,6 @@ function snakeAI(){
snakeAIappleBias();
break;
case "snakeDetection":
snakeAIsnakeDetection();
break;
case "smart":
snakeAIsmart();
break;
@@ -368,6 +385,10 @@ function snakeAI(){
case "survival":
snakeAIsurvival();
break;
case "perfect":
snakeAIperfect();
break;
}
}
@@ -481,6 +502,43 @@ function countFreeNeighbors(x, y){
return count;
}
// got this inspiration by an LLM AI
function getHamiltonianDirection(){
let x = snake[0][0];
let y = snake[0][1];
// snake-like scan pattern
if (y % 2 == 0){
// move right
if (x < gridColumns - 1){
return 0;
}
// move down
if (y < gridRows - 1){
return 1;
}
}else{
// move left
if (x > 0){
return 2;
}
// move down
if (y < gridRows - 1){
return 1;
}
}
// top fallback
return 3;
}
function snakeAIrandom() {
snakeDirection = floor(random(0, 4));
}
@@ -712,4 +770,75 @@ function snakeAIsurvival(){
}
snakeDirection = bestDirection;
}
// instpired by an LLM AI
function snakeAIperfect(){
let apple = getClosestApple();
// try smart movement first
if (apple != null){
let bestDirection = -1;
let bestDistance = Infinity;
for (let i = 0; i < 4; i++) {
if (!isDirectionSafe(i)){
continue;
}
let vec = directionToVector(i);
let newX = snake[0][0] + vec[0];
let newY = snake[0][1] + vec[1];
let distance =
abs(newX - apple[0]) +
abs(newY - apple[1]);
let freeNeighbors =
countFreeNeighbors(newX, newY);
// avoid traps
if (
freeNeighbors <= 1 &&
snake.length < (gridColumns * gridRows) - 5
){
continue;
}
if (distance < bestDistance){
bestDistance = distance;
bestDirection = i;
}
}
if (bestDirection != -1){
snakeDirection = bestDirection;
return;
}
}
// fallback to Hamiltonian path
let direction = getHamiltonianDirection();
if (isDirectionSafe(direction)){
snakeDirection = direction;
return;
}
// fallback survival
snakeAIsurvival();
}
function rewardPerfectAI(){
let reward =
floor(
(gridColumns * gridRows) * 0.75
);
eatenApples += reward;
}