Skip to content

Instantly share code, notes, and snippets.

@josepdecid
Last active December 9, 2025 08:41
Show Gist options
  • Select an option

  • Save josepdecid/cabc7c54c8a8b23153c76c0ca4d77f0a to your computer and use it in GitHub Desktop.

Select an option

Save josepdecid/cabc7c54c8a8b23153c76c0ca4d77f0a to your computer and use it in GitHub Desktop.
Snake Minigame Auto-Player for adventjs.dev
// adventjs.dev Snake Minigame Auto-Player
// Dummy technique to zig-zag and eventually cover the whole grid
// without overengineering it to avoid self-collisions :)
//
// TLDR: copy the whole script and paste it into the browser console
// It will Auto-play when pasting the full script.
// Stop playing by either refresing the screen or with:
// > clearInterval(autoPlayInterval);
const GRID_COLS = 50;
const GRID_ROWS = 25;
const ARROW_UP = 'ArrowUp';
const ARROW_DOWN = 'ArrowDown';
const ARROW_LEFT = 'ArrowLeft';
const ARROW_RIGHT = 'ArrowRight';
const SNAKE_HEAD = { r: 120, g: 160, b: 70 };
const canvas = document.querySelectorAll('canvas')[0];
const ctx = canvas.getContext('2d');
function getCellMeanColor(imageData, cellX, cellY, cellWidth, cellHeight) {
let totalR = 0, totalG = 0, totalB = 0;
let pixelCount = 0;
const startX = Math.floor(cellX * cellWidth);
const startY = Math.floor(cellY * cellHeight);
const endX = Math.floor((cellX + 1) * cellWidth);
const endY = Math.floor((cellY + 1) * cellHeight);
for (let y = startY; y < endY; y++) {
for (let x = startX; x < endX; x++) {
const index = (y * canvas.width + x) * 4;
totalR += imageData.data[index];
totalG += imageData.data[index + 1];
totalB += imageData.data[index + 2];
pixelCount++;
}
}
return {
r: totalR / pixelCount,
g: totalG / pixelCount,
b: totalB / pixelCount
};
}
function colorMatches(color, target, tolerance = 10) {
return (
Math.abs(color.r - target.r) <= tolerance &&
Math.abs(color.g - target.g) <= tolerance &&
Math.abs(color.b - target.b) <= tolerance
);
}
function detectSnakeHeadPosition() {
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const cellWidth = canvas.width / GRID_COLS;
const cellHeight = canvas.height / GRID_ROWS;
for (let row = 0; row < GRID_ROWS; row++) {
for (let col = 0; col < GRID_COLS; col++) {
const color = getCellMeanColor(imageData, col, row, cellWidth, cellHeight);
if (colorMatches(color, SNAKE_HEAD)) {
return { row, col };
}
}
}
}
function getNextMove({ row, col }) {
// Return row
if (row === 0) {
if (col === 0) return ARROW_DOWN;
return ARROW_LEFT;
}
// Zig-zag pattern even columns
if (col % 2 === 0) {
if (row === GRID_ROWS - 1) return ARROW_RIGHT;
return ARROW_DOWN;
}
// Zig-zag pattern odd columns
if (row === 1 && col !== GRID_COLS - 1) return ARROW_RIGHT;
return ARROW_UP;
}
const autoPlayInterval = setInterval(() => {
const snakeHead = detectSnakeHeadPosition();
const direction = getNextMove(snakeHead);
const event = new KeyboardEvent('keydown', { key: direction, code: direction, bubbles: true });
document.dispatchEvent(event);
}, 50);
@josepdecid
Copy link
Author

Easy Win

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment