Phone AI (no libraries) · Part 5 · HTML + JavaScript
My phone AI taught ITSELF to drivePRO
Part 5: 40 cars, each with its own tiny brain and 5 eyes. Nobody shows them the road: the 5 that get furthest survive each round and the rest are mutated copies. Watch generation 1 crash at the first bend and a later one drive whole laps.
<meta name="viewport" content="width=device-width, user-scalable=no">
<style>body { margin: 0; background: #0b0f17; }</style>
<canvas id="c"></canvas>
<script>
// a fixed seed: the same evolution every time you run it (change 7 to watch a different one)
let sd = 7;
Math.random = () => (sd = (sd * 16807) % 2147483647) / 2147483647;
const c = document.getElementById("c");
const g = c.getContext("2d");
const S = innerWidth, D = Math.min(devicePixelRatio, 1.5);
c.width = S * D;
c.height = S * 1.2 * D; // the track on top, the scoreboard under it
c.style.width = S + "px";
g.scale(D, D);
// the track: a wobbly loop, 0.2 wide. R(a) = how far the road is from the middle at angle a
+ 113 more lines in the app (Pro)
Open code AI5 in Pocket Compiler
Download the Pocket Compiler app — free
No app yet? Install it, then open the app and type the code AI5 in Code Library.