Yo Developers!!
So, a few days ago, I was thinking about building something using AI.
I didn't want to make another chatbot or just integrate an API into a website. I wanted to build something where AI actually does something.
But I had a problem.
I didn't have any GPU, ML resources, or much knowledge about training AI models.
I wasn't even sure where to start.
So I did what most of us probably do when we have a random idea...
I asked ChatGPT. ๐
I started asking it about different AI models, how their backend works, how they learn, and whether I could build something interesting without needing expensive resources.
That's when I came across Q-learning.
And somehow, this random exploration turned into a game.
Meet NinjaDuel!
๐ฎ Play it here: https://developerpuneet.github.io/NinjaDuel/
Github: https://github.com/DeveloperPuneet/NinjaDuel
Okay, how did I actually build it?
I didn't build the whole thing in one go. It was more like experimenting with different things and seeing what worked.
Here's how it happened.
1. Started with a basic game
First, I decided to make a simple ninja fighting game.
I started with the basics:
- Player movement.
- Basic attacks and fighting mechanics.
- A simple background.
- Keyboard controls using JavaScript.
- Basic collision and movement logic.
Nothing fancy at this point.
I just wanted two characters to fight each other and have something playable and i made them... simple...
2. Then I gave it to DeepSeek...
Once I had the basic game working, I wanted to improve how it looked.
So I gave the project to DeepSeek and asked it to improve the UI.
AND BRO... ๐ญ
It came up with a pretty insane-looking UI.
I genuinely liked what it created, so I kept that direction and continued improving the game.
This was the point where it started looking like an actual game instead of a random JavaScript experiment.
3. Discovered Q-learning through YouTube
Now came the interesting part.
I didn't want the opponent to just perform random attacks or follow a fixed pattern.
I wanted an opponent that could learn.
I started watching YouTube videos about reinforcement learning and Q-learning.
After watching a few tutorials, I understood the basic concept:
An AI agent observes a situation, takes an action, receives a reward, and updates what it has learned.
I decided to try implementing a basic structure of Q-learning myself.
I started thinking about:
- What information should the AI observe?
- What actions should it be allowed to take?
- How should it receive rewards?
- How should it remember what it learned?
I created a basic structure for this and then used Claude to help improve and refine the implementation.
Obviously, I had to experiment with it and connect the learning logic to the actual game mechanics.
And that's how the Q-learning-based opponent started taking shape.
Wait, how does the AI actually learn?
Let me explain the basic idea.
Q-learning is a reinforcement learning algorithm.
Imagine you're playing a game for the first time. You don't know which moves are good.
You try different actions.
Some work well, some don't.
You learn from those experiences.
Q-learning follows a similar idea.
There are four important things:
- State: What is happening in the game right now?
- Action: What can the AI do?
- Reward: How useful was the result?
- Q-value: How valuable is an action in a particular state?
The AI maintains a Q-table containing values for different state-action combinations.
After taking an action and receiving feedback, it updates those values.
The general Q-learning update equation is:
Q(s,a) = Q(s,a) + ฮฑ[r + ฮณ max Q(s',a') - Q(s,a)]
Don't worry if that looks complicated. The basic idea is that the AI updates its estimate of how good an action is based on the reward and what it expects to happen next.
How Q-learning is used in NinjaDuel
In NinjaDuel, the AI observes the combat situation, including things like:
- Distance between the player and opponent.
- Current combat situation.
- Available actions.
Based on the current state, it chooses an action.
It can move, attack, block, jump, and perform other actions.
Afterward, it receives feedback and updates its Q-values.
With more matches, it can use its stored experience to make different decisions.
I also added two different AI modes:
1. Q-learning AI
Starts with an empty Q-table and learns through matches.
2. Pretrained Q-learning AI
Starts with existing practice values and continues learning while playing.
The learning data is saved using browser localStorage, so the AI can retain its learned values between visits.
How to play?
You can choose your opponent and jump straight into the fight.
Here are the controls:
| Action | Key |
|---|---|
| Move | A / D or Arrow Keys |
| Jump | W / Space / Up |
| Block / Parry | S / Down |
| Quick Slash | J / F |
| Heavy Slash | K |
| Kick | L |
| Shuriken | U |
| Dash Strike | I |
| Quick Attack | Left Mouse Button |
| Pause / Resume | P / Esc |
Some extra controls
| Key | Function |
|---|---|
| Tab | Toggle AI Mind View |
| 0 | Reset AI learning memory |
| M | Open opponent selection |
| 1 / 2 / 3 | Change difficulty |
| R | Rematch after knockout |
One feature I particularly like is AI Mind View.
Press Tab during the game to see information about the AI's decision-making.
I wanted to make the AI learning process more visible rather than just having a mysterious opponent fighting in front of you.
What did I use?
The project is built using:
- HTML
- CSS
- JavaScript
- Canvas API
- Q-learning-inspired reinforcement learning
I also added sound effects, particles, different attacks, difficulty levels, offline support, and PWA support.
And the interesting thing is that everything runs directly in the browser.
No external AI API, no GPU, and no model hosting required.
It's open source!
I have published the project on GitHub under the MIT License.
You can explore the code, try it, or contribute if you find something interesting.
๐ฎ Live Demo: https://developerpuneet.github.io/NinjaDuel/
๐ป GitHub Repository: https://github.com/DeveloperPuneet/NinjaDuel
What's next?
Honestly, this project started with a random question to ChatGPT.
Then I wrote some JavaScript, experimented with DeepSeek, learned Q-learning through YouTube tutorials, and used Claude to improve my implementation.
I didn't expect to end up with an entire playable game.
I'm still learning about reinforcement learning, so I'd genuinely love to hear suggestions from people who have worked with it.
What would you improve in the AI?
And what other interesting things can we build using Q-learning?
Also, try the game and tell me if you managed to defeat the AI. ๐ also the learning data is stored in your device as cache
Thanks for reading!!




Top comments (49)
Nice build, and for me the best is the AI Mind View. You made the decision visible which I think is more than most demos bother with.
What I'd add isn't in the algorithm. Right now, the only evidence it's learning is that the opponent feels harder, and you're learning too, so you can't separate the two. Play the agent against a fixed scripted opponent for a few hundred matches and plot the win rate.
And the localStorage learning is per browser and per device, so wrap the read in a try/catch. It can come back empty or throw in private browsing.
Thanks a lot for the detailed feedback! Really appreciate it. ๐
Actually, before writing the post, I tested the AI myself. I played 29 matches against it, and it managed to win only 5 of them ๐
But you're right, that's not really a proper way to measure its learning since I'm also getting better at the game while playing.
The fixed-opponent idea is interesting. I'll try running multiple matches against a scripted opponent and track the win rate to see how its performance changes over time.
And thanks for pointing out the localStorage edge case! I'll look into handling that as well.
Really appreciate you taking the time to check out the project and share these suggestions!
Don't mention. I am bored now just finished my second series. And I had fun reading through your article.
Haha, glad you enjoyed reading it! And congrats on finishing your second series! What was it about? ๐๐๐คฃ๐คฃ
100 Days of DevOps and Cloud. One lab a day for a hundred days, AWS, then Azure, with Ansible and Terraform near the end.
Working the tasks in labs was pretty easy. The writing was where I found out what I'd actually understood, which is why most of the posts ended up being about something that looked right and wasn't. I had to look at technical blogs, documentation, etc to get full insight into the tools and the expected results.
Day 100 went out this morning, so your post was good timing. ๐
Woah, 100 days straight?! That's some serious dedication! ๐ฅ Huge congratulations on completing the series!
And I totally get what you mean about writing revealing how much you've actually understood. It's one thing to get something working in a lab, but explaining why it works is a whole different challenge.
Thank you, @puneetkumar2010. It was worth it.
Yes, that's the skill I'm developing: training myself to read deeply and to take an architect-level view of anything.
That's a really interesting skill to develop. I think it's easy to focus on just getting something to work, but understanding the bigger picture and why things work the way they do is a whole different level.
I'm still learning that myself through building projects. Definitely something worth working towards! ๐
Hey Puneet! played two rounds just now, the ninja started reading my jump attacks by round three lol. closed the tab and came back later, it still remembered how i play. bit spooky haha. if i spam one combo does it just keep farming me? thanks :D
Haha, the ninja is watching your moves! ๐
Yep, the learning data is stored in your browser's localStorage, so it remembers your playstyle even after you close and reopen the tab.
And yes, if you keep spamming the same combo, the AI can start adapting to it and countering it. The idea is to make it learn from your patterns rather than just follow predefined enemy behavior.
Glad you gave it a try! ๐
thanks Puneet!! localStorage explains it lol xD spammed jump attack.. got farmed haha, thanks :D
Lmaoo ๐ looks like the ninja figured out your strategy! Time to switch up your moves and surprise it this time haha. Thanks for playing again! ๐
haha Puneet it read me xD I kept jumping... 6 times lol, yeah I'm switching it up :D
Lmaooo ๐ the ninja has officially caught on to your favourite move! Now let's see if you can outsmart it haha. Waiting to see what strategy you try next! ๐
lol Puneet yeah it fully had my number xD jump jump jump, that was me the whole time... ok next run I'm forcing myself to just... wait a second before I move, throw a dash in, probably still get read haha. thanks again, this was a blast :D
Hahaha ๐ Next time, try pressing the Tab key during the game! You'll get to see how the AI's brain is working.
There's an AI Mind View that shows how it's learning, trying to predict your next move, and what action it plans to take depending on whether you're jumping, dashing, standing still, etc.
You can literally watch its decision-making in real time and ai's thought process!
Would love to hear what you think of it! ๐
Puneet ohh Tab key xD that's getting pressed next run, watching it call my jump before I jump... humbling stuff lol. thanks for the heads up btw, I'll let you know how badly it reads me this time :D
๐ Looking forward to your next run! The best part is watching the AI predict your moves and seeing whether its decisions actually match what you're about to do.
Let's see if you can finally outsmart it this time! xD
Puneet deal xD Tab key open, dash only, jump button is dead to me lol. thanks for believing in me :D
Waaaait bruhhh ๐๐ญ Dash is actually the fastest attack in the game! I gave the AI bot a normal human reaction time to respond, so technically, if you time your attacks right, defeating it shouldn't be that difficult haha.
Ngl, when I was building this, I was so confused because the AI wasn't reacting properly, and I had to figure out a way to fix that. And you're literally the only person who's gone this deep into actually testing it after me!! ๐ญ๐
You're genuinely investing in this game, bruhhh!! Btw, are you actually enjoying it? :D
One thing though: I feel like playing without spamming moves gives you a much better experience. You get to actually feel the game and see how the AI adapts.
Now I'm wondering... should I add a cooldown timer to skills as well? Because spamming dash can feel a little like cheating against an AI that has human-like reaction time lol.
2 minutes of silence for the ninja if you go full dash mode ๐๐
Puneet xD fastest attack, ok that changes things lol. and yeah I'm genuinely enjoying it, it's stuck in my head rent free. cooldown wise a small one could work I reckon, just not so long that dash stops feeling fun. mixing attacks next run, proper ninja behaviour :D
Lmaooo bruhhh ๐ You can literally finish the game in 4โ5 seconds if you keep spamming dash ๐
And yeah, I'm thinking of keeping the cooldown really short, around 0.5โ1 second max. Just enough to prevent dash spamming without ruining the fun!
Can't wait to see your mixed-attack ninja strategy next run
Puneet lol dash spam speedrun xD 4-5 seconds and done, poor AI never stood a chance. 0.5-1s cooldown sounds right btw, mixed attacks next :D
๐ the ninja needs some survival chances too bruhhh xD
Let's see how your mixed-attack strategy goes next time! I'm curious whether the AI will adapt to that as quickly as it did to your jump spam ๐
Puneet lol fair, ninja deserves a fighting chance xD mixed attacks next run and we'll see, if it adapts that fast again I'll honestly be impressed. thanks for the game btw :D
Haha, you're welcome bruhhh!! ๐โค๏ธ Honestly, thanks for spending so much time playing and experimenting with it. Your feedback means a lot!
Now I'm curious too whether the ninja can keep up with your mixed strategies xD
Waiting for your next battle report haha! ๐ญ
I didn't know about Q-learning! ๐ธ๐
It's fascinating how the opponent learns through playing against you. This could be a really interesting direction for the next generation of CPU opponents in games!
I'm interested in game development myself, so I'll definitely take some inspiration from your project!
By the way, I defeated your AI! ๐น๐
Haha, you defeated it! ๐๐
Yeah, in the beginning, the AI behaves pretty dumb. But I tested it for around 60โ70 matches, and now I'm seeing some interesting behaviour. It's started dodging my shurikens and kicks at the right moments. Before I land a heavy sword attack, it often moves back or interrupts its own action to avoid getting hit. That's the kind of improvement I wanted to see!
One thing to mention: its learning is mainly stored in your browser's local storage, so it learns from your matches on your device. If you play against my AI, it'll probably be much harder after you've played several matches with it. ๐
Actually, I've been thinking about trying Q-learning with a chatbot too. But then I wondered... would that even work? ๐
Imagine:
User: How are you?
AI: Who are you? ๐
I guess Q-learning could help a chatbot learn which responses or actions users prefer, but it probably wouldn't be enough on its own to understand and generate natural conversations. Still, it might be an interesting experiment!
I also like the idea of lightweight AI that can run on CPU-based devices, especially since GPUs can be pretty expensive.
Btw, what kind of project are you thinking of building with Q-learning? I'd love to hear your ideas! ๐
Haha!! Maybe I should retire undefeated before your AI gets too strong! ๐น๐
One idea I have is to let an AI-controlled character initially react to a boss's movements with random actions, then gradually learn which responses are most effective.
If the learning data can be stored on the client side using something like localStorage, there could be quite a lot of interesting possibilities without requiring expensive servers or GPUs!
I'd love to explore this further. Thanks for introducing me to Q-learning! ๐ธ๐
Haha, exactly! ๐ I'm actually using client-side localStorage only to store the learning data. Haven't connected any database or external server to it. Everything runs entirely in the browser!
That's what makes experimenting with Q-learning so interesting without needing extra infrastructure.
Pretty Amazing. I tried it myself too, and while enjoying the game, I decided to test the learning capabilities by throwing the same attack again and again. At first, it lost again and again, but then it started blocking my attacks and started counterattacking.
That seems pretty amazing to me. In fact, I am also working on a project where I need AI to learn, and now I think I should also use Q-Learning there.
Yooo, that's awesome! ๐๐ฅ The fact that you kept repeating the same attack and it eventually started blocking and counterattacking is exactly the kind of behaviour I wanted to see when building this.
And that's interesting about your project! Q-learning is definitely worth exploring if your AI needs to learn through actions and rewards.
Btw, is your project open source? I'd love to check it out! Also, what is it about? Is it a game or some kind of software? ๐
Yeah, my project is definitely open-source, but I am currently building it with my 2 other friends, as it is our Final Year Project. It will be a middle layer that will encounter every incoming request and check if it's a hacker or a normal person.
The AI part is that it will encounter the request, display it on the dashboard and check the behaviour and pattern of the user. If the AI can't decide, then the request will be sent to the human, he will check and then mark it either as a normal user or a hacker and the AI will then store that pattern in itself.
Ohh, now I get the idea! That's actually a really interesting project. ๐
So basically, you're building a security layer that monitors incoming requests, detects suspicious behaviour, and learns from human feedback whenever it isn't confident about a decision.
I especially like the human-in-the-loop part. It means the system can keep improving from patterns that humans identify.
Are you planning to use Q-learning for this, or are you exploring some other machine learning approach?
Also, would love to check out the GitHub repo once it's public! Good luck to you and your teammates with the final year project!
Thanks, there is no code at the moment because we are doing research on the attacks and the different kinds of attacks that hackers do.
The human part was added because we don't want the AI to handle everything, as it might start hallucinating.
Regarding th Q-learning, I am mainly interested in any Reinforcement learning type, since now, I have seen a demo of Q-learning, I will definitely discuss this with my teammates.
Ohh, now I get the idea! That's actually a really interesting project. ๐
So basically, you're building a security layer that monitors incoming requests, detects suspicious behaviour, and learns from human feedback whenever it isn't confident about a decision.
I especially like the human-in-the-loop part. It means the system can keep improving from patterns that humans identify.
Good luck to you and your teammates with the final year project!
i like it :O
๐ glad you liked it!!
The AI Mind View is the bit I'd steal โ making the decision visible is what turns a demo into something you can actually learn from. The measurement problem ndcodes raised is the one that'd bug me though: you're getting better at the same time the AI is, so the win rate doesn't prove much either way. Have you tried freezing a Q-table snapshot and running it against a fresh random player for a hundred matches? That'd give you a baseline that doesn't move while you do.
Thanks Paul! ๐ The AI Mind View was something I really wanted to include because I think making the learning process visible is just as interesting as the gameplay itself.
And that's a really good point about the measurement problem. Since both the player and AI are adapting, win rate alone isn't a reliable measure of how much the AI has improved.
I haven't tried freezing a Q-table snapshot against a fresh random player yet, but that's definitely an interesting experiment. Could give a much clearer picture of how well the AI is actually learning. Appreciate the suggestion!
A game that adapts to the player is a much harder claim than a game that reacts to the player, and the difference is where I'd poke. In our own runs this week we asked a model a question five different ways with no documents at all, and it answered --no-cache five times out of five, because the flag was already in its head, not because it learned anything from the run. How do you tell, in your game, whether the ninja is actually adapting to how someone plays versus just repeating a pattern that already worked once?
That's a fair question! ๐
The distinction is important. In my game, the ninja uses Q-learning, so it's not simply hardcoded to repeat a successful move. It updates its Q-values based on the player's actions and the rewards it receives over multiple interactions.
One interesting thing is that the AI Mind View actually shows which moves the ninja chooses in response to certain player actions. I've observed those moves changing when the player's strategy changes over around 3โ4 matches. So you can actually see the adaptation happening rather than just noticing a different difficulty level.
The learning data is also stored in client-side localStorage, so its behavior can evolve across sessions.
That said, I agree that seeing different reactions doesn't automatically prove genuine adaptation. Testing it against different playstyles and comparing its behavior with a fixed baseline would be a much better way to validate that.
Definitely something worth experimenting with! Thanks for bringing this up.
Fellow game dev hereโlove how lightweight this is! Using Q-tables and browser storage to get adaptive enemy behavior without external servers is a great approach ๐
Thanks! ๐ Really wanted to keep it lightweight and fully client-side without depending on external servers. Glad to hear another game dev appreciates the approach!
Tab showing the AI Mind View mid-fight sold me on Q-learning faster than the 29-match test ever could.
That's awesome to hear! ๐ I wanted the AI Mind View to make the learning process visible rather than keeping everything hidden behind the scenes. Glad it helped make Q-learning easier to understand!
Bro, nice Game ๐๐ฅฐ๐
Thank you so much!! ๐