Python Tic-Tac-Toe Game Program Code

In this source code example, we will write a code to implement basic Tic-Tac-Toe game in Python.

Python Tic-Tac-Toe Game Program Code

Here's a basic implementation of the Tic-Tac-Toe game in Python:
board = [" " for x in range(9)]

def print_board():
    row1 = "| {} | {} | {} |".format(board[0], board[1], board[2])
    row2 = "| {} | {} | {} |".format(board[3], board[4], board[5])
    row3 = "| {} | {} | {} |".format(board[6], board[7], board[8])

    print()
    print(row1)
    print(row2)
    print(row3)
    print()

def player_move(icon):
    if icon == "X":
        number = 1
    elif icon == "O":
        number = 2
    print("Your turn player {}".format(number))
    choice = int(input("Enter your move (1-9): ").strip())
    if board[choice - 1] == " ":
        board[choice - 1] = icon
    else:
        print()
        print("That space is already taken!")

def is_victory(icon):
    if (board[0] == icon and board[1] == icon and board[2] == icon) or \
       (board[3] == icon and board[4] == icon and board[5] == icon) or \
       (board[6] == icon and board[7] == icon and board[8] == icon) or \
       (board[0] == icon and board[3] == icon and board[6] == icon) or \
       (board[1] == icon and board[4] == icon and board[7] == icon) or \
       (board[2] == icon and board[5] == icon and board[8] == icon) or \
       (board[0] == icon and board[4] == icon and board[8] == icon) or \
       (board[2] == icon and board[4] == icon and board[6] == icon):
        return True
    else:
        return False

def is_draw():
    if " " not in board:
        return True
    else:
        return False

while True:
    print_board()
    player_move("X")
    print_board()
    if is_victory("X"):
        print("X wins! Congratulations!")
        break
    elif is_draw():
        print("It's a draw!")
        break
    player_move("O")
    if is_victory("O"):
        print_board()
        print("O wins! Congratulations!")
        break
    elif is_draw():
        print("It's a draw!")
        break

Output:

|   |   |   |
|   |   |   |
|   |   |   |

Your turn player 1
Enter your move (1-9): 2

|   | X |   |
|   |   |   |
|   |   |   |

Your turn player 2
Enter your move (1-9): 1

| O | X |   |
|   |   |   |
|   |   |   |

Your turn player 1
Enter your move (1-9): 5

| O | X |   |
|   | X |   |
|   |   |   |

Your turn player 2
Enter your move (1-9): 8

| O | X |   |
|   | X |   |
|   | O |   |

Your turn player 1
Enter your move (1-9): 9

| O | X |   |
|   | X |   |
|   | O | X |

Your turn player 2
Enter your move (1-9): 4

| O | X |   |
| O | X |   |
|   | O | X |

Your turn player 1
Enter your move (1-9): 6

| O | X |   |
| O | X | X |
|   | O | X |

Your turn player 2
Enter your move (1-9): 7

| O | X |   |
| O | X | X |
| O | O | X |

O wins! Congratulations!

Explanation:

  • board is a list that represents the game board. Each element in the list represents a cell on the board, with an empty string (" ") representing an empty cell.
  • The print_board function prints the current state of the board by formatting the contents of the board list into a Tic-Tac-Toe grid.
  • The player_move function takes the player's icon (either X or O) as input and allows the player to enter their move by choosing a number between 1 to 9. The chosen space is then updated with the player's icon.


Comments