Tic Tac Toe Program in C: To learn the famous game which everyone usually enjoys to play Tic Tac Toe Program in C.

What is tic tac toe game?
Tic tac toe is a simple technical game for two players. The game consists of 3 × 3 matrix in which each player marks one space at a time in a grid and the player wins who makes the marking in 3 places horizontal, vertical or diagonally.

TIC TAC TOE game
TIC TAC TOE game

How to play tic tac toe game?
As we give 3×3 matrix and 2 people can only this.
As the game starts first player marks with any symbol in grid anywhere.and after that second player marks in a grid with another symbol.this continue till any player makes marks in 3 successive grid horizontal, vertically or diagonally. The player which makes such 3 marks wins the game.

Tic Tac Toe Program in C

#include<stdio.h>

#include<string.h>

void main()

{

char choice1,choice2;

int player1,player2,i,j,position,move=1,x=0,y=0,count=0,c=1,d=0,a[8],k=0,flag=1;

for(i=0;i<9;i++)

a[i]=3;

printf("Welcome to play TIC-TAC-TOE world\n");

printf("game format\n");

printf(" ______________\n");

printf("| 0 | 1 | 2 |\n");

printf("|__|__|__|\n");

printf("| 3| 4 | 5 |\n");

printf("|__|__|__|\n");

printf("| 6 | 7 | 8 |\n");

printf("|__|__|__|\n");

while(flag==1)

{

printf("Enter X or O for Player1\n");

scanf("%c",&choice1);

if(choice1=='X'||choice1=='x'||choice1=='O'||choice1=='o')

{

flag=0;

}

else

{
flag=1;

printf("Invalid choice\n");

}

}

if(choice1=='X'||choice1=='x')

{

choice2='O';

}

else

{

choice2='X';

}

printf("Player2 choice is %c\n",choice2);

if(choice1=='X'||choice1=='x')

{

player1=1;

player2=0;

}

else

{

player1=0;

player2=1;

}

while(move<3)

{

count=0;

if(move==1)

{

printf("Player1 Enter the position\n");

scanf("%d",&position);


if(a[position]==3)
{

a[position]=player1;

k=0;

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

if(a[k]==3)

printf("\t-");

if(a[k]==1)

printf("\tX");

if(a[k]==0)

printf("\tO");

k++;

}

printf("\n");

}

if((a[0]==player1&&a[1]==player1&&a[2]==player1)

||(a[3]==player1&&a[4]==player1&&a[5]==player1)||(a[6]==player1&&a[7]==player1&&a[8]==player1)||

(a[0]==player1&&a[3]==player1&&a[6]==player1)||(a[1]==player1&&a[4]==player1&&a[7]==player1)||

(a[2]==player1&&a[5]==player1&&a[8]==player1)||(a[0]==player1&&a[4]==player1&&a[8]==player1)||

(a[2]==player1&&a[4]==player1&&a[6]==player1))

{

printf("Player1 wins\n");

x=1;

printf("Thank you for playing this game\n");

move=3;

break;

}

else

{
move=2;

}

}

else

{

printf("This place is not empty.Please give another position\n");

move=1;

}

}

for(i=0;i<9;i++)
{

if(a[i]==3)

count++;

}

if(count==0)

{

move=3;

break;

}

if(move==2)

{

printf("Player2 Enter position\n");

scanf("%d",&position);

if(a[position]==3)

{

a[position]=player2;

k=0;

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

if(a[k]==3)

printf("\t-");

else if(a[k]==1)

printf("\tX");

else

printf("\tO");

k++;

}

printf("\n");

}

if((a[0]==player2&&a[1]==player2&&a[2]==player2)||

(a[3]==player2&&a[4]==player2&&a[5]==player2)||(a[6]==player2&&a[7]==player2&&a[8]==player2)||

(a[0]==player2&&a[3]==player2&&a[6]==player2)||(a[1]==player2&&a[4]==player2&&a[7]==player2)||(a[2]==player2&&a[5]==player2&&a[8]==player2)||

(a[0]==player2&&a[4]==player2&&a[8]==player2)||

(a[2]==player2&&a[4]==player2&&a[6]==player2))

{

printf("Player2 wins\n");

y=1;

printf("Thank you for playing this game\n");

move=3;

break;

}

else

{
move=1;

}

}

else

{

printf("This place is not empty.Please give another position\n");

move=2;
}
}

}

if(x!=1&&y!=1)

{

printf("Draw Match\n");

printf("Thank you for playing this game\n");

}

}

Output :

C Program to Implement a TIC TAC TOE game

Write A Comment