Search code examples
javaandroidartificial-intelligenceminimax

What are effective ways to implement "difficulty" levels to an AI in Tic Tac Toe?


I am developing a simple Tic Tac Toe android app that supports 1 or 2 players. I have implemented an AI for 1 player mode which uses the minimax algorithm to play perfectly (either wins or ties). I want to allow for easy, medium, and hard difficulties the user can switch between. What are some ways I can achieve this?

My first thought was randomly choosing between making a random move or a perfect move. The probability for making a perfect move would be 60% for medium and 10% for easy. Any other ideas or modifications?


Solution

  • You have a few ways you can adjust strength, although the level of granularity is not great for a game as simple as tic-tac-toe.

    1. Limit your search depth. For example: if the AI only looks 1-2 turns ahead, it's possible to use a strategy to trap it into an unavoidable losing state, while a deeper tree could predict well enough to counter every strategy and always force a draw.
    2. Weaken your evaluation function. This is a bit difficult to do meaningfully in tic-tac-toe, but you might be able to come up with something. If the AI undervalues or overvalues something, it will play worse.
    3. Add noise. Give your program a random chance to select a suboptimal move.
    4. Bias suboptimal decisions. Make the AI less likely to lead the first move with a corner spot, for example.

    You will need to experiment to find out what feels right.