This is the Part II of a board game Tic-Tac-Tao. In this post, a tree searching algorithm for determining the moves of the computer is presented. With that intelligence, the computer will act like an expert player and can beat all human in theory.
- A depth first search (DFS) algorithm is employed.
- A scoring scheme is installed to assist the DFS so that the algorithm will choose the path with the highest score to go in order to increase the chance of winning.
- The scoring scheme will consider both the computer's moves and the subsequent human's moves during the evaluation.
- The scoring scheme will work recursively until it reaches the predefined maximum depth of the tree, or until
it has found a way to win, or until it has tried all nodes.
- The AI has been tuned not to be so strong, otherwise it will either win or draw in each game. This is achieved by setting its first move by random, and use smaller value (e.g. 3) for the predefined maximum depth.
Source Code
Test Run
| |
---|---|---
| |
---|---|---
| |
Your turn [1, 2, 3, 4, 5, 6, 7, 8, 9] ? 3
| | O
---|---|---
| |
---|---|---
| |
Computer choose 4
| | O
---|---|---
X | |
---|---|---
| |
Your turn [1, 2, 5, 6, 7, 8, 9] ? 6
| | O
---|---|---
X | | O
---|---|---
| |
L1: c tries 1
L2: h tries 2
L3: c tries 5
L3: c tries 7
L2: h tries 5
L3: c tries 2
L3: c tries 7
L2: h tries 7
L3: c tries 2
L3: c tries 5
L3: c tries 8
L3: c tries 9
L2: h tries 8
L3: c tries 2
L3: c tries 5
L3: c tries 7
L2: h tries 9
Pos: 1 C Score: -7
--------------------
L1: c tries 2
L2: h tries 1
L3: c tries 5
L3: c tries 7
L3: c tries 8
L3: c tries 9
L2: h tries 5
L3: c tries 1
L3: c tries 7
L3: c tries 8
L3: c tries 9
L2: h tries 7
L3: c tries 1
L3: c tries 5
L3: c tries 8
L3: c tries 9
L2: h tries 8
L3: c tries 1
L3: c tries 5
L3: c tries 7
L3: c tries 9
L2: h tries 9
Pos: 2 C Score: -8
--------------------
L1: c tries 5
L2: h tries 1
L3: c tries 2
L3: c tries 7
L3: c tries 8
L3: c tries 9
L2: h tries 2
L3: c tries 1
L3: c tries 7
L3: c tries 8
L3: c tries 9
L2: h tries 7
L3: c tries 1
L3: c tries 2
L3: c tries 8
L3: c tries 9
L2: h tries 8
L3: c tries 1
L3: c tries 2
L3: c tries 7
L3: c tries 9
L2: h tries 9
Pos: 5 C Score: -7
--------------------
L1: c tries 7
L2: h tries 1
L3: c tries 2
L3: c tries 5
L3: c tries 8
L3: c tries 9
L2: h tries 2
L3: c tries 1
L2: h tries 5
L3: c tries 1
L2: h tries 8
L3: c tries 1
L2: h tries 9
Pos: 7 C Score: -7
--------------------
L1: c tries 8
L2: h tries 1
L3: c tries 2
L3: c tries 5
L3: c tries 7
L3: c tries 9
L2: h tries 2
L3: c tries 1
L3: c tries 5
L3: c tries 7
L3: c tries 9
L2: h tries 5
L3: c tries 1
L3: c tries 2
L3: c tries 7
L3: c tries 9
L2: h tries 7
L3: c tries 1
L3: c tries 2
L3: c tries 5
L3: c tries 9
L2: h tries 9
Pos: 8 C Score: -7
--------------------
L1: c tries 9
L2: h tries 1
L3: c tries 2
L3: c tries 5
L3: c tries 7
L3: c tries 8
L2: h tries 2
L3: c tries 1
L3: c tries 5
L3: c tries 7
L3: c tries 8
L2: h tries 5
L3: c tries 1
L3: c tries 2
L3: c tries 7
L3: c tries 8
L2: h tries 7
L3: c tries 1
L3: c tries 2
L3: c tries 5
L3: c tries 8
L2: h tries 8
L3: c tries 1
L3: c tries 2
L3: c tries 5
L3: c tries 7
Pos: 9 C Score: 3
--------------------
Computer choose 9
| | O
---|---|---
X | | O
---|---|---
| | X
Your turn [1, 2, 5, 7, 8] ? 5
| | O
---|---|---
X | O | O
---|---|---
| | X
L1: c tries 1
L2: h tries 2
L3: c tries 7
L2: h tries 7
Pos: 1 C Score: -7
--------------------
L1: c tries 2
L2: h tries 1
L3: c tries 7
L3: c tries 8
L2: h tries 7
Pos: 2 C Score: -8
--------------------
L1: c tries 7
L2: h tries 1
L3: c tries 2
L3: c tries 8
L2: h tries 2
L3: c tries 1
L2: h tries 8
L3: c tries 1
Pos: 7 C Score: 4
--------------------
L1: c tries 8
L2: h tries 1
L3: c tries 2
L3: c tries 7
L2: h tries 2
L3: c tries 1
L3: c tries 7
L2: h tries 7
Pos: 8 C Score: -7
--------------------
Computer choose 7
| | O
---|---|---
X | O | O
---|---|---
X | | X
Your turn [1, 2, 8] ? 1
O | | O
---|---|---
X | O | O
---|---|---
X | | X
L1: c tries 2
L2: h tries 8
Pos: 2 C Score: 2
--------------------
L1: c tries 8
Pos: 8 C Score: 10
--------------------
Computer choose 8
O | | O
---|---|---
X | O | O
---|---|---
X | X | X
(X) Computer wins!
No comments:
Post a Comment