### GRASP CODING

PYTHON CODE | AI CODE | UI/UX CODE

Python Free Code

# Build a Tic-Tac-Toe Python Game using the minimax algorithm

Hello Guys, today we came up with the new Python Script based on the minimax algorithm of the modem and popular game “Tic Tac Toe”. As asked by some of the followers of Instagram (@_tech_tutor). In this project, we are going to show you something really interesting that you can do using PyQT library. It is a framework for creating a GUI and minimax algorithm which is also known as a backtracking algorithm.

Let’s understand the rules of the Tic Tac Toe game. Tic Tac Toe is a game in which two players search for alternative turns. To make continuous line players draw three cells horizontally, vertically, or diagonally with either three noughts and crosses or (O’s or X’s) in nine-square grid spaces. An opponent may avoid a victory by preventing the opponent’s line from being completed. Along with the Python script, we have used a minimax algorithm. It is extremely effective in fields such as Artificial Intelligence, Finance, Game Theory, Statistics, or also in Philosophy.

#### Let’s move ahead and build this minimax algorithm together. While building a minimax algorithm together a question may arise: What is a minimax algorithm?

Minimax is a kind of backtracking algorithm which is used to minimize the maximum loss and used in decision making. The philosophy of games to find the best step for a player, believing that the opponent is always playing optimally.

“You should consider the algorithm as a reflection of the cycle of the human thinking process of saying, “OK, if I make this move, then my opponent can only make two moves, and each of those would let me win. So, this is the right move to make.”

Let’s consider our game with two players one is a minimizer, and another is maximizer. Each turn of our game will have two possible moves. Let’s consider the end result to be associated with a number that the maximizer is trying to maximize and minimize tries to minimize.

#### Let’s consider a turn game. We can transform every possible scenario into a tree that is shown below:

In the above figure, we are going to give the four end values i.e. 3, 5,2, and 9. Actually, minimax expects that the player can perform every single move optimally. Initially, the maximizers go first and chose left. We have noticed that there is a 9 on the right side of the tree but we are assuming that the opponent is playing optimally. So, they were picked 2 resulting in a net loss and the minimizer goes next and picks a three.

#### Now let’s dive at pseudocode for how the minimax algorithm works.

So, the minimax function is the recursive algorithm that takes in three parameters: they are nodes, depth of the tree where the bottom of the tree is zero, and maximizing player. The first statement is the general case because we are at the end of the tree or are the terminal nodes. We return the heuristic value of the node. What this means is that it’s the value at that node like the number is 3, 5, 2, and 9 from our example. Then we go to the place of the maximizing player. Each child of this node we get the maximum value going down after this node per single direction.

Note: We have to switch turns because it’s not the turn of the minimizer and then we return the best value essentially. What we are doing is we are trying every single possible path and finding the most optimum. We do the same thing except in this case it is the minimizing players turn that can generalize to tic-tac-toe except we have a lot more children because we have a lot more moves possible.

#### A link to the completed Tic Tac Toe Python Game is given below so check that out if you’re interested.

• Orban