SlideShare a Scribd company logo
2
Most read
6
Most read
10
Most read
Tic Tac Toe
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

PPTX
Tic Tac Toe ppt
SanchitRastogi15
 
PPTX
Tic tac toe game with graphics presentation
Prionto Abdullah
 
PPTX
Seven ages of man
shyrey mae macabitas
 
PPTX
AI based Tic Tac Toe game using Minimax Algorithm
Kiran Shahi
 
PPTX
PTA MEETING PPT.pptx
MaryMialynSalva
 
PPTX
ROBOTICS FOR BIOLOGICAL AND MEDICAL APPLICATIONS
sathish sak
 
PPT
Long division
ericadetman
 
PPTX
SWIPE-LEFT-AND-SWIPE-RIGHT.pptxbnnnnnnbnbbbbbbbbbnbbbbbbbbbbbbbbnnnnnnnnbbbbb...
Stephaniemagnaye2
 
Tic Tac Toe ppt
SanchitRastogi15
 
Tic tac toe game with graphics presentation
Prionto Abdullah
 
Seven ages of man
shyrey mae macabitas
 
AI based Tic Tac Toe game using Minimax Algorithm
Kiran Shahi
 
PTA MEETING PPT.pptx
MaryMialynSalva
 
ROBOTICS FOR BIOLOGICAL AND MEDICAL APPLICATIONS
sathish sak
 
Long division
ericadetman
 
SWIPE-LEFT-AND-SWIPE-RIGHT.pptxbnnnnnnbnbbbbbbbbbnbbbbbbbbbbbbbbnnnnnnnnbbbbb...
Stephaniemagnaye2
 

What's hot (20)

PPTX
Artificial Intelligence- TicTacToe game
manika kumari
 
PPTX
Tic tac toe
Tanvir360
 
PPTX
Tic Tac Toe using Mini Max Algorithm
Ujjawal Poudel
 
PPTX
tic-tac-toe: Game playing
kalpana Manudhane
 
PPTX
Min-Max algorithm
Dr. C.V. Suresh Babu
 
PPT
Tic tac toe c++ programing
Krishna Agarwal
 
PPTX
Tic tac toe
Niloy Biswas
 
DOC
C++ projct
J M
 
PPT
Tic tac toe c++ project presentation
Saad Symbian
 
DOC
Tic tac toe game code
Upendra Sengar
 
PPT
Game Playing in Artificial Intelligence
lordmwesh
 
PPTX
Alpha beta
sabairshad4
 
PPTX
Church Turing Thesis
Hemant Sharma
 
PPTX
Turing Test in Artificial Intelligence.pptx
RSAISHANKAR
 
PPTX
Minimax
Nagarajan
 
PPT
project on snake game in c language
Ashutosh Kumar
 
PPTX
Relationship Among Token, Lexeme & Pattern
Bharat Rathore
 
PPTX
Minmax Algorithm In Artificial Intelligence slides
SamiaAziz4
 
PPTX
Pipeline processing and space time diagram
Rahul Sharma
 
Artificial Intelligence- TicTacToe game
manika kumari
 
Tic tac toe
Tanvir360
 
Tic Tac Toe using Mini Max Algorithm
Ujjawal Poudel
 
tic-tac-toe: Game playing
kalpana Manudhane
 
Min-Max algorithm
Dr. C.V. Suresh Babu
 
Tic tac toe c++ programing
Krishna Agarwal
 
Tic tac toe
Niloy Biswas
 
C++ projct
J M
 
Tic tac toe c++ project presentation
Saad Symbian
 
Tic tac toe game code
Upendra Sengar
 
Game Playing in Artificial Intelligence
lordmwesh
 
Alpha beta
sabairshad4
 
Church Turing Thesis
Hemant Sharma
 
Turing Test in Artificial Intelligence.pptx
RSAISHANKAR
 
Minimax
Nagarajan
 
project on snake game in c language
Ashutosh Kumar
 
Relationship Among Token, Lexeme & Pattern
Bharat Rathore
 
Minmax Algorithm In Artificial Intelligence slides
SamiaAziz4
 
Pipeline processing and space time diagram
Rahul Sharma
 
Ad

Similar to Tic Tac Toe (20)

PPTX
MINI-MAX ALGORITHM.pptx
NayanChandak1
 
PDF
AI Lesson 07
Assistant Professor
 
PPTX
Two player games
Subash Chandra Pakhrin
 
PPT
Games
kalavathisugan
 
PPTX
Artificial intelligence dic_SLIDE_3.pptx
PenielAnkomah1
 
PPTX
0-miniproject sem 4 review 1(1)(2).pptx
AhishektttPhm
 
PPT
problem-reduction-game-problemgmgntc.ppt
s0618614
 
PDF
quarto
Matthew Kerner
 
PPT
Unit_I_Introduction(Part_III).ppt
ganesh15478
 
PPT
AIMA_ch3_L2-complement.ppt kjekfkjekjfkjefkjefkjek
pavan402055
 
PDF
Android application - Tic Tac Toe
Sarthak Srivastava
 
PDF
Cse 402 offline b2
sujoyhnkc
 
PDF
Games.4
Praveen Kumar
 
PDF
AI3391 Artificial Intelligence UNIT III Notes_merged.pdf
Guru Nanak Technical Institutions
 
PPT
Adversarial Search and Game-Playing .ppt
0fakerxfaker0
 
DOCX
This is an individual project, to be completed on your own. It i.docx
abhi353063
 
PPT
Chess
Iffat Anjum
 
PPTX
python.pptx
sreeshanthSingarapu
 
PPTX
AI- to eat boiled egg and cheese Unit-II.pptx
PGARMYff
 
PPT
AI.ppt
ArghyaGayen2
 
MINI-MAX ALGORITHM.pptx
NayanChandak1
 
AI Lesson 07
Assistant Professor
 
Two player games
Subash Chandra Pakhrin
 
Artificial intelligence dic_SLIDE_3.pptx
PenielAnkomah1
 
0-miniproject sem 4 review 1(1)(2).pptx
AhishektttPhm
 
problem-reduction-game-problemgmgntc.ppt
s0618614
 
Unit_I_Introduction(Part_III).ppt
ganesh15478
 
AIMA_ch3_L2-complement.ppt kjekfkjekjfkjefkjefkjek
pavan402055
 
Android application - Tic Tac Toe
Sarthak Srivastava
 
Cse 402 offline b2
sujoyhnkc
 
Games.4
Praveen Kumar
 
AI3391 Artificial Intelligence UNIT III Notes_merged.pdf
Guru Nanak Technical Institutions
 
Adversarial Search and Game-Playing .ppt
0fakerxfaker0
 
This is an individual project, to be completed on your own. It i.docx
abhi353063
 
python.pptx
sreeshanthSingarapu
 
AI- to eat boiled egg and cheese Unit-II.pptx
PGARMYff
 
AI.ppt
ArghyaGayen2
 
Ad

More from Varunjeet Singh Rekhi (10)

PPTX
Natural Language Processing
Varunjeet Singh Rekhi
 
PPTX
Transistors and Applications
Varunjeet Singh Rekhi
 
PPTX
Expert Systems
Varunjeet Singh Rekhi
 
PPTX
Production System
Varunjeet Singh Rekhi
 
PPT
Simple Harmonic Motion
Varunjeet Singh Rekhi
 
PPTX
SCSI Interfaces
Varunjeet Singh Rekhi
 
PPTX
Video DIsplay Technologies
Varunjeet Singh Rekhi
 
PPTX
Tower of Hanoi
Varunjeet Singh Rekhi
 
PPTX
Reconnaissance and Social Engineering
Varunjeet Singh Rekhi
 
PPTX
Bullet trains
Varunjeet Singh Rekhi
 
Natural Language Processing
Varunjeet Singh Rekhi
 
Transistors and Applications
Varunjeet Singh Rekhi
 
Expert Systems
Varunjeet Singh Rekhi
 
Production System
Varunjeet Singh Rekhi
 
Simple Harmonic Motion
Varunjeet Singh Rekhi
 
SCSI Interfaces
Varunjeet Singh Rekhi
 
Video DIsplay Technologies
Varunjeet Singh Rekhi
 
Tower of Hanoi
Varunjeet Singh Rekhi
 
Reconnaissance and Social Engineering
Varunjeet Singh Rekhi
 
Bullet trains
Varunjeet Singh Rekhi
 

Recently uploaded (20)

PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PDF
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PPT
SCOPE_~1- technology of green house and poyhouse
bala464780
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PPTX
Tunnel Ventilation System in Kanpur Metro
220105053
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PDF
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PPTX
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PPT
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PDF
Introduction to Data Science: data science process
ShivarkarSandip
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
SCOPE_~1- technology of green house and poyhouse
bala464780
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
Inventory management chapter in automation and robotics.
atisht0104
 
Tunnel Ventilation System in Kanpur Metro
220105053
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
Information Retrieval and Extraction - Module 7
premSankar19
 
Introduction to Data Science: data science process
ShivarkarSandip
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 

Tic Tac Toe

  • 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