TicTacToe AI Making Incorrect Decisions

后端 未结 4 1263
攒了一身酷
攒了一身酷 2021-02-08 12:30

A little background: as a way to learn multinode trees in C++, I decided to generate all possible TicTacToe boards and store them in a tree such that the branch beginning at a n

4条回答
  •  天命终不由人
    2021-02-08 13:17

    Tic-Tac-Toe can be solved using a greedy algorithm and doesn't really require a decision tree.

    If you want to continue using your current algorithm, do as patros suggests, and minimize the possibility of losing at each decision.

    If you want a simpler approach have the AI do the following each turn:

    1. Complete a winning Tic-Tac-Toe if possible.
    2. Block an opposing Tic-Tac-Toe if possible.
    3. Rate each square for its desirability, for each other taken square (by the AI) on a line, add one point of desirability for that square. For each square taken by the opponent, remove one point of desirability.

      For example, if the board is currently:

      _|O|X
      _|X|_
      O| |
      

      The top-left corner has a desirability of 0 (1 for the X in the same row, and 1 for the X in the diagonal, but -1 for each of the Os).

    4. Play on the most desirable square. Breaking ties arbitrarily.

      In the example from above, the AI would choose the mid-right square, since it has a desirability of 2, which would lead to a win the following turn.

    5. If the game has just begun, play the center square, if the center square is taken, choose a corner at random.

    6. Win (or tie).

    This was my grade 10 Visual Basic term project. It's impossible to beat and requires far less memory than storing a decision tree.

提交回复
热议问题