Sixty neural-net cars evolve to race a track, then face one you draw.
Each car sees through seven range sensors plus its speedometer, and a tiny network (8 inputs, 10 tanh units) outputs steering and throttle. A genetic algorithm scores each car by how far it gets in 20 seconds, keeps the best six and breeds the rest by tournament selection, uniform crossover and Gaussian mutation, turning mutation up when progress stalls. Evolution runs headlessly several generations a second while the latest generation is replayed as sixty rank-colored ghosts, with the champion's sensor rays and live network on top and every replay's trails piling into a long exposure. The track is a centerline in a distance field, so a sensor ray is a few sphere-tracing steps, a crash is one lookup, and any loop you draw becomes a valid track instantly.
Try it. Draw a closed loop to build your own track: the current population is tested on it first (its zero-shot score shows whether the champion generalized or overfit) and then keeps evolving there. N makes a new random circuit, R restarts evolution from scratch and F speeds up the replay.
Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.
Build a top-down racing game where cars teach themselves to drive by neuroevolution, with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.
Start simple:
- Make a closed track: pick about 12 points around an ellipse at random radii, smooth them into a loop with a Catmull-Rom spline, and give the road a fixed width. Draw it as a thick dark stroke with bright edges and a start line.
- A car has a position, heading and speed. Each step it casts five rays (ahead, plus or minus 35 and 80 degrees) and measures how far each goes before leaving the road, meaning more than half the road width from the centerline.
- The driver is a tiny neural network: the five ray lengths and the speed go in, 8 tanh hidden units, two outputs for steering and throttle. Its weights are one flat array.
- Run 50 cars at once from the start line. A car stops when it leaves the road or makes no progress for a couple of seconds. Its fitness is how far along the track it got.
- When every car has stopped, keep the best five unchanged and fill the rest of the next generation with mutated copies (add small Gaussian noise to some weights) of cars picked from the top half.
- Draw the cars, highlight the best one, and show the generation.
Once that works, make it beautiful:
- Draw the best car's rays and color them by distance.
- Leave fading trails so you can see the racing line form across generations.
- Run several generations per frame without drawing them, and replay only the latest one.
- Let me draw my own loop with the mouse and see whether the champion can drive a track it has never seen.
Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as a distance field for much faster ray casting, crossover between parents, or training on several tracks at once to stop overfitting.