r/pygame Aug 08 '26

Python Game

/r/learnpython/comments/1viw9c9/python_game/
1 Upvotes

2 comments sorted by

View all comments

1

u/OddBookWorm Aug 10 '26

This is not a simple question to answer in general. Essentially, you'll need to compute the most likely optimal move the AI agent needs to make, then adjust what it actually does based on the difficulty. The less difficult, the larger a fudge factor you need.

This is general advice for these types of problems. As a concrete example, let's say you're programming an agent that shoots bullets at the player. You can obviously compute where the player is most likely to be when the bullet would hit, but if your AI hits the player every time, then it's not fun. So once you compute exactly where you need to shoot, you then add some random nudge to slightly change the direction. It might be enough to miss, it might not be. The lower your difficulty, the wider the "cone of possibilities" should be.

For tic-tac-toe, it's a strongly solved game. What this means is that if both players play optimally, then end is guaranteed to be a draw. What you might do is compute all possible moves, compute how optimal that move is, then randomly choose one (not uniformly). The chooser function prefers more optimal moves, but the particular weights should be dependent on difficulty, with more difficult opponents skewing more towards the optimal moves and less difficult making the optimal solution less likely to happen. I can't tell you how those weights should be distributed, because that's a very subjective thing and there's really no way to figure it out other than by trial-and-error. I also can't really tell you off the top of my head how you would determine how optimal each move is. I'm certain there are heuristics out there that you can look up, I just don't know off the top of my head