Automatic Tic Tac Toe Game using Random Number - Python Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 ImplementationA 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: Python 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: 2Code 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 detectionNote: Everytime we will run this code, we will get a different ouput because the games is played randomlyRelated articles:NumPyrandom module Comment More infoAdvertise with us Next Article Automatic Tic Tac Toe Game using Random Number - Python R RichaKumari1 Follow Improve Article Tags : Misc Python Practice Tags : Miscpython Similar Reads Tic Tac Toe Game using PyQt5 in Python In this article , we will see how we can create a Tic Tac Toe game using PyQt5. Tic-tac-toe, noughts, and crosses, or Xs and Os is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3Ã3 grid. The player who succeeds in placing three of their marks in a horizonta 5 min read Create a Simple Two Player Game using Turtle in Python Prerequisites: Turtle Programming in Python TurtleMove game is basically a luck-based game. In this game two-players (Red & Blue), using their own turtle (object) play the game. How to play The game is played in the predefined grid having some boundaries. Both players move the turtle for a unit 4 min read Draw a Tic Tac Toe Board using Python-Turtle The Task Here is to Make a Tic Tac Toe board layout using Turtle Graphics in Python. For that lets first know what is Turtle Graphics. Turtle graphics In computer graphics, turtle graphics are vector graphics using a relative cursor upon a Cartesian plane. Turtle is drawing board like feature which 2 min read Number Guessing Game Using Python Tkinter Module Number Guessing Game using the Python Tkinter module is a simple game that involves guessing a randomly generated number. The game is developed using the Tkinter module, which provides a graphical user interface for the game. The game has a start button that starts the game and a text entry field wh 5 min read Tic Tac Toe game with GUI using tkinter in Python Tic-tac-toe (American English), noughts and crosses (British English), or Xs and Os is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3Ã3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner. 15+ min read Like