AI Snake
Snake plays itself — pick a strategy below and watch how differently each one handles the exact same problem.
Snake plays itself — pick a strategy below and watch how differently each one handles the exact same problem.
There are four selectable strategies here, in roughly the order they were actually built this session while chasing a single goal: make the snake stop trapping itself. Switching strategies restarts the run and resets the death counter, so you can watch the same problem handled four different ways.
"Greedy" always takes the shortest path to the apple with zero regard for its own body — it dies as soon as that path happens to cross where the tail will be. "Greedy + tail safety" adds a one-step lookahead (simulate the move, check a path to the tail still exists afterwards) plus a fallback that follows its own tail through open space. "Cycle + shortcuts" follows a fixed loop through every cell by default but jumps straight for the apple whenever that same one-step check allows it.
Only "cycle (pure)" is actually provable: it always moves to the next cell in one fixed loop that visits the whole board exactly once, so its body is guaranteed to stay one contiguous, non-crossing arc - there's no lookahead to fool. The other three rely on that one-step safety check, which is inherently short-sighted: a move can look completely safe and still turn out to have sealed off a pocket a few steps later. There's a second, quieter failure mode too - a strategy can end up permanently "safe" by orbiting a loop that never actually gets any closer to food, which looks fine at a glance since it never crashes, so catching it needed watching whether the score is still climbing rather than just whether a path to the food exists in theory. Stress-testing all four with both checks in place confirmed the ranking: greedy dies constantly, cycle-shortcuts and greedy-safe both occasionally fail (either by trapping themselves or by looping forever without progress), and pure cycle-following has never failed once across hundreds of thousands of simulated moves.
The trade-off is speed: pure cycle-following never beelines for the apple, so growth depends on however long the loop takes to sweep past it. It's still by far the fastest strategy to compute (a single array lookup, no search at all) - it's simply not the fastest at finding food.