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 c++ project presentation
Tic tac toe c++ project presentationTic tac toe c++ project presentation
Tic tac toe c++ project presentation
Saad Symbian
 
Rock, Paper, Scissors
Rock, Paper, ScissorsRock, Paper, Scissors
Rock, Paper, Scissors
Johnathan
 
Rock paper-scissors
Rock paper-scissorsRock paper-scissors
Rock paper-scissors
2gymkori
 

What's hot (20)

Tic tac toe
Tic tac toeTic tac toe
Tic tac toe
 
Tic Tac Toe
Tic Tac ToeTic Tac Toe
Tic Tac Toe
 
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
 
Tic toc game presentation
Tic toc game presentationTic toc game presentation
Tic toc game presentation
 
Tic tac toe c++ project presentation
Tic tac toe c++ project presentationTic tac toe c++ project presentation
Tic tac toe c++ project presentation
 
Tic tac toe simple ai game
Tic tac toe simple ai gameTic tac toe simple ai game
Tic tac toe simple ai game
 
Tic Tac Toe ppt
Tic Tac Toe pptTic Tac Toe ppt
Tic Tac Toe ppt
 
Game playing in artificial intelligent technique
Game playing in artificial intelligent technique Game playing in artificial intelligent technique
Game playing in artificial intelligent technique
 
tic-tac-toe: Game playing
 tic-tac-toe: Game playing tic-tac-toe: Game playing
tic-tac-toe: Game playing
 
Minimax
MinimaxMinimax
Minimax
 
Rock, Paper, Scissors
Rock, Paper, ScissorsRock, Paper, Scissors
Rock, Paper, Scissors
 
Game Playing in Artificial Intelligence
Game Playing in Artificial IntelligenceGame Playing in Artificial Intelligence
Game Playing in Artificial Intelligence
 
Report on car racing game for android
Report on car racing game for androidReport on car racing game for android
Report on car racing game for android
 
Build tic tac toe with javascript (4:11 dc)
Build tic tac toe with javascript (4:11 dc)Build tic tac toe with javascript (4:11 dc)
Build tic tac toe with javascript (4:11 dc)
 
AI 7 | Constraint Satisfaction Problem
AI 7 | Constraint Satisfaction ProblemAI 7 | Constraint Satisfaction Problem
AI 7 | Constraint Satisfaction Problem
 
Rock paper-scissors
Rock paper-scissorsRock paper-scissors
Rock paper-scissors
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Introduction to Game Development
Introduction to Game DevelopmentIntroduction to Game Development
Introduction to Game Development
 
Tic Tac Toe Project
Tic Tac Toe Project Tic Tac Toe Project
Tic Tac Toe Project
 

Similar to Tic Tac Toe

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
abhi353063
 

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
 
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
 
Ai
AiAi
Ai
 

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

1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
meharikiros2
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
pritamlangde
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 

Recently uploaded (20)

Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 

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