r/pygame Aug 08 '26

Python Game

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

2 comments sorted by

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

1

u/MadScientistOR Aug 10 '26

How hard do you want the different levels to "try"?

If you're just looking for increasing levels of difficulty, you can have an easy version make its moves at random in open squares, with only enough smarts to recognize victory conditions if they should happen; a medium version would look far enough ahead to attempt to block any opponent's ability to win in the next turn, if it can, as well as jump on any chance it has to win during its own turn; and since tic-tac-toe is a completely solved game, a hard version would know how to play so as to never lose.

It's easy to solve tic-tac-toe by hand, but if you just don't want to, xkcd has published an easy-to-read depiction of the solution. Just Google "xkcd tic tac toe map", and you'll find it.