0% found this document useful (0 votes)
130 views17 pages

Tic-Tac-Toe Problem

Tic Tac Toe problem in AI

Uploaded by

Omkar Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views17 pages

Tic-Tac-Toe Problem

Tic Tac Toe problem in AI

Uploaded by

Omkar Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Tic-Tac-Toe Problem

• The game Tic Tac Toe is also known as Noughts and Crosses or Xs and Os ,the player needs
to take turns marking the spaces in a 3x3 grid with their own marks, if 3 consecutive marks
(Horizontal, Vertical, Diagonal) are formed then the player who owns these moves get won.
• In order to solve Tic Tac Toe, we need to go deeper than just to think about it as a game where
two players place X’s and O’s on the board.
• Formally speaking, Tic Tac Toe is a zero-sum and perfect information game. It means that
each participant’s gain is equal to the other participants’ losses and we know everything about
the current game state.
• In a two-player (A vs B) game, if player A scores x points (utility units), player B loses x
points. Total gains/losses always sum up to 0.
• Assume ,
• Player 1 - X
• Player 2 - O
• So,a player who gets 3 consecutive marks first, they will win the game .
• Let's discuss how a board's data structure looks and how the Tic Tac Toe algorithm works.
• Board's Data Structure:

• The cells could be represented as Center square, Corner, Edge as like below
• Number each square starts from 1 to 9 like following image

• Consider a Board having nine elements vector. Each element will contain
• 0 for blank
• 1 indicating X player move
• 2 indicating O player move
• Computer may play as X or O player. First player is always X.
• Move Table
• It is a vector of 3^9 elements, each element of which is a nine element vector representing board
position.
• Move Table
Index Current Board position New Board position
0 000000000 000010000
1 000000001 020000001
2 000000002 000100002
3 000000010 002000010
.
.
• Algorithm
• To make a move, do the following:
• View the vector (board) as a ternary number and convert it to its corresponding decimal
number.
• Use the computed number as an index into the move table and access the vector stored there.
• The vector selected in step 2 represents the way the board will look after the move that should
be made. So set board equal to that vector.
• Let's start with empty board

• Step 1:Now our board looks like 000 000 000 (tenary number) convert it into decimal no.The
decimal no is 0
• Step 2:Use the computed number ie 0 as an index into the move table and access the vector
stored in
• New Board Position.
• The new board position is 000 010 000
• Step 3:The vector selected in step 2(000 010 000 ) represents the way the board will look after
the move that should be made. So set board equal to that vector.
• After complete the 3rd step your board looks like\
This process continues until the player get win or tie.
Flowchart:
Start

Ask for Letter Decide who


goes first

Player Turn Computer Turn

Show the board Get Computer’s


move

Get Player’s
Check if
move
Computer’s won

Check if Player
won

Check for tie


Check for tie

Ask Player to
play again END
• When the board becomes populated, the best possible move pops out to our eyes.

• Let’s use this populated board as our starting point. Let's decide that the next move is ours, and
that our symbol is an “X”.
• Let’s try to identify the best possible move with the tools we already have. There are 3 empty
cells that correspond with 3 possible moves. Let's check the result for each of these options.
• We can do this by iterating over the possible moves, and for each of them:
1. Create a new board
2. Add our symbol to the corresponding empty cell
3. Send this board to the get result function
Move 1 Move 2 Move 3
• From the 3 boards in the figure above, when we send the second board to the getresult
function, we will receive our trophy.
• Please concentrate for the next essential steps:
• We need to grade the possible moves so we can compare them. Let’s decide that if a move
yields a winning board we will grade it 1. If it yields a losing board it will receive the grade of
-1. A tie will receive a grade of 0.
• Move 2 will receive a grade of 1. When we find a move graded with 1 we can ignore all other
possible moves. There is no other better possible move than a definite victory.
• But for the sake of understanding, how would we grade moves 1 or 3, or any other move with
an incomplete result?
• Let’s Focus on move 3. The solution is to send the corresponding board recursively to the
getBestMove function.
• You might be thinking, “But wait! Our opponent plays the next move.” That’s right. Let’s find
• Our opponent has only two possible moves:

Move 3–1 Move 3–2


• Move 3–1 will win the game in favor of our opponent. Since we are using the exact same
getBestMove function, Move 3–1 will receive a grade of 1.
• This might be a bit confusing as both our victory and our loss will receive grades of 1. We
need to remember that this function call belongs to our opponent, and his victory is our loss
and vice versa.
• We must negate any grade returned to the getBestMove function by the getBestMove
function.
• Move 3–1 receives a grade of 1. The getBestMove function returns a grade of 1, and we can
grade Move 3 with a -1.
• In this manner, the getBestMove function continues to explore moves and consequent moves.
This process will continue until:
• It finds a move graded with 1, in which case it will return the move immediately
• It will continue until each possible move has a grade. The possible moves (with grades 0 and
-1) are stored in an array
The array will then be:
[a] randomized
[b] sorted from high to low
[c] the first element will be returned
These steps guarantee that:
1. A losing move will be avoided unless it’s the only option
2.The computer player can play diversely
• Unbeatable Tic Tac Toe AI
• Let’s look at the following board state. We will try to solve it as X.

• How would you solve it?


• I guess that would you think that the best move in such a scenario would be to place an X in
the middle of the board and win the game.
• And you would be correct, but is this the only winning solution for X? How to derive this
solution?
• In order to determine this, let’s draw a tree of all possible board states.
Search tree for 0.0 state and X’s turn
• As you can see above, starting from the initial state 0.0, we have 3 possibilities (1.0, 1.1, 1.2).
• 1.0 gives us a win (+1), but let’s explore other paths as well.
• 1.1 gives our opponent 2 possibilities (2.0, 2.1). 2.0 is a winning state for our opponent, so it’s a
losing state for us (-1). 2.1 gives only one possibility 3.0 in which we are winning (+1).
• 1.2 gives our opponent 2 possibilities (2.2, 2.3). 2.2 is a winning state for our opponent, so it’s a
losing state for us (-1). 2.3 gives only one possibility 3.1 in which we are winning (+1).
• Okay, but how can we interpret this?
• Let’s start from the terminal states at the bottom and calculate the minimax scores.
• At the depth 3 we are maximizing, so we are propagating +1 scores to the previous moves at the
depth 2.
• At the depth 2 we are minimizing so we are propagating -1 scores to the previous moves at the
depth 1.
• At the depth 1 we are maximizing so we are propagating +1 to the previous move at the depth 0.
• Ultimately at the depth 0, where we actually are, we should pick the move associated with the
+1 score we’ve ended up with.
• Tic Tac Toe AI would decide to go to the 1.0 node and win the game.
• Our Tic Tac Toe AI performs such simulations for every move thus making itself an unbeatable
opponent.
• Minimax algorithm that allowed us to create an unbeatable Tic Tac Toe AI agent. Minimax is a
very powerful and universal algorithm that can be applied in a wide variety of applications.

You might also like