DEV Community

Cover image for Tic-tac-toe, Human vs Machine (AI)
Dev Anand
Dev Anand

Posted on

Tic-tac-toe, Human vs Machine (AI)

Ultimate challenge of human intelligence against artificial intelligence!

Today, we will witness a battle of wits between a human and an AI opponent in one of the simplest yet most beloved games of all time: Tic-Tac-Toe.

'tic-tac-toe-image'

Tic-tac-toe and AI

Tic-Tac-Toe is a classic game that has been played by generations of children and adults alike. It's a game that is easy to learn, but difficult to master.

However, with the advent of AI (Artificial Intelligence), even the most skilled human players may find themselves struggling to keep up with their electronic counterparts.

It's important to first gain experience by coding the game to play against a human opponent.

Related Article: Tic-tac-toe, Human vs Human.

Keep calm coding begins....

Tic-tac-toe in Python

Grid box pattern

The game_grid(value) function takes a list value containing the current state of the Tic-Tac-Toe game board as input.

def game_grid(value):  
    print("\n")  
    print("\t____________________")  
    print("\t|      |      |     |")  
    print("\t|    {} |  {}   |  {}  |".format(value[0], value[1], value[2]))  
    print('\t|______|______|_____|')  
    print("\t|      |      |     |") 
    print("\t|   {}  |  {}   |  {}  |".format(value[3], value[4], value[5]))  
    print('\t|______|______|_____|')  
    print("\t|      |      |     |")  
    print("\t|  {}   |  {}   |  {}  |".format(value[6], value[7], value[8]))  
    print('\t|______|______|_____|')
    print("\n") 
Enter fullscreen mode Exit fullscreen mode

Possible winning patterns

The code defines a list called wining_pattern which contains sub-lists of all the possible winning combinations in a Tic-Tac-Toe game.

wining_pattern = [

    [1,2,3],
    [4,5,6],
    [7,8,9],
    [1,4,7],
    [2,5,8],
    [3,6,9],
    [1,5,9],
    [3,5,7]

]
Enter fullscreen mode Exit fullscreen mode

Variables initialisation

These variables are used to keep track of the state of the game, including which positions on the board have been taken and by whom.

taken_moves = []
default_values = [' ' for i in range(9)]
players_moves = {
    'X_moves': [],
    'O_moves': []
}
players = ['X', 'O']
Enter fullscreen mode Exit fullscreen mode

Update Game state

The update_game function updates the game state based on the player's move and returns a visual representation of the updated game board.

def update_game(move, player):
    taken_moves.append(move)
    players_moves[player + '_moves'].append(move)
    default_values[move - 1] = player
    return game_grid(default_values)
Enter fullscreen mode Exit fullscreen mode

Decide winner

On the basis of taken steps, recorded inside players_moves dictionary, check_wining will decide the winner.

def check_wining(moves):
    for i in wining_pattern:
        if all(j in moves for j in i):
            return True 
    return False 
Enter fullscreen mode Exit fullscreen mode

How machine will play

AI First move

The initial_move() used to determine the first move made by the machine when playing tic-tac-toe against a human player.

def initial_move():
    #Acquire Center
    if 5 not in players_moves['X_moves']:
        return 5
    #Acquire any corner
    return random.choice([1,3,7,9])
Enter fullscreen mode Exit fullscreen mode

AI Smart move 🤓

The smart_move() tries to find the best possible move for the player by analyzing the current state of the game and searching for potential winning slots.

def smart_move(mine_move, opp_moves):
    next_move = []
    for i in winning_pattern:
        if not [x for x in mine_move if x in i]:
            for j in i:
                if j not in opp_moves:
                    next_move.append(j)
            if len(next_move) == 1:
                return next_move[0]
            next_move = []
    return False
Enter fullscreen mode Exit fullscreen mode

AI Winning move 🏆

The winning_move() checks if there is a possible winning move for the player and returns that move if there is, otherwise returns None.

def winning_move():
    moves = smart_move(players_moves['X_moves'], players_moves['O_moves'])
    return moves if moves else None
Enter fullscreen mode Exit fullscreen mode

AI Random Edge move

The random_edge_move() returns the first available edge slot on the board, starting from the top-left and moving clockwise, if there is one.

def random_edge_move():
    for i in [2,4,6,8]:   
        if i not in taken_moves:
            return i
Enter fullscreen mode Exit fullscreen mode

Driver Code - AI vs Human

It plays a game between a human player (X) and the computer (O), using a combination of pre-defined strategies and randomness to determine the computer's moves.

step = 0
game_grid(default_values)
while step < 9 and step >= 0:

    if step >= 4:
        print('Match tied!')
        break

    player = "X"
    try:
        move = int(input(player + " : "))
        if move < 10 and move not in taken_moves:
            step += 1
            update_game(move,player)
            winner = check_winning() if step > 1 else None
            if winner:
                print(winner, 'Won!')
                break
            elif step == 1:
                O_move = initial_move()
            else:
                O_move = winning_move()
                O_move = smart_move(players_moves['O_moves'], players_moves['X_moves']) if not O_move else O_move
                O_move = random_edge_move() if not O_move else O_move
            print("O (Python 🐍): ",O_move)
            player = "O"
            update_game(O_move, player)
            winner = check_winning() if step > 1 else None
            if winner:
                print(winner, 'Won!')
                break
            continue
        print("Invalid move! - either your move is too high or already taken.")
    except:
        print("Invalid move! - wrong input.")
Enter fullscreen mode Exit fullscreen mode

Example testing

  • X = Human Player
  • O = AI (Computer)

'tic-tac-toe-ai-code-output'

I understand it's difficult at once, to understand the code, please refers: speakpython.codes/tic-tac-toe-human-vs-machine - for complete explanation.


This article is contributed by speakpython.codes

Top comments (0)