![]() |
VOOZH | about |
Tic-Tac-Toe is a simple and fun game. In this article, we’ll build an automatic version using Python. The twist is that the game plays itself, no user input needed! Players randomly place their marks on the board and the winner is declared when three in a row is achieved.
We’ll use:
A Python program that:
Note: Run this code in a local Python environment to ensure smooth execution of the required modules.
Code:
Output:
[[0 0 0]
[0 0 0]
[0 0 0]]
Board after move 1:
[[0 0 0]
[0 0 0]
[0 1 0]]
Board after move 2:
[[0 0 0]
[0 0 0]
[2 1 0]]
Board after move 3:
[[0 0 0]
[1 0 0]
[2 1 0]]
Board after move 4:
[[0 0 0]
[1 0 2]
[2 1 0]]
Board after move 5:
[[0 1 0]
[1 0 2]
[2 1 0]]
Board after move 6:
[[0 1 0]
[1 0 2]
[2 1 2]]
Board after move 7:
[[1 1 0]
[1 0 2]
[2 1 2]]
Board after move 8:
[[1 1 2]
[1 0 2]
[2 1 2]]
Winner is: 2
Code Breakdown:
Note: Everytime we will run this code, we will get a different ouput because the games is played randomly