Automatic Tic Tac Toe Game using Random Number - Python
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:
- NumPy for managing the 3x3 board.
- random module for placing marks randomly.
Game Implementation
A Python program that:
- Initializes a 3x3 Tic-Tac-Toe board.
- Lets player 1 and 2 take turns placing marks randomly.
- Checks for win conditions (rows, columns, diagonals).
- Prints the board after each move.
- Announces the winner or a draw when the game ends.
Code:
import numpy as np
import random
from time import sleep
def create_board():
return np.zeros((3, 3), dtype=int)
def possibilities(board):
return [(i, j) for i in range(3) for j in range(3) if board[i][j] == 0]
def random_place(board, player):
loc = random.choice(possibilities(board))
board[loc] = player
return board
def row_win(board, player):
return any(all(cell == player for cell in row) for row in board)
def col_win(board, player):
return any(all(row[i] == player for row in board) for i in range(3))
def diag_win(board, player):
return all(board[i][i] == player for i in range(3)) or \
all(board[i][2 - i] == player for i in range(3))
def evaluate(board):
for player in [1, 2]:
if row_win(board, player) or col_win(board, player) or diag_win(board, player):
return player
return -1 if np.all(board != 0) else 0
def play_game():
board, winner, move = create_board(), 0, 1
print(board)
sleep(1)
while winner == 0:
for player in [1, 2]:
board = random_place(board, player)
print(f"\nBoard after move {move}:\n{board}")
sleep(1)
move += 1
winner = evaluate(board)
if winner != 0:
break
return winner
# Run the game
print(f"\nWinner is: {play_game()}")
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:
- create_board(): Initializes the Board, creates a 3x3 grid filled with zeros using NumPy where 0 means an empty cell.
- random_place(): Selects a random empty spot on the board and places the player’s number (1 or 2):
- Win Check Functions: row_win(), col_win(), diag_win() -These functions verify if a player has a winning line:
- evaluate(): Determines Game Status by checking if any player has won, or if the board is full (draw). Returns 1 or 2 if a player wins, -1 for draw and 0 if the game is still on.
- play_game(): It creates the game loop, i.e. it handles turn-taking, board updates and win/draw detection
Note: Everytime we will run this code, we will get a different ouput because the games is played randomly
Related articles: