# Square Tic Tac Toe AI

In this final section, you'll see how to code a smarter computer move. Another big difference is that the game logic and GUI are separated into different classes. Benefits include easier unit testing and extending the GUI to handle multiple games, etc.

There are various approaches you can follow to code an intelligent computer player depending upon the requirements, see wikipedia: Game artificial intelligence for examples. A weight based solution is presented here.

Quoting from wikipedia: Artificial intelligence in video games:

AI in video games is a distinct subfield and differs from academic AI. It serves to improve the game-player experience rather than machine learning or decision making.

However, "game AI" does not, in general, as might be thought and sometimes is depicted to be the case, mean a realization of an artificial person corresponding to an NPC, in the manner of say, the Turing test or an artificial general intelligence.

## Weight based algorithm

Minimax is one of the popular algorithms to implement an AI for Tic Tac Toe. Here's some resources to get started:

The algorithm presented here borrows a few things from Minimax, but decisions are based on current state of the game alone. So, there's no need for recursive calculations and other complexities related to the number of future moves. Here's a rough explanation of the algorithm:

• Loop over all the valid squares, which is 20 squares for a 4x4 board.
• If all the corners of a square are empty, each empty cell gets `1` weight for both the players.
• If a particular square has moves from both the user and the AI, the empty cells (if any) won't get any weight addition.
• If a particular square has moves only from one player, find the total (`t`) number of moves (possible values `1` to `3`), square this total and add `1` more. This value gets added to each empty cell of this square for that particular player only.
• `t * t + 1` will thus work for all corners empty case as well.
• I wanted to use a formula that grows exponentially with number of moves already made. Squaring fits thematically with the game name and seems to work well enough for this game.

Here's the initial weights for all the cells. Since no player has made a move yet, this will apply for both the players. Also, the numbers will be exactly equal to the number of possible squares from that particular cell.

``````3  5  5  3
5  7  7  5
5  7  7  5
3  5  5  3
``````

Here's a screenshot where the user has made 3 moves, and the AI has to make the next move. The user and AI weights for all the empty cells are also shown for reference.

Here's some weight calculations for two of the empty cells:

• User at index 4
• As seen from initial weight matrix, there are `5` possible squares from index `4`.
• For the given game situation, `(4, 5, 8, 9)` has mixed moves, user at index `5` and AI at index `9`. Similarly, `(1, 4, 6, 9)` has user at index `1` and AI at index `9`.
• The three remaining squares that the user can possibly form are — `(0, 1, 4, 5)`, `(4, 6, 12, 14)` and `(2, 4, 11, 13)`.
• `(0, 1, 4, 5)` has two moves already made, so weight to add is `2 * 2 + 1`.
• `(4, 6, 12, 14)` has one move already made, so weight to add is `1 * 1 + 1`.
• `(2, 4, 11, 13)` has no moves so far, so weight to add is `0 * 0 + 1`.
• Hence, the total user weight for index `4` is `5 + 2 + 1` which is `8`.
• AI at index 4
• Only one square `(2, 4, 11, 13)` is possible for the AI, so the total AI weight for index 4 is `1`.
• User at index 2
• Winning possibilities — `(1, 2, 5, 6)`, `(2, 3, 6, 7)` and `(2, 4, 11, 13)`.
• User weights, respectively — `2 * 2 + 1`, `0 * 0 + 1` and `0 * 0 + 1` which comes to `7` in total.
• AI at index 2
• Winning possibilities — `(0, 2, 8, 10)`, `(2, 3, 6, 7)` and `(2, 4, 11, 13)`.
• AI weights, respectively — `1 * 1 + 1`, `0 * 0 + 1` and `0 * 0 + 1` which comes to `4` in total.

The full decision algorithm will be explained later. In this particular game situation:

• As seen from the illustration above, user has maximum weight of `8` at index `4`, `6` and `7`.
• AI has maximum weight of `4` at index `2` and `8`.
• AI will need to choose among the three indexes with maximum user weights. AI will try to maximize its own chances. AI weights are `1`, `3` and `3` for those three user indexes respectively. So, the final choice will be randomly picked between indexes `6` and `7`.

## Code

First, the GUI portion, which is also the main program.

``````# square_gui.py
from square_ai import Square
import tkinter as tk

class Root(tk.Tk):
def __init__(self):
super().__init__()

self.title('Square Tic Tac Toe')
self.geometry('500x450')

self.char_x = tk.PhotoImage(file='./char_x.png')
self.char_o = tk.PhotoImage(file='./char_o.png')
self.empty = tk.PhotoImage()

self.ai = {'bg': 'orange', 'image': self.char_x}
self.user = {'bg': 'grey', 'image': self.char_o}
self.board_bg = 'white'

self.sq = Square()
self.create_first_move_frame()
self.create_difficulty_frame()
self.create_control_frame()

def create_first_move_frame(self):

self.move_choice = tk.IntVar()
self.move_choice.set(self.sq.user['value'])
variable=self.move_choice, value=self.sq.ai['value']
).pack(side=tk.LEFT)
variable=self.move_choice, value=self.sq.user['value']
).pack(side=tk.RIGHT)

def create_difficulty_frame(self):
self.difficulty_frame = tk.Frame()

tk.Label(self.difficulty_frame, text='Difficulty').pack(side=tk.LEFT)
self.difficulty_choice = tk.IntVar()
self.difficulty_choice.set(self.sq.easy)
variable=self.difficulty_choice, value=self.sq.easy
).pack(side=tk.LEFT)
variable=self.difficulty_choice, value=self.sq.hard
).pack(side=tk.RIGHT)

def create_control_frame(self):
self.control_frame = tk.Frame()

self.b_quit = tk.Button(self.control_frame, text='Quit',
command=self.quit)
self.b_quit.pack(side=tk.LEFT)

self.b_play = tk.Button(self.control_frame, text='Play',
command=self.play)
self.b_play.pack(side=tk.RIGHT)

def create_status_frame(self):
self.status_frame = tk.Frame()
self.status_frame.pack(expand=True)

tk.Label(self.status_frame, text='Status: ').pack(side=tk.LEFT)
self.l_status = tk.Label(self.status_frame)
self.l_status.pack(side=tk.RIGHT)

def create_board_frame(self):
self.board_frame = tk.Frame()
self.board_frame.pack(expand=True)

self.sq.reset_board(self.difficulty_choice.get())
self.cell = [None] * self.sq.total_cells
for i in range(self.sq.total_cells):
self.cell[i] = tk.Label(self.board_frame, highlightthickness=1,
width=60, height=60, bg=self.board_bg,
image=self.empty)
self.cell[i].bind('<Button-1>',
lambda e, move=i: self.user_click(e, move))
r, c = divmod(i, self.sq.corners)
self.cell[i].grid(row=r, column=c)

def play(self):
self.b_play['state'] = 'disabled'
if self.b_play['text'] == 'Play':
self.create_status_frame()
self.b_play['text'] = 'Play Again'
else:
self.board_frame.destroy()
self.create_board_frame()
self.l_status['text'] = self.sq.active
self.last_click = 0
if self.move_choice.get() == self.sq.ai['value']:
self.ai_click()

def quit(self):
self.destroy()

def user_click(self, e, user_move):
if self.sq.board[user_move] != 0 or self.sq.state != self.sq.active:
return
self.sq.set_user_move(user_move)
self.update_cell(self.user, user_move)
if self.sq.state == self.sq.active:
self.ai_click()

def ai_click(self):
ai_move = self.sq.get_ai_move()
self.update_cell(self.ai, ai_move)

def update_cell(self, player, move):
self.cell[self.last_click]['bg'] = self.board_bg
self.last_click = move
self.cell[move]['image'] = player['image']
self.cell[move]['bg'] = player['bg']
self.l_status['text'] = self.sq.state
if self.sq.state != self.sq.active:
self.b_play['state'] = 'normal'
if self.sq.state != 'TIE':
self.highlight_winning_squares(player)

def highlight_winning_squares(self, player):
for square in self.sq.winning_squares:
for i in square:
self.cell[i]['bg'] = player['bg']

if __name__ == '__main__':
root = Root()
root.mainloop()
``````

And here's the class which implements the game logic:

``````# square_ai.py
import random

class Square():
def __init__(self):
self.active = 'GAME ACTIVE'
self.total_cells = 16
self.corners = 4
self.easy, self.hard = (0, 1)
self.ai = {'value': 1, 'win': 'AI WINS'}
self.user = {'value': self.corners+1, 'win': 'USER WINS'}
self.max_ai_sum = (self.corners-1) * self.ai['value']
self.max_user_sum = (self.corners-1) * self.user['value']
self.all_squares = ((0, 1, 4, 5), (1, 2, 5, 6), (2, 3, 6, 7),
(4, 5, 8, 9), (5, 6, 9, 10), (6, 7, 10, 11),
(8, 9, 12, 13), (9, 10, 13, 14), (10, 11, 14, 15),
(0, 2, 8, 10), (1, 3, 9, 11), (4, 6, 12, 14),
(5, 7, 13, 15), (0, 3, 12, 15), (1, 4, 6, 9),
(2, 5, 7, 10), (5, 8, 10, 13), (6, 9, 11, 14),
(1, 7, 8, 14), (2, 4, 11, 13))

def reset_board(self, difficulty):
self.board = [0] * self.total_cells
self.remaining_moves = list(range(self.total_cells))
self.state = self.active
self.difficulty = difficulty

def set_user_move(self, move):
self.update_board(self.user, move)

def get_ai_move(self):
if self.difficulty == self.easy:
move = random.choice(self.remaining_moves)
else:
move = self.ai_hard_move()
self.update_board(self.ai, move)
return move

def update_board(self, player, move):
self.board[move] = player['value']
self.remaining_moves.remove(move)
self.update_status(player)

def update_status(self, player):
winner_sum = self.corners * player['value']
self.winning_squares = []
for square in self.all_squares:
if sum(self.board[i] for i in square) == winner_sum:
self.state = player['win']
self.winning_squares.append(square)
if self.state == self.active and not self.remaining_moves:
self.state = 'TIE'

def ai_hard_move(self):
self.update_weights()

# making a winning move or block a winning move
if self.ai_winning_indexes:
return random.choice(self.ai_winning_indexes)
elif self.user_winning_indexes:
return random.choice(self.user_winning_indexes)

# if there are no possible squares left, return a random move
max_user_weight = max(self.user_weights)
max_ai_weight = max(self.ai_weights)
if max_user_weight == 0 and max_ai_weight == 0:
return random.choice(self.remaining_moves)

# there can be multiple indexes with max weight
def max_moves(seq, val):
return [i for i,w in enumerate(seq) if w == val]
max_user_moves = max_moves(self.user_weights, max_user_weight)
max_ai_moves = max_moves(self.ai_weights, max_ai_weight)

# randomize multiple indexes and choose best move based on weights
if max_user_weight > max_ai_weight:
random.shuffle(max_user_moves)
return max(max_user_moves, key=lambda x: self.ai_weights[x])
else:
random.shuffle(max_ai_moves)
return max(max_ai_moves, key=lambda x: self.user_weights[x])

def update_weights(self):
def update(s, w, t, ot):
for i in square:
if self.board[i] == 0:
w[i] += t * t + 1
if ot == self.max_ai_sum:
self.ai_winning_indexes.append(i)
elif ot == self.max_user_sum:
self.user_winning_indexes.append(i)

self.user_weights = [0] * self.total_cells
self.ai_weights = [0] * self.total_cells
self.user_winning_indexes = []
self.ai_winning_indexes = []
for square in self.all_squares:
total = sum(self.board[i] for i in square)
if total == 0:
update(square, self.user_weights, 0, 0)
update(square, self.ai_weights, 0, 0)
elif total <= self.max_ai_sum:
update(square, self.ai_weights, total, total)
else:
q, r = divmod(total, self.user['value'])
if r == 0:
update(square, self.user_weights, q, total)
``````

## Layout changes

A new frame to choose between `Easy` and `Hard` difficulty level has been added. When `Easy` mode is chosen, the AI will make random moves. The weight based algorithm will come into play when `Hard` mode is active.

## Weight based decision making

Earlier, you saw one example of AI choosing the next move to be made. Here's the complete decision making possibilities explained:

• In addition to calculating the weights, the `update_weights()` method also creates two lists to save indexes with three moves already made.
• If AI has squares with three moves done, choose a random move among such indexes. This will result in AI winning.
• Else, if user has squares with three moves done, again choose a random move. If there were multiple such indexes, user can win in the next move. User winning is possible with the current algorithm if the very first move is made by the user.
• If there are no winning moves, first check if there are any winning squares left at all. If none are remaining, return a random move.
• Only two possible choices are left — user has higher maximum weight and AI has equal to or higher maximum weight. Also, there cannot be any square with three moves made by the same player, since that case is already covered.
• As seen earlier, there can be multiple indexes with the same maximum weight.
• When user has the higher maximum weight, AI needs to choose the index where its own weights are the best.
• When AI has equal to or higher maximum weight, the index where user's weight is the most is chosen so that user's future chances are reduced.