Article

Tic-tac-toe in Python

Last updated Dec. 11, 2022

...

Tic-Tac-Toe Implemented in Python

Tic-tac-toe, also known as noughts and crosses, is a simple two-player game played on a 3x3 grid. The goal of the game is to be the first player to place three of their markers in a horizontal, vertical, or diagonal row. The game is typically played using paper and pencil, but it can also be implemented in a programming language such as Python. It is a game of strategy and skill, and it can be a fun way to pass the time. Here is an example of how you might implement a simple tic-tac-toe game in Python:

# Board is represented as a list of strings, where each string represents a row on the board
board = [" ", " ", " ",
         " ", " ", " ",
         " ", " ", " "]

# Function to print the current state of the board
def print_board():
    print(board[0] + " | " + board[1] + " | " + board[2])
    print("---------")
    print(board[3] + " | " + board[4] + " | " + board[5])
    print("---------")
    print(board[6] + " | " + board[7] + " | " + board[8])

# Function to check if the game has been won
def check_win():
    # Check horizontal wins
    if board[0] == board[1] == board[2] != " ":
        return True
    if board[3] == board[4] == board[5] != " ":
        return True
    if board[6] == board[7] == board[8] != " ":
        return True

    # Check vertical wins
    if board[0] == board[3] == board[6] != " ":
        return True
    if board[1] == board[4] == board[7] != " ":
        return True
    if board[2] == board[5] == board[8] != " ":
        return True

    # Check diagonal wins
    if board[0] == board[4] == board[8] != " ":
        return True
    if board[2] == board[4] == board[6] != " ":
        return True

    return False

# Function to check if the game has ended in a draw
def check_draw():
    for i in range(9):
        if board[i] == " ":
            return False
    return True

# Main game loop
while True:
    # Print the current state of the board
    print_board()

    # Get player X's move
    x = int(input("Player X: Choose a position (1-9): "))
    board[x - 1] = "X"

    # Check if player X has won
    if check_win():
        print_board()
        print("Player X wins!")
        break

    # Check if the game has ended in a draw
    if check_draw():
        print_board()
        print("It's a draw!")
        break

    # Print the current state of the board
    print_board()

    # Get player O's move
    o = int(input("Player O: Choose a position (1-9): "))
    board[o - 1] = "O"

    # Check if player 0 has won
    if check_win():
        print_board()
        print("Player O wins!")
        break

    # Check if the game has ended in a draw
    if check_draw():
        print_board()
        print("It's a draw!")
        break

 

Post a Comment

To leave a comment, click the button below to sign in with Google.

Signup To My Newsletter

By subscribing, you will get one email every month on tips, tutorials, and resources to improve your skills as a developer. You will get early access to my courses and videos and also access to special bonus of the time. No spam, unsubscribe at any time





Subscribe
Contact
Contact form