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.
Assume ,
Player 1 - X
Player 2 - O
So,a player who gets 3 consecutive marks first,they will win the game .
Let's have a discussion about how a board's data structure looks and how the Tic Tac Toe algorithm works.
Consider a Board having nine elements vector.Each element will contain
Move Table
It is a vector of 3^9 elements, each element of which is a nine element vector representing board position.
Total of 3^9(19683) elements in move table
Move Table
Index Current Board position New Board position
0 000000000 000010000
1 000000001 020000001
2 000000002 000100002
3 000000010 002000010
.
.
Assume ,
Player 1 - X
Player 2 - O
Let's have a discussion about how a board's data structure looks and how the Tic Tac Toe algorithm works.
Board's Data Structure:
The cells could be represent 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
Move Table
It is a vector of 3^9 elements, each element of which is a nine element vector representing board position.
Total of 3^9(19683) elements in move table
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.
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:
Hope you all understood .......
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:
Hope you all understood .......
Great man!!!! Really helpfulll... Wherever i see.. There is only algorithm. Even in the book no explanation or example is given. Here its so crystal clear.
ReplyDeletebc smjh ni aaya
DeleteExplained clearly
ReplyDeleteFrom where can I get the full moveable
ReplyDeleteThank you for such clear explanation
ReplyDeleteEasy to understand.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteVery clear explanation..
ReplyDeleteOllywood Cine news
ReplyDeleteclearly demonstrated. However python code to back it is requested.
ReplyDeletehow ai algorithms work in tic tac toe explained
ReplyDeleteOdia Film Updates
ReplyDeleteHow did you get the new board position as 000010000 in step2
ReplyDeletesame question here
Deleteread again it simple concept
DeleteVery helpful for me.....
ReplyDeleteSuperb explanation
ReplyDeleteExcellent explanation
ReplyDeleteIn basis of what are the new board positions stored in the movetable? Is there a particular method ?
ReplyDeleteThanks for giving such ease in understaning...
ReplyDeleteNice explaination
ReplyDelete