Tic Tac Toe Project with Source Code

Here is a simple Tic Tac Toe game in Python:


Source code:

# Tic Tac Toe game in Python


board = [' ' for x in range(10)]


def insert_letter(letter, position):

    board[position] = letter


def space_is_free(position):

    return board[position] == ' '


def print_board(board):

    print('   |   |   ')

    print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3] + ' ')

    print('   |   |   ')

    print('-----------')

    print('   |   |   ')

    print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6] + ' ')

    print('   |   |   ')

    print('-----------')

    print('   |   |   ')

    print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9] + ' ')

    print('   |   |   ')


def is_winner(bo, le):

    return (bo[7] == le and bo[8] == le and bo[9] == le) or \

           (bo[4] == le and bo[5] == le and bo[6] == le) or \

           (bo[1] == le and bo[2] == le and bo[3] == le) or \

           (bo[1] == le and bo[4] == le and bo[7] == le) or \

           (bo[2] == le and bo[5] == le and bo[8] == le) or \

           (bo[3] == le and bo[6] == le and bo[9] == le) or \

           (bo[1] == le and bo[5] == le and bo[9] == le) or \

           (bo[3] == le and bo[5] == le and bo[7] == le)


def player_move():

    run = True

    while run:

        move = input("Please select a position to place an 'X' (1-9): ")

        try:

            move = int(move)

            if move > 0 and move < 10:

                if space_is_free(move):

                    run = False

                    insert_letter('X', move)

                else:

                    print("Sorry, this space is occupied!")

            else:

                print("Please type a number within the range!")

        except:

            print("Please type a number!")


def computer_move():

    possible_moves = [x for x, letter in enumerate(board) if letter == ' ' and x != 0]

    move = 0


    for letter in ['O', 'X']:

        for i in possible_moves:

            board_copy = board[:]

            board_copy[i] = letter

            if is_winner(board_copy, letter):

                move = i

                return move


    corners_open = []

    for i in possible_moves:

        if i in [1, 3, 7, 9]:

            corners_open.append(i)

    if len(corners_open) > 0:

        move = select_random(corners_open)

        return move


    if 5 in possible_moves:

        move = 5

        return move


    edges_open = []

    for i in possible_moves:

        if i in [2, 4, 6, 8]:

            edges_open.append(i)

    if len(edges_open) > 0:

        move = select_random(edges_open)

        return move


def select_random(li):

    import random

Post a Comment

Previous Post Next Post