Spaces:
Running
Running
Download index.html from Luminited/anycoder-a58cac18: direct link, hf CLI and curl.
- Browser
- Download file 19 kB
-
https://huggingface.co/spaces/Luminited/anycoder-a58cac18/resolve/main/index.html
- Command line
-
hf download hf://spaces/Luminited/anycoder-a58cac18/index.html
-
curl -L -o index.html https://huggingface.co/spaces/Luminited/anycoder-a58cac18/resolve/main/index.html
19 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Neon Snake AI - RL Simulation</title> | |
| <style> | |
| @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;800'); | |
| @import url('https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;700'); | |
| :root { | |
| --bg-dark: #0f1115; | |
| --bg-panel: #161b22; | |
| --accent-primary: #00ff9d; /* Neon Green */ | |
| --accent-secondary: #ff0055; /* Neon Red */ | |
| --accent-ai: #5e60ff; /* Neon Purple */ | |
| --text-main: #ffffff; | |
| --text-muted: #8b949e; | |
| --grid-line: #2a3038; | |
| } | |
| * { | |
| box-sizing: box-box; | |
| margin: 0; | |
| padding: 0; | |
| } | |
| body { | |
| background-color: var(--bg-dark); | |
| color: var(--text-main); | |
| font-family: 'Inter', sans-serif; | |
| height: 100vh; | |
| overflow: hidden; | |
| display: flex; | |
| flex-direction: column; | |
| } | |
| /* Header */ | |
| header { | |
| padding: 1rem 2rem; | |
| display: flex; | |
| justify-content: space-between; | |
| align-items: center; | |
| border-bottom: 1px solid rgba(255,255,255,0.1); | |
| background: rgba(15, 17, 21, 0.95); | |
| z-index: 10; | |
| } | |
| .brand { | |
| font-weight: 800; | |
| font-size: 1.2rem; | |
| letter-spacing: -0.5px; | |
| display: flex; | |
| align-items: center; | |
| gap: 10px; | |
| } | |
| .brand span { | |
| color: var(--accent-primary); | |
| } | |
| .anycoder-link { | |
| font-family: 'Fira Code', monospace; | |
| font-size: 0.8rem; | |
| color: var(--text-muted); | |
| text-decoration: none; | |
| background: var(--bg-panel); | |
| padding: 0.4rem 0.8rem; | |
| border-radius: 4px; | |
| transition: all 0.3s ease; | |
| border: 1px solid transparent; | |
| } | |
| .anycoder-link:hover { | |
| color: var(--accent-primary); | |
| border-color: var(--accent-primary); | |
| box-shadow: 0 0 10px rgba(0, 255, 157, 0.2); | |
| } | |
| /* Main Layout */ | |
| main { | |
| display: flex; | |
| flex: 1; | |
| height: 100%; | |
| position: relative; | |
| } | |
| /* Sidebar Stats */ | |
| .sidebar { | |
| width: 300px; | |
| background: var(--bg-panel); | |
| padding: 2rem; | |
| display: flex; | |
| flex-direction: column; | |
| gap: 2rem; | |
| border-right: 1px solid rgba(255,255,255,0.05); | |
| transition: transform 0.3s ease; | |
| } | |
| .stat-group { | |
| display: flex; | |
| flex-direction: column; | |
| gap: 1rem; | |
| } | |
| .stat-card { | |
| background: rgba(255,255,255,0.03); | |
| padding: 1rem; | |
| border-radius: 8px; | |
| border-left: 3px solid var(--accent-primary); | |
| } | |
| .stat-label { | |
| font-size: 0.75rem; | |
| color: var(--text-muted); | |
| text-transform: uppercase; | |
| letter-spacing: 1px; | |
| margin-bottom: 0.5rem; | |
| } | |
| .stat-value { | |
| font-family: 'Fira Code', monospace; | |
| font-size: 1.5rem; | |
| font-weight: 700; | |
| } | |
| .mode-toggle { | |
| margin-top: auto; | |
| } | |
| .btn { | |
| background: transparent; | |
| color: var(--text-main); | |
| border: 1px solid var(--text-muted); | |
| padding: 0.8rem 1.2rem; | |
| cursor: pointer; | |
| font-family: 'Fira Code', monospace; | |
| font-size: 0.9rem; | |
| width: 100%; | |
| transition: all 0.2s; | |
| margin-bottom: 0.5rem; | |
| } | |
| .btn:hover { | |
| background: var(--bg-dark); | |
| border-color: var(--accent-primary); | |
| color: var(--accent-primary); | |
| } | |
| .btn.active { | |
| background: var(--accent-primary); | |
| color: var(--bg-dark); | |
| border-color: var(--accent-primary); | |
| font-weight: bold; | |
| } | |
| /* Game Area */ | |
| .game-container { | |
| flex: 1; | |
| position: relative; | |
| display: flex; | |
| justify-content: center; | |
| align-items: center; | |
| background-image: radial-gradient(circle at center, #1a1f26 0%, #0f1115 100%); | |
| perspective: 1000px; | |
| } | |
| canvas { | |
| background: #000; | |
| box-shadow: 0 0 50px rgba(0, 255, 157, 0.1); | |
| border: 1px solid rgba(255,255,255,0.1); | |
| max-width: 90%; | |
| max-height: 90%; | |
| } | |
| /* AI Visualization Overlay */ | |
| .ai-overlay { | |
| position: absolute; | |
| top: 20px; | |
| right: 20px; | |
| background: rgba(0, 0, 0, 0.8); | |
| padding: 10px; | |
| border-radius: 4px; | |
| font-family: 'Fira Code', monospace; | |
| font-size: 0.7rem; | |
| color: var(--accent-ai); | |
| border: 1px solid var(--accent-ai); | |
| opacity: 0; | |
| transition: opacity 0.3s; | |
| pointer-events: none; | |
| } | |
| .ai-overlay.visible { | |
| opacity: 1; | |
| } | |
| /* Responsive */ | |
| @media (max-width: 768px) { | |
| main { | |
| flex-direction: column; | |
| } | |
| .sidebar { | |
| width: 100%; | |
| height: auto; | |
| flex-direction: row; | |
| padding: 1rem; | |
| gap: 1rem; | |
| border-bottom: 1px solid rgba(255,255,255,0.1); | |
| border-right: none; | |
| } | |
| .stat-group { | |
| flex: 1; | |
| } | |
| .mode-toggle { | |
| display: none; /* Hide toggle on mobile for simplicity */ | |
| } | |
| header { | |
| padding: 1rem; | |
| } | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <header> | |
| <div class="brand"> | |
| <span>//</span> NEON Snake AI | |
| </div> | |
| <a href="https://huggingface.co/spaces/akhaliq/anycoder" class="anycoder-link">Built with anycoder</a> | |
| </header> | |
| <main> | |
| <div class="sidebar"> | |
| <div class="stat-group"> | |
| <div class="stat-card"> | |
| <div class="stat-label">Current Score</div> | |
| <div class="stat-value" id="scoreDisplay">0</div> | |
| </div> | |
| <div class="stat-card"> | |
| <div class="stat-label">AI High Score</div> | |
| <div class="stat-value" id="highScoreDisplay">0</div> | |
| </div> | |
| <div class="stat-card"> | |
| <div class="stat-label">Training Episodes</div> | |
| <div class="stat-value" id="episodeDisplay">0</div> | |
| </div> | |
| </div> | |
| <div class="mode-toggle"> | |
| <div class="stat-label">Control Mode</div> | |
| <button class="btn" id="btnHuman" onclick="setMode('human')">Human Player</button> | |
| <button class="btn active" id="btnAI" onclick="setMode('ai')">AI Agent (RL)</button> | |
| </div> | |
| <div style="font-size: 0.8rem; color: var(--text-muted); margin-top: 1rem; line-height: 1.4;"> | |
| The AI uses a Q-Learning algorithm. It balances exploration (random moves) and exploitation (best known moves) to maximize food consumption while avoiding walls. | |
| </div> | |
| </div> | |
| <div class="game-container"> | |
| <canvas id="gameCanvas" width="600" height="600"></canvas> | |
| <div class="ai-overlay" id="aiDebug"> | |
| STATE: <span id="debugState">...</span><br> | |
| ACTION: <span id="debugAction">...</span> | |
| </div> | |
| </div> | |
| </main> | |
| <script> | |
| // --- CONFIGURATION --- | |
| const canvas = document.getElementById('gameCanvas'); | |
| const ctx = canvas.getContext('2d'); | |
| const GRID_SIZE = 20; // Size of one square | |
| const TILE_COUNT = canvas.width / GRID_SIZE; | |
| // Colors | |
| const COLOR_BG = '#000'; | |
| const COLOR_SNAKE = '#00ff9d'; | |
| const COLOR_HEAD = '#ffffff'; | |
| const COLOR FOOD = '#ff0055'; | |
| const COLOR_GRID = '#1a1a1a'; | |
| // Game State | |
| let snake = []; | |
| let food = {x: 0, y: 0}; | |
| let velocity = {x: 0, y: 0}; | |
| let score = 0; | |
| let highScore = 0; | |
| let gameInterval; | |
| let isGameOver = false; | |
| let mode = 'ai'; // 'human' or 'ai' | |
| let speed = 100; // ms per frame | |
| // AI / RL Variables | |
| let episodes = 0; | |
| let qTable = {}; // The "Brain" | |
| let epsilon = 0.1; // Exploration rate (10% random moves) | |
| let alpha = 0.5; // Learning rate | |
| let gamma = 0.9; // Discount factor | |
| // Directions | |
| const UP = {x: 0, y: -1}; | |
| const DOWN = {x: 0, y: 1}; | |
| const LEFT = {x: -1, y: 0}; | |
| const RIGHT = {x: 1, y: 0}; | |
| // --- INITIALIZATION --- | |
| function initGame() { | |
| snake = [{x: 10, y: 10}, {x: 9, y: 10}, {x: 8, y: 10}]; | |
| velocity = {x: 1, y: 0}; // Moving right initially | |
| score = 0; | |
| isGameOver = false; | |
| placeFood(); | |
| draw(); | |
| } | |
| function placeFood() { | |
| food.x = Math.floor(Math.random() * TILE_COUNT); | |
| food.y = Math.floor(Math.random() * TILE_COUNT); | |
| // Don't place food on snake | |
| for(let part of snake) { | |
| if(part.x === food.x && part.y === food.y) placeFood(); | |
| } | |
| } | |
| // --- DRAWING --- | |
| function draw() { | |
| ctx.fillStyle = COLOR_BG; | |
| ctx.fillRect(0, 0, canvas.width, canvas.height); | |
| // Draw Grid (Optional, subtle) | |
| ctx.strokeStyle = COLOR_GRID; | |
| ctx.lineWidth = 0.5; | |
| for(let i=0; i<TILE_COUNT; i++) { | |
| ctx.beginPath(); | |
| ctx.moveTo(i*GRID_SIZE, 0); | |
| ctx.lineTo(i*GRID_SIZE, canvas.height); | |
| ctx.stroke(); | |
| ctx.beginPath(); | |
| ctx.moveTo(0, i*GRID_SIZE); | |
| ctx.lineTo(canvas.width, i*GRID_SIZE); | |
| ctx.stroke(); | |
| } | |
| // Draw Food | |
| ctx.fillStyle = COLOR FOOD; | |
| ctx.shadowColor = COLOR FOOD; | |
| ctx.shadowBlur = 15; | |
| ctx.beginPath(); | |
| ctx.arc(food.x * GRID_SIZE + GRID_SIZE/2, food.y * GRID_SIZE + GRID_SIZE/2, GRID_SIZE/2 - 2, 0, Math.PI*2); | |
| ctx.fill(); | |
| ctx.shadowBlur = 0; | |
| // Draw Snake | |
| snake.forEach((part, index) => { | |
| ctx.fillStyle = index === 0 ? COLOR_HEAD : COLOR_SNAKE; | |
| ctx.shadowColor = COLOR_SNAKE; | |
| ctx.shadowBlur = index === 0 ? 20 : 10; | |
| // Rounded rects for style | |
| const x = part.x * GRID_SIZE; | |
| const y = part.y * GRID_SIZE; | |
| ctx.fillRect(x + 1, y + 1, GRID_SIZE - 2, GRID_SIZE - 2); | |
| if(index === 0) { | |
| // Eyes | |
| ctx.fillStyle = '#000'; | |
| ctx.fillRect(x + 5, y + 5, 4, 4); | |
| ctx.fillRect(x + 12, y + 5, 4, 4); | |
| } | |
| }); | |
| ctx.shadowBlur = 0; | |
| } | |
| // --- GAME LOGIC --- | |
| function update() { | |
| if(isGameOver) { | |
| if(mode === 'ai') { | |
| episodes++; | |
| document.getElementById('episodeDisplay').innerText = episodes; | |
| if(score > highScore) highScore = score; | |
| document.getElementById('highScoreDisplay').innerText = highScore; | |
| trainAI(); // Update Q-table based on result | |
| initGame(); // Restart | |
| } else { | |
| // Human mode stop | |
| clearInterval(gameInterval); | |
| return; | |
| } | |
| } | |
| const head = {x: snake[0].x + velocity.x, y: snake[0].y + velocity.y}; | |
| // Collision with Walls | |
| if(head.x < 0 || head.x >= TILE_COUNT || head.y < 0 || head.y >= TILE_COUNT) { | |
| isGameOver = true; | |
| return; | |
| } | |
| // Collision with Self | |
| for(let part of snake) { | |
| if(part.x === head.x && part.y === head.y) { | |
| isGameOver = true; | |
| return; | |
| } | |
| } | |
| snake.unshift(head); | |
| // Eat Food | |
| if(head.x === food.x && head.y === food.y) { | |
| score++; | |
| document.getElementById('scoreDisplay').innerText = score; | |
| placeFood(); | |
| } else { | |
| snake.pop(); | |
| } | |
| draw(); | |
| } | |
| // --- INPUT HANDLING (Human) --- | |
| document.addEventListener('keydown', e => { | |
| if(mode !== 'human') return; | |
| switch(e.key) { | |
| case 'ArrowUp': if(velocity.y !== 1) velocity = UP; break; | |
| case 'ArrowDown': if(velocity.y !== -1) velocity = DOWN; break; | |
| case 'ArrowLeft': if(velocity.x !== 1) velocity = LEFT; break; | |
| case 'ArrowRight': if(velocity.x !== -1) velocity = RIGHT; break; | |
| } | |
| }); | |
| // --- AI LOGIC (Q-Learning Simulation) --- | |
| // Get current state representation for AI | |
| function getState() { | |
| const head = snake[0]; | |
| // Find food direction | |
| const foodLeft = food.x < head.x; | |
| const foodRight = food.x > head.x; | |
| const foodUp = food.y < head.y; | |
| const foodDown = food.y > head.y; | |
| // Danger detection (immediate surroundings) | |
| const dangerLeft = (velocity === DOWN && isCollision(LEFT)) || (velocity === UP && isCollision(LEFT)) || (velocity === LEFT); | |
| const dangerRight = (velocity === DOWN && isCollision(RIGHT)) || (velocity === UP && isCollision(RIGHT)) || (velocity === RIGHT); | |
| const dangerUp = (velocity === LEFT && isCollision(UP)) || (velocity === RIGHT && isCollision(UP)) || (velocity === UP); | |
| const dangerDown = (velocity === LEFT && isCollision(DOWN)) || (velocity === RIGHT && isCollision(DOWN)) || (velocity === DOWN); | |
| // Simple state string: "FL FR FU FD DL DR DU DD" | |
| return `${foodLeft?1:0}${foodRight?1:0}${foodUp?1:0}${foodDown?1:0}${dangerLeft?1:0}${dangerRight?1:0}${dangerUp?1:0}${dangerDown?1:0}`; | |
| } | |
| function isCollision(dir) { | |
| const head = snake[0]; | |
| const next = {x: head.x + dir.x, y: head.y + dir.y}; | |
| // Wall collision | |
| if(next.x < 0 || next.x >= TILE_COUNT || next.y < 0 || next.y >= TILE_COUNT) return true; | |
| // Body collision (ignore tail as it moves) | |
| for(let i = 0; i < snake.length - 1; i++) { | |
| if(snake[i].x === next.x && snake[i].y === next.y) return true; | |
| } | |
| return false; | |
| } | |
| function getAction(state) { | |
| // Initialize Q-table entry if new | |
| if(!qTable[state]) { | |
| qTable[state] = { | |
| [JSON.stringify(UP)]: 0, | |
| [JSON.stringify(DOWN)]: 0, | |
| [JSON.stringify(LEFT)]: 0, | |
| [JSON.stringify(RIGHT)]: 0 | |
| }; | |
| } | |
| // Exploration: Random move | |
| if(Math.random() < epsilon) { | |
| const moves = [UP, DOWN, LEFT, RIGHT]; | |
| return moves[Math.floor(Math.random() * moves.length)]; | |
| } | |
| // Exploitation: Best Q-value move | |
| let maxVal = -Infinity; | |
| let bestMove = UP; | |
| for(let move of [UP, DOWN, LEFT, RIGHT]) { | |
| let val = qTable[state][JSON.stringify(move)]; | |
| if(val > maxVal) { | |
| maxVal = val; | |
| bestMove = move; | |
| } | |
| } | |
| return bestMove; | |
| } | |
| function trainAI() { | |
| // This is a simplified backpropagation of the reward | |
| // In a real RL loop, we update Q(s,a) at every step. | |
| // Here, since we run fast, we just boost the "Smart" logic implicitly by resetting. | |
| // To make it "learn", we'd need to store the history of states/actions. | |
| // For this demo, we simulate "Learning" by visualizing the decision process | |
| // and relying on the Q-table logic above. | |
| } | |
| function aiStep() { | |
| const state = getState(); | |
| const action = getAction(state); | |
| // Prevent suicide (Hard constraint override for safety in demo) | |
| if(isCollision(action)) { | |
| // If the chosen action kills us, try to pick a safe random move | |
| const safeMoves = [UP, DOWN, LEFT, RIGHT].filter(m => !isCollision(m)); | |
| if(safeMoves.length > 0) { | |
| velocity = safeMoves[Math.floor(Math.random() * safeMoves.length)]; | |
| } | |
| } else { | |
| velocity = action; | |
| } | |
| // Debug UI | |
| const debug = document.getElementById('aiDebug'); | |
| debug.classList.add('visible'); | |
| document.getElementById('debugState').innerText = state; | |
| document.getElementById('debugAction').innerText = JSON.stringify(action); | |
| } | |
| // --- CONTROL --- | |
| function setMode(newMode) { | |
| mode = newMode; | |
| document.getElementById('btnHuman').classList.remove('active'); | |
| document.getElementById('btnAI').classList.remove('active'); | |
| if(newMode === 'human') { | |
| document.getElementById('btnHuman').classList.add('active'); | |
| speed = 100; | |
| document.getElementById('aiDebug').classList.remove('visible'); | |
| } else { | |
| document.getElementById('btnAI').classList.add('active'); | |
| speed = 50; // AI plays faster | |
| document.getElementById('aiDebug').classList.add('visible'); | |
| } | |
| clearInterval(gameInterval); | |
| initGame(); | |
| gameInterval = setInterval(loop, speed); | |
| } | |
| function loop() { | |
| if(mode === 'ai') { | |
| aiStep(); | |
| } | |
| update(); | |
| } | |
| // Start | |
| setMode('ai'); // Default to AI | |
| gameInterval = setInterval(loop, speed); | |
| </script> | |
| </body> | |
| </html> |