RL2048 is a compact Deep Q-Network implementation for the game 2048. It includes both a training script for producing PyTorch checkpoints and a small Flask viewer for watching a trained policy play in the browser.
The project is intentionally small enough to read end to end, while still covering the core pieces of a reinforcement learning workflow: environment dynamics, experience replay, policy training, checkpoint loading, and interactive playback.
- 2048 environment with move validation, rewards, terminal-state handling, and observations for learning
- Deep Q-Learning agent implemented in PyTorch
- Automatic device selection for training (
cuda,mps,xpu, orcpu) - Browser viewer with autoplay, pause/resume, single-step agent control, and manual moves while paused
- Bundled
dqn_2048.pthcheckpoint so the viewer can run immediately after setup - Unit tests covering the game logic, agent behavior, and server/controller flows
git clone https://github.com/rexmhall09/RL2048.git
cd RL2048
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python server.pyOpen http://127.0.0.1:5000 in your browser.
The viewer loads dqn_2048.pth from the project root by default.
python server.pyViewer controls include:
- pause and resume autoplay
- step the agent one move at a time
- play manually with the on-screen arrows or keyboard arrow keys while paused
- reset the board
- adjust autoplay speed
Train a new checkpoint with:
python train.py --episodes 30000 --device autoOptions:
--episodes: number of training episodes--device:auto,cuda,mps,xpu, orcpu
Training writes:
dqn_2048.pth: best checkpoint seen during the rundqn_2048_final.pth: final checkpoint at the end of training
python -m unittest discover -s tests -vgame_2048.py— 2048 environment and game rulesdqn_agent.py— replay buffer, network, and action-selection logictrain.py— training entry pointserver.py— Flask app and viewer controllertemplates/index.html— browser UI for the viewertests/— unit testsdqn_2048.pth— bundled checkpoint for the viewer
If you find a bug, have a question, or want to suggest an improvement, open an issue on GitHub.
This project is licensed under the MIT License. See LICENSE for the full text.