Build a complete Snake game in Python (like the old Nokia ga...
Prompt
Build a complete Snake game in Python (like the old Nokia game) where the real objective is a configurable AI that learns to play by itself. Do not ask me questions. Where something is unspecified, choose sensibly and state your assumptions in the final message. Show the game in a html-window. GAME - Discrete 2D grid seen from above, default 20x20, size configurable (also non-square). - The snake collects red-circle fruits. Each fruit gives 1 point and grows the snake by 1. - Game over on hitting a wall or the snake's own body. Filling the board is a win. - Playable with the arrow keys. pygame (or pygame-ce) for the window. - Starts in a menu: new game, high scores, settings, quit. - A config file (config.json) and a high-score file are created next to the game, with defaults if missing. A broken config file must not crash the game: keep a backup and fall back to defaults. - Must run on Windows, macOS and Linux, with all file paths relative to the game folder. MODES (chosen from the menu when starting a new game) 1. Play myself, with the AI watching and learning from my moves (supervised learning from demonstrations). 2. Watch the AI play while it learns. 3. Watch the AI play without learning (greedy, uses the saved model). 4. Train AI fast: no drawing, as many moves per second as possible, with a live statistics screen: moves per second, games played, average score over the last 50 and 200 games, best score, games per minute, how games ended (hit wall / hit itself / starved), a score histogram, a progress curve, and the exploration level, loss and memory fill. Keys: P pause, S save, Esc stop, save and return to the menu. Autosave every minute. The session best is added to the high scores. - Tab switches to a network view: weight heatmaps per layer (blue negative, orange positive; show mean absolute weight and drift from session start) and a bar chart of what the AI relies on (replace one input at a time by its average and show how often the chosen move changes; per input branch, board channel and sensor group). The weight view must be computed only while visible and at most once per finished game, spaced so it costs at most about 10% of training time. - Speed control while watching (up to a maximum speed). AI - Configurable from config.json: network type, hidden layers, layer sizes, activation functions, and combinations: fully connected, CNN, RNN (GRU/LSTM), and a custom layer list. Ship presets: hybrid (default: CNN on the board image plus an MLP on sensor features, merged, with a dueling output head), mlp, cnn, cnn_gru, mlp_lstm, custom. Do not test which architecture is best; just pick a good default. - Learning methods: reinforcement learning (DQN with a target network, double DQN, gradient clipping, configurable optimizer, learning rate, gamma, batch size, buffer size), supervised learning from human demonstrations, and a hybrid of the two. - Observation: board image channels (head, body, fruit, walls), optionally rotated to the snake's heading, plus sensor features (immediate danger, heading, fruit direction and distance, ray distances to wall/body/fruit, flood-fill free space per move, snake length). Must work on any grid size: feature part independent of board size, with global pooling in the CNN. - Training improvements that are game-independent and configurable, all on by default for the default preset, with the old behaviour selectable: - Prioritized Experience Replay (TD-error priorities, sum-tree, alpha/beta annealing, importance-sampling weights). - Noisy Nets (factorised Gaussian) as an alternative to epsilon-greedy exploration; greedy evaluation uses the mean weights. - N-step returns (default 3) with correct handling of episode ends. - Do NOT use a hand-made Snake expert (such as a Hamiltonian cycle) or a safe-move filter in the default setup. The AI must discover strategy by itself. An optional, clearly separated expert-demo mode is acceptable but off by default. - Additional training tools: curriculum on small boards followed by transfer to larger boards, partial weight transfer (freeze the feature part), and an evolution mode (--evolve) with parallel evaluation of a population across worker processes, plus mutation and inheritance of weights. - Parallelism: say clearly in the README whether games run sequentially or in parallel in each mode. - PyTorch must be optional: on machines where it cannot load (for example Windows Smart App Control blocking the torch DLL), the game and training must still run with a numpy MLP backend that has its own manual backprop and optimizers (adam, adamw, rmsprop, sgd). Verify its gradients against finite differences. Presets that need torch fall back to the MLP with a visible notice. Selecting the backend is a config option (auto, torch, numpy). COMMAND LINE (main.py) --train, --evaluate, --evolve, --workers, --pretrain, --curriculum, --grid, --freeze-transferred, and flags for expert demos if implemented. Document every option in the README, including what each one does. QUALITY - Clear package structure (game logic, environment, observation, agents, networks, replay, UI, config, high scores, CLI), no game logic inside the UI code. - Unit tests for the game rules, config handling, high scores, observation, every learning method (update, save, load), the numpy gradients, prioritized replay, noisy layers, n-step targets, the fast-training screen, the network view update rule, and a subprocess test that simulates torch being unavailable. All tests must pass. - Check any UI by rendering it headless and looking at screenshots: no overlapping or clipped text. - README covering installation (including without PyTorch), every mode and key, every config option, the CLI reference, how training works step by step (when and how weights update), how to configure the network, and honest expectations: how long training takes to reach a given score on 10x10 and 20x20, and what the known limits are. - Run a short real training to prove it learns (report scores of a random baseline vs the trained AI, and the old vs new default if you changed defaults). Report results honestly, including what did not work. - Clean up before packaging: no config, model, cache or log files in the zip. FINAL MESSAGE Brief. State what was built, how to start it (commands for Windows), test results, the measured training results with their limits, and the assumptions you made. Attach the zip.
A system prompt was added to support web rendering