Code for Tic Tac Toe in Python: Program the Game Easiest Way

Recommended skills: if/else, loops, functions

# creating the board
board = [
    ["_", "_", "_"],
    ["_", "_", "_"],
    ["_", "_", "_"]
]

# function for printing the board
def printBoards(board):
    print("\n")
    print("\t     |     |")
    print("\t  {}  |  {}  |  {}".format(board[0][0], board[0][1], board[0][2]))
    print('\t_____|_____|_____')
    print("\t     |     |")
    print("\t  {}  |  {}  |  {}".format(board[1][0], board[1][1], board[1][2]))
    print('\t_____|_____|_____')
    print("\t     |     |")
    print("\t  {}  |  {}  |  {}".format(board[2][0], board[2][1], board[2][2]))
    print("\t     |     |")
    print("\n")

# function for checking the winner
def checkWinner(currPlayer, board):
    # checking for the winner in the rows
    for row in range(0,3):
        if board[row][0]==board[row][1]==board[row][2] and board[row][0] != '-':
            if board[row][0]==currPlayer:
                print("{} is winner".format(currPlayer))
                return True

    # checking for the winner in the columns
    for col in range(0,3):
        if board[0][col]==board[1][col]==board[2][col] and board[0][col] != '-':
            if board[0][col]==currPlayer:
                print("{} is winner".format(currPlayer))
                return True

    # checking for the winner in one diagonal
    if board[0][0]==board[1][1]==board[2][2] and board[0][0] !='-':
        if board[0][0]==currPlayer:
            print("{} is winner".format(currPlayer))
            return True

    # checking for the winner in another diagonal
    if board[0][2]==board[1][1]==board[2][0] and board[0][2]!='-':
        if board[0][2]==currPlayer:
            print("{} is winner".format(currPlayer))
            return True
    return False

# printing board for the first time
printBoards(board)

# initiating rows and columns for the board
col = 0
row = 0

# initiating player turn
playerTurn = "X"

# main loop for running the game steps untill all spots filled
for counter in range(1, 10):
    validMove = False
    while (validMove == False):
        col = 0
        row = 0

        while (col < 1 or col > 3):
            col = int(input(playerTurn + " player, select a column 1-3: "))
            if (col < 1 or col > 3):
                print("The column must be between 1 and 3.")

        while (row < 1 or row > 3):
            row = int(input(playerTurn + " player, select a row 1-3: "))
            if (row < 1 or row > 3):
                print("The row must be between 1 and 3.")

        col -= 1
        row -= 1

        if board[row][col] == "_":
            board[row][col] = playerTurn
            validMove = True
        else:
            print("Oops, That spot was already taken. Please select another spot.")

    # printing the board after each player gives an input
    printBoards(board)
    if (checkWinner(playerTurn, board)):
        break
    
    # switching player after each input
    if playerTurn == "X":
        playerTurn = "O"
    else:
        playerTurn = "X"

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top