SlideShare a Scribd company logo
1 of 15
Tic Tac Toe Game
• Tic-tac-toe (also known as noughts and
crosses or Xs and Os) is a paper-and-pencil
game for two players, X and O, who take turns
marking the spaces in a 3×3 grid.
• The player who succeeds in placing three of their
marks in a horizontal, vertical, or diagonal row
wins the game.
• Players soon discover that the best play from both
parties leads to a draw.
• Hence, tic-tac-toe is most often played by young
children.
Data members and functions used
• char matrix[3][3]; //intitial matrix declaration
• char check(void); // checks for someone to win
• void init_matrix(void); //initializes 3*3 matrics
• void get_player_move(void); //get player’s move
• void get_computer_move(void); //get computer’s
move using AI
• void disp_matrix(void); // display matrics after
each move
1. board data structure
• The board matrix (3*3) contains 3 arrays, each
representing a row.
• Each row array contains 3 character or string
elements.
• These elements are either:
– “ ” as an empty string, representing an empty cell
– “X” representing the X player
– “O” representing the O player or computer as a
player
2. check() function
• At any given state, the board will be in one and
one only of these possible states:
– Incomplete
– Player X won
– Player O won(or computer Won)
– or a tie
• The check() function receives a board array,
iterates over all the rows, through all the columns
and across both diagonals.
• It checks the succession of symbols.
• Then it lets us know the current state of that
board.
3. get_computer_move() & get_player_move()
Function
• When the board is empty it is very difficult to
identify the best possible move.
What is
best possible move?
X: - 000 000 000
O: - 000 000 000
• When the board becomes populated, the best possible
move pops out to our eyes.
• Let’s use this populated board as our starting point. Lets
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.
• Lets check the result for each of these options.
X: - 000 110 010
O: - 110 000 100
• From the 3 boards in the figure above, when we send the second
board to the check() function, we will receive our trophy.
Move-1 Move-2 Move-3
• We can do this by iterating over the possible moves, and for
each of them:
 Create a new board
 Add our symbol to the corresponding empty cell
 Send this board to the check() function
X: - 001 110 010
O: - 110 000 100
X: - 000 111 010 X: - 000 110 011
O: - 110 000 100 O: - 110 000 100
Please concentrate for the next essential steps:
1. 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.
2. 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.
3. But for the sake of understanding, how would we
grade moves 1 or 3, or any other move with an
incomplete result?
A (partial) game tree for the game of tic-tac-toe. The top node is the initial state, and MAX
moves first, placing an X in an empty square. We show part of the tree, giving alternating
moves by MIN ( 0) and MAX (X), until we eventually reach terminal states, which can be
assigned utilities according to the rules of the game.
• Let’s Focus on move 3. The solution is to send the
corresponding board recursively to get_computer_move()
and get_player_move() function.
• You might be thinking, “But wait! Our opponent plays the
next move.” That’s right. Let’s find out what grade our
opponent gets for his best future move.
• 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
get_player_move 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.
• Move 3–1 receives a grade of 1 and Move 3 will
receive a grade of -1.
• In this manner, these 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:
– A losing move will be avoided unless it’s the only option
– The computer player can play diversely
Tic Tac Toe

More Related Content

What's hot

Tic tac toe on c++ project
Tic tac toe on c++ projectTic tac toe on c++ project
Tic tac toe on c++ projectUtkarsh Aggarwal
 
Android application - Tic Tac Toe
Android application - Tic Tac ToeAndroid application - Tic Tac Toe
Android application - Tic Tac ToeSarthak Srivastava
 
Tic Tac Toe Java Development
Tic Tac Toe Java DevelopmentTic Tac Toe Java Development
Tic Tac Toe Java Developmentpengqia chen
 
Snake game implementation in c
Snake game implementation in cSnake game implementation in c
Snake game implementation in cUpendra Sengar
 
Tic tac toe c++ project presentation
Tic tac toe c++ project presentationTic tac toe c++ project presentation
Tic tac toe c++ project presentationSaad Symbian
 
project on snake game in c language
project on snake game in c languageproject on snake game in c language
project on snake game in c languageAshutosh Kumar
 
Game Tree ( Oyun Ağaçları )
Game Tree ( Oyun Ağaçları )Game Tree ( Oyun Ağaçları )
Game Tree ( Oyun Ağaçları )Alp Çoker
 
Snake project report
Snake project reportSnake project report
Snake project reportManju Rajput
 
Minmax Algorithm In Artificial Intelligence slides
Minmax Algorithm In Artificial Intelligence slidesMinmax Algorithm In Artificial Intelligence slides
Minmax Algorithm In Artificial Intelligence slidesSamiaAziz4
 
Introduction to Game Development and the Game Industry
Introduction to Game Development and the Game IndustryIntroduction to Game Development and the Game Industry
Introduction to Game Development and the Game IndustryNataly Eliyahu
 
Adversarial Search
Adversarial SearchAdversarial Search
Adversarial SearchMegha Sharma
 
report on snake game
report on snake game report on snake game
report on snake game azhar niaz
 

What's hot (20)

Tic tac toe on c++ project
Tic tac toe on c++ projectTic tac toe on c++ project
Tic tac toe on c++ project
 
Tic tac toe
Tic tac toeTic tac toe
Tic tac toe
 
Android application - Tic Tac Toe
Android application - Tic Tac ToeAndroid application - Tic Tac Toe
Android application - Tic Tac Toe
 
Tic Tac Toe Java Development
Tic Tac Toe Java DevelopmentTic Tac Toe Java Development
Tic Tac Toe Java Development
 
Tic Tac Toe ppt
Tic Tac Toe pptTic Tac Toe ppt
Tic Tac Toe ppt
 
Tic toc game presentation
Tic toc game presentationTic toc game presentation
Tic toc game presentation
 
Tic Tac Toe
Tic Tac ToeTic Tac Toe
Tic Tac Toe
 
Snake game implementation in c
Snake game implementation in cSnake game implementation in c
Snake game implementation in c
 
Tic tac toe c++ project presentation
Tic tac toe c++ project presentationTic tac toe c++ project presentation
Tic tac toe c++ project presentation
 
project on snake game in c language
project on snake game in c languageproject on snake game in c language
project on snake game in c language
 
Snake Game Report
Snake Game ReportSnake Game Report
Snake Game Report
 
Game Tree ( Oyun Ağaçları )
Game Tree ( Oyun Ağaçları )Game Tree ( Oyun Ağaçları )
Game Tree ( Oyun Ağaçları )
 
Snake project report
Snake project reportSnake project report
Snake project report
 
Chess engine presentation
Chess engine presentationChess engine presentation
Chess engine presentation
 
Minmax Algorithm In Artificial Intelligence slides
Minmax Algorithm In Artificial Intelligence slidesMinmax Algorithm In Artificial Intelligence slides
Minmax Algorithm In Artificial Intelligence slides
 
Introduction to Game Development and the Game Industry
Introduction to Game Development and the Game IndustryIntroduction to Game Development and the Game Industry
Introduction to Game Development and the Game Industry
 
Adversarial Search
Adversarial SearchAdversarial Search
Adversarial Search
 
report on snake game
report on snake game report on snake game
report on snake game
 
snake game
snake gamesnake game
snake game
 
Adversarial search
Adversarial search Adversarial search
Adversarial search
 

Similar to Tic Tac Toe

Similar to Tic Tac Toe (20)

MINI-MAX ALGORITHM.pptx
MINI-MAX ALGORITHM.pptxMINI-MAX ALGORITHM.pptx
MINI-MAX ALGORITHM.pptx
 
AI Lesson 07
AI Lesson 07AI Lesson 07
AI Lesson 07
 
Two player games
Two player gamesTwo player games
Two player games
 
Games
GamesGames
Games
 
tic-tac-toe: Game playing
 tic-tac-toe: Game playing tic-tac-toe: Game playing
tic-tac-toe: Game playing
 
0-miniproject sem 4 review 1(1)(2).pptx
0-miniproject sem 4 review 1(1)(2).pptx0-miniproject sem 4 review 1(1)(2).pptx
0-miniproject sem 4 review 1(1)(2).pptx
 
quarto
quartoquarto
quarto
 
Unit_I_Introduction(Part_III).ppt
Unit_I_Introduction(Part_III).pptUnit_I_Introduction(Part_III).ppt
Unit_I_Introduction(Part_III).ppt
 
AIMA_ch3_L2-complement.ppt kjekfkjekjfkjefkjefkjek
AIMA_ch3_L2-complement.ppt kjekfkjekjfkjefkjefkjekAIMA_ch3_L2-complement.ppt kjekfkjekjfkjefkjefkjek
AIMA_ch3_L2-complement.ppt kjekfkjekjfkjefkjefkjek
 
Cse 402 offline b2
Cse 402 offline b2Cse 402 offline b2
Cse 402 offline b2
 
Games.4
Games.4Games.4
Games.4
 
AI3391 Artificial Intelligence UNIT III Notes_merged.pdf
AI3391 Artificial Intelligence UNIT III Notes_merged.pdfAI3391 Artificial Intelligence UNIT III Notes_merged.pdf
AI3391 Artificial Intelligence UNIT III Notes_merged.pdf
 
This is an individual project, to be completed on your own. It i.docx
This is an individual project, to be completed on your own. It i.docxThis is an individual project, to be completed on your own. It i.docx
This is an individual project, to be completed on your own. It i.docx
 
C++ projct
C++ projctC++ projct
C++ projct
 
Chess
ChessChess
Chess
 
python.pptx
python.pptxpython.pptx
python.pptx
 
AI.ppt
AI.pptAI.ppt
AI.ppt
 
Sol90
Sol90Sol90
Sol90
 
Sol90
Sol90Sol90
Sol90
 
1.game
1.game1.game
1.game
 

More from Varunjeet Singh Rekhi (10)

Natural Language Processing
Natural Language ProcessingNatural Language Processing
Natural Language Processing
 
Transistors and Applications
Transistors  and ApplicationsTransistors  and Applications
Transistors and Applications
 
Expert Systems
Expert SystemsExpert Systems
Expert Systems
 
Production System
Production SystemProduction System
Production System
 
Simple Harmonic Motion
Simple Harmonic MotionSimple Harmonic Motion
Simple Harmonic Motion
 
SCSI Interfaces
SCSI InterfacesSCSI Interfaces
SCSI Interfaces
 
Video DIsplay Technologies
Video DIsplay TechnologiesVideo DIsplay Technologies
Video DIsplay Technologies
 
Tower of Hanoi
Tower of HanoiTower of Hanoi
Tower of Hanoi
 
Reconnaissance and Social Engineering
Reconnaissance and Social EngineeringReconnaissance and Social Engineering
Reconnaissance and Social Engineering
 
Bullet trains
Bullet trainsBullet trains
Bullet trains
 

Recently uploaded

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 

Recently uploaded (20)

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 

Tic Tac Toe

  • 1.
  • 2. Tic Tac Toe Game • Tic-tac-toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. • The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game. • Players soon discover that the best play from both parties leads to a draw. • Hence, tic-tac-toe is most often played by young children.
  • 3. Data members and functions used • char matrix[3][3]; //intitial matrix declaration • char check(void); // checks for someone to win • void init_matrix(void); //initializes 3*3 matrics • void get_player_move(void); //get player’s move • void get_computer_move(void); //get computer’s move using AI • void disp_matrix(void); // display matrics after each move
  • 4. 1. board data structure • The board matrix (3*3) contains 3 arrays, each representing a row. • Each row array contains 3 character or string elements. • These elements are either: – “ ” as an empty string, representing an empty cell – “X” representing the X player – “O” representing the O player or computer as a player
  • 5. 2. check() function • At any given state, the board will be in one and one only of these possible states: – Incomplete – Player X won – Player O won(or computer Won) – or a tie • The check() function receives a board array, iterates over all the rows, through all the columns and across both diagonals. • It checks the succession of symbols. • Then it lets us know the current state of that board.
  • 6. 3. get_computer_move() & get_player_move() Function • When the board is empty it is very difficult to identify the best possible move. What is best possible move? X: - 000 000 000 O: - 000 000 000
  • 7. • When the board becomes populated, the best possible move pops out to our eyes. • Let’s use this populated board as our starting point. Lets 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. • Lets check the result for each of these options. X: - 000 110 010 O: - 110 000 100
  • 8. • From the 3 boards in the figure above, when we send the second board to the check() function, we will receive our trophy. Move-1 Move-2 Move-3 • We can do this by iterating over the possible moves, and for each of them:  Create a new board  Add our symbol to the corresponding empty cell  Send this board to the check() function X: - 001 110 010 O: - 110 000 100 X: - 000 111 010 X: - 000 110 011 O: - 110 000 100 O: - 110 000 100
  • 9. Please concentrate for the next essential steps: 1. 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. 2. 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. 3. But for the sake of understanding, how would we grade moves 1 or 3, or any other move with an incomplete result?
  • 10. A (partial) game tree for the game of tic-tac-toe. The top node is the initial state, and MAX moves first, placing an X in an empty square. We show part of the tree, giving alternating moves by MIN ( 0) and MAX (X), until we eventually reach terminal states, which can be assigned utilities according to the rules of the game.
  • 11. • Let’s Focus on move 3. The solution is to send the corresponding board recursively to get_computer_move() and get_player_move() function. • You might be thinking, “But wait! Our opponent plays the next move.” That’s right. Let’s find out what grade our opponent gets for his best future move. • Our opponent has only two possible moves: Move 3-1 Move 3-2
  • 12. • Move 3–1 will win the game in favor of our opponent. Since we are using the exact same get_player_move 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. • Move 3–1 receives a grade of 1 and Move 3 will receive a grade of -1.
  • 13. • In this manner, these 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.
  • 14. • These steps guarantee that: – A losing move will be avoided unless it’s the only option – The computer player can play diversely