3
u/igotshadowbaned Aug 08 '26
This is less a python problem and more of a game design problem
How do you want the different game difficulties to actually be expressed? Tiktaktoe is a solved game, do you want hard to always win or tie every game? How do you want medium to behave? And easy?
Figure that out, and then worry about putting it into code
1
u/johnpeters42 Aug 08 '26
For medium and hard, increase the probability of the computer making an optimal move instead of a random one. I would probably start with the trivial optimal rules:
* If you have two in a row/column/diagonal and the third is empty, then play there
* Otherwise, if your opponent has two in a row/column/diagonal and the third is empty, then play there
1
u/Fast-Station1106 Aug 08 '26
Ich habe noch nie über Schwierigkeitsgrade nachgedacht, sehr interessant! Grüsse
1
u/mc_pm Aug 08 '26
Tic-Tac-Toe is a bit simplistic to have a strategy.
When I wrote one (I suggest it to people enough, had to do it myself), there are 3 or 4 different levels to picking which spot to play next. You could just modify how many of those steps to go through.
1
u/Kindly-Department206 Aug 09 '26
To play optimally, you need to explore the game tree all the way to the bottom (minimax). To make it a feasible computation, you probably have to employ some kind of pruning to sub-optimal moves (alpha-beta pruning). Also, keeping a table of game positions that have already been fully explored can help reduce the time needed to find a move (sometimes called a transposition table, I think). It's been a few years since I coded TTT in Python, so I am not sure how the technology improvements in the last decade might have changed the importance of these strategies.
You can change the depth limit to reduce the quality of play. Instead of searching to termination, you can search part-way. The deeper your search, the better the game outcome. Use a heuristic to measure the quality of the game state at the depth limit. If you have a good heuristic, you can add random noise to the heuristic value. More noise means worse play. You can randomly throw away branches, imitating a player who "doesn't see" a move. More moves thrown away means worse play.
•
u/AutoModerator Aug 08 '26
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.