This repository contains a single HTML file (index.html) that demonstrates a Q-Learning-based “Run & Catch” scenario on a 6×6 grid:
- Two Runners (blue) try to survive for 30 seconds.
- Two Chasers (red) attempt to catch both Runners before time runs out.
- A Q-Learning system updates the policy for both teams (Runners and Chasers) each episode.
- LocalStorage is used to save/load the learned Q-tables, allowing the AI to persist across browser sessions.
- A custom HUD shows the game state (time, caught runners, etc.), and a Chart.js line graph tracks the Runners’ Win-Rate over multiple episodes.
-
Q-Learning for Both Teams
- Each team has its own Q-table.
- (\epsilon)-greedy actions; rewards for distance changes, attacking, catching, or avoiding capture.
-
Auto-Restart Episodes
- Game runs for up to 30 seconds.
- If both Runners are caught beforehand, the Chasers win immediately.
- Otherwise, if time expires with at least one Runner free, the Runners win.
- The code automatically starts a new episode (unless stopped), incrementing an episode counter.
-
Model Persistence via LocalStorage
- Q-tables are saved after each episode.
- On reload, the game continues from previously saved Q-tables.
-
HUD & Live Chart
- Time left, actions taken, episodes, and game status displayed in a panel-based HUD.
- Chart.js line chart for Runners’ overall win-rate across multiple episodes.
- Clone or Download this repository (you only need
index.html). - Open
index.htmlin a modern web browser:- Either directly (some browsers might restrict localStorage if loaded via
file://). - Or, use a basic local server, for example:
then go to http://localhost:8000/index.html in your browser.
python -m http.server
- Either directly (some browsers might restrict localStorage if loaded via
- Click “Start” to begin the first episode:
- Two Runners and two Chasers will move on the 6×6 grid.
- The Runners try to survive; the Chasers try to catch them.
- Observe the Q-Learning in action:
- At each episode’s end, the result (Runners win or Chasers win) updates the stats.
- After a brief pause, the next episode starts automatically (unless you click “Stop”).
- Monitor the HUD:
- Time left in the current episode.
- How many Runners are caught.
- The chosen action (e.g.,
UP, DOWN, STAY, ATTACK, SPECIAL) for each team. - The overall episode count, and in the Chart, the Runners’ win-rate.
- Stop the process anytime with the “Stop” button. The next run can continue from the stored model.
- Adjust Q-Learning: Tweak parameters (\alpha, \gamma, \epsilon) in the Brython code.
- Rewards: Edit the distance-based rewards, catching penalty/bonus, or attack/special logic to encourage different AI behaviors.
- Auto-Restart: Switch off
AUTO_RESTARTor changeEPISODE_DELAY_MSin the code to control the downtime between episodes. - LocalStorage: If you prefer ephemeral training, remove the calls to
load_all_q_tables()/save_all_q_tables().
Feel free to open issues or pull requests to improve the AI logic, add new features, or enhance the visuals. Feedback is welcome!