Write a program to implement Tic-Tac-Toe game using python.

Introduction

Tic-Tac-Toe is a simple yet classic game that has been enjoyed by people of all ages for generations. It's a two-player game where the objective is to get three of your marks in a row, column, or diagonal on a 3x3 grid. Implementing Tic-Tac-Toe in Python is a great way to practice your programming skills, especially if you're learning about loops, functions, and basic AI.



In this article, we'll walk through the steps to create a fully functional Tic-Tac-Toe game using Python. By the end, you'll have a game that you can play against a friend!

Now, let's write a Python program to implement Tic-Tac-Toe game:

def create_board():
    return [' '] * 9

def print_board(board):
    print(f" {board[0]} | {board[1]} | {board[2]} ")
    print("---|---|---")
    print(f" {board[3]} | {board[4]} | {board[5]} ")
    print("---|---|---")
    print(f" {board[6]} | {board[7]} | {board[8]} ")

def check_win(board, player):
    win_conditions = [
        [0, 1, 2], [3, 4, 5], [6, 7, 8], # rows
        [0, 3, 6], [1, 4, 7], [2, 5, 8], # columns
        [0, 4, 8], [2, 4, 6] # diagonals
    ]

for condition in win_conditions:
    if all(board[i] == player for i in condition):
        return True
return False

def is_board_full(board):
    return ' ' not in board

def play_game():
    board = create_board()
    players = ['X', 'O']
    current_player = 0

while True:
    print_board(board)
    player = players[current_player]
    print(f"Player {player}'s turn:")

move = int(input("Enter your move (1-9): ")) - 1
    if board[move] != ' ':
        print("Invalid move. Try again.")
    continue

board[move] = player

if check_win(board, player):
    print_board(board)
    print(f"Player {player} wins!")
break

if is_board_full(board):
    print_board(board)
    print("It's a tie!")
break

current_player = (current_player + 1) % 2

if __name__ == "__main__":

play_game()

Example Game Output:

   |   |   
---|---|---
   |   |   
---|---|---
   |   |   
Player X's turn:
Enter your move (1-9): 2
   | X |   
---|---|---
   |   |   
---|---|---
   |   |   
Player O's turn:
Enter your move (1-9): 3
   | X | O 
---|---|---
   |   |   
---|---|---
   |   |   
Player X's turn:
Enter your move (1-9): 5
   | X | O 
---|---|---
   | X |   
---|---|---
   |   |   
Player O's turn:
Enter your move (1-9): 6
   | X | O 
---|---|---
   | X | O 
---|---|---
   |   |   
Player X's turn:
Enter your move (1-9): 8
   | X | O 
---|---|---
   | X | O 
---|---|---
   | X |   
Player X wins!

Process exited - Return Code: 0 
Press Enter to exit terminal


This code implements a Tic-Tac-Toe game with the following features:
  • Creates a 3x3 board.
  • Prints the board after each move.
  • Allows players to take turns.
  • Checks for win conditions (rows, columns, diagonals).
  • Detects a tie if the board is full.
  • Provides clear output for win or tie.

Feel free to enhance this code with additional features like:
  • Player names input.
  • Best-of-n game mode.
  • AI opponent.
  • Graphical user interface.

Happy Coding !

Comments