This is an AI Snake, I mentioned it like tweny times I know.
I need to warn you this project is still in DEVELOPMENT.
This project has in it:
- DQN NN:
- DQN neural network with greedy algorithm, target/policy function, automated training.
- Snake Game:
- Classic snake game like old nokia had, all thoght it has some issues.
- Server:
- Basic server hosted using framework
Sinatrain Ruby.
This is everthing in the project I will explain the rest down here.
- Basic server hosted using framework
Oww how we love backend do we ? This project uses really unsecure techniques how can Server communicate with Client and AI. It works like this:
-
- Client/Frontend will read /ai-latest for any connected AI's
-
- Server will host 3 of paths for AI
-
- AI Communication system will send json of info, example (python variable):
Or in JSON:payload = { "name":"SimpleAI", "training":"no", "status":"run", "direction_x":0, "direction_y":0 }
Program will read this JSON and will use ALL values. I will inform you more just keep reading.{ "name":"SimpleAI", "training":"no", "status":"run", "direction_x":0, "direction_y":0 } -
- Final.
- AI will send info about its existense to
/aiin JSON format like I mentioned - Server will read it and post it on
/ai-latestin JSON format for Client - Client will read
/ai-latestand will move Snake as AI needs. - Client will post response on
/ai-responsein once again JSON format for AI.
This process repeats with 0.2 second delay
Artistic one hm?
This project uses basics nothing to talk about here.
All info about grid are shown on web (frontend) unless you change it.
AI uses raw socket to post and receaive data from Server.
AI always will send startup-payload it is the same payload as I shown before.
This startup payload will send all of the info like direction_x, direction_y but with 0 values.
All of those metadata like name, training, status MUST be mentioned
In main_loop always put delay of 0.2 seconds to prevent any un-sync (not being synchronized).
Tuff guy that holds it all.
To run this you need ruby!
- Libs
- Sinatra
Sometimes they will be error that puma is not installed then:
gem install sinatra
gem install puma
- Python DQN example
- For this one use Python 3.13 (2025 - latest)
- Use:
- NumPy:
Or for Linux
pip install numpy
python3 -m venv MyEnv source MyEnv/bin/activate python3 -m pip install numpy - Torch/Pytorch
Or for Linux
pip install pytorch
python3 -m venv MyEnv # Assuming if you already did this ignore source MyEnv/bin/activate python3 -m pip install pytoch
- Matplotlib:
Our Linus friends
pip install matplotlib
# Same thing python3 -m pip install matplotlib
- NumPy:
- This is everthing you need for python DQN example
- JS
- You just used browser for this one.
- Use this command to install everthing (ONLY LINUX):
sudo apt-get update sudo apt-get install ruby python3 python3-venv python3-pip firefox -y # Check it and delete what you dont need gem install sinatra gem install puma # optional if sinatra throws error while testing !!! sudo python3 -m venv MyEnv source MyEnv/bin/activate python3 -m pip install numpy pytorch matplotlib
- Sinatra
- Run
- To run this Program you need to first run server (Wow no sht!)
- To run it use:
ruby server.rb
- Then when your server is running check:
start firefox "127.0.0.1:4576" - Now when you have your client runnin, run this:
For any errors I am sorry. The DQN will start only when the Client is running ans try to run it multiple times until it works The DQN "template" (only if you want to modify it) when start is automaticly in train mode to change this wait for next update :).
python3 DQN.py
In future I plan it to save models progress and load it when wanted.
- Hope
- If you have religion pray, pray I mean it
- If not then get one (if you want)
- Do you want christianity ?
- here:
- [Your-name], ego te baptizo in nomine Patris et Filii et Spiritus Sancti.
- pouring water at you (!!!NOT A JOKE!!!)
I programed this on Windows so I had my limitations.
Here is my aproven that you can use my project as a template but please be kind and always leave my name at the bottom like:
Thanks to TheBlackDev for a template.
I recommend good PC that can hold Basic DoS because of the AI Communication.
If tuning settings like setInterval you need to have AI update interval (that 0.2 seconds) below players update interval
Good luck with tuning, modifing, injecting diffrent model.