16+ C Tic Tac Toe Examples

This post contains a total of 16+ Hand-Picked C Tic Tac Toe Examples with Source Code. All the Tic Tac Toe programs are made using C Programming Language.

You can use the source code of these programs for educational use with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By Balzhibek

Made by Balzhibek. Simple C Tic Tac Toe game. ( Source )

#include<stdio.h>

#include<string.h>
 int main()
{
char choice1,choice2;
int p1,p2,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("for reference to players\n");
printf(" ______________\n");
printf("|a[0]|a[1]|a[2]|\n");
printf("|____|____|____|\n");
printf("|a[3]|a[4]|a[5]|\n");
printf("|____|____|____|\n");
printf("|a[6]|a[7]|a[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')
{
p1=1;
p2=0;

}
else
{
p1=0;
p2=1;

}
while(move<3)
{
 count=0;
 if(move==1) 
{
 printf("Player1 Enter the position\n");
 scanf("%d",&position);
 if(a[position]==3) 
{
 a[position]=p1;
 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]==p1&&a[1]==p1&&a[2]==p1)
||(a[3]==p1&&a[4]==p1&&a[5]==p1)||(a[6]==p1&&a[7]==p1&&a[8]==p1)||
(a[0]==p1&&a[3]==p1&&a[6]==p1)||(a[1]==p1&&a[4]==p1&&a[7]==p1)||
(a[2]==p1&&a[5]==p1&&a[8]==p1)||(a[0]==p1&&a[4]==p1&&a[8]==p1)||
(a[2]==p1&&a[4]==p1&&a[6]==p1)) 
{
 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]=p2;
 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]==p2&&a[1]==p2&&a[2]==p2)||
(a[3]==p2&&a[4]==p2&&a[5]==p2)||(a[6]==p2&&a[7]==p2&&a[8]==p2)||
(a[0]==p2&&a[3]==p2&&a[6]==p2)||(a[1]==p2&&a[4]==p2&&a[7]==p2)||(a[2]==p2&&a[5]==p2&&a[8]==p2)||
(a[0]==p2&&a[4]==p2&&a[8]==p2)||
(a[2]==p2&&a[4]==p2&&a[6]==p2)) 
{
 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");

}

}
 

2. By Sriram Kummari

Made by Sriram Kummari. ( Source )

#include <stdio.h>


#include <stdlib.h>

char square[10]={'0','1','2','3','4','5','6','7','8','9'};
int checkWin();
void drawBoard();

int main() {
int player= 1,i,choice;
char mark;
do{drawBoard();
player=(player%2)?1:2;
printf("player %d,enter the choice: ",player);
scanf("%d",&choice);
mark=(player==1)?'x':'0';
if(choice==1&&square[1]=='1')
square[1]=mark;
else if(choice==2&&square[2]=='2')
square[2]=mark;
else if(choice==3&&square[3]=='3')
square[3]=mark;
else if(choice==4&&square[4]=='4')
square[4]=mark;
else if(choice==5&&square[5]=='5')
square[5]=mark;
else if(choice==6&&square[6]=='6')
square[6]=mark;
else if(choice==7&&square[7]=='7')
square[7]=mark;
else if(choice==8&&square[8]=='8')
square[8]=mark;
else if(choice==9&&square[9]=='9')
square[9]=mark;
else{printf("invalid option!");
player--;
}
i=checkWin();
player++;

}while(i==1);
drawBoard();
if(i==1){
    printf("==>player %d won",--player);
    
}else{printf("==>game drawn");
};
  return 0;
}
int checkWin(){
    if(square[1]==square[2]&&square[2]==square[3])
    return 1;
   else if(square[4]==square[5]&&square[5]==square[6])
    return 1; 
else if(square[7]==square[8]&&square[8]==square[9])
    return 1;
 
else if(square[1]==square[4]&&square[4]==square[7])
    return 1;
 else if(square[2]==square[5]&&square[5]==square[8])
    return 1;
 else if(square[3]==square[6]&&square[6]==square[9])
    return 1;
 
 else if(square[1]==square[5]&&square[5]==square[9])
    return 1;
 else if(square[3]==square[5]&&square[5]==square[7])
    return 1;
 else if(square[1]!='1'&&square[2]!='2'&&square[3]!='3'&&square[4]!='4'&&square[5]!='5'&&square[6]!='6'&&square[7]!='7'&&square[8]!='8'&&square[9]!='9')
 return 0;
 else
return -1;
    
}
void drawBoard(){
  system("c/s");
  printf("\n\n\t Tic tac toe\n\n");
  printf("player1(x)-player2(0)\n\n\n");
printf("    |     |   \n");
printf(" %c  |  %c  | %c \n",square[1],square[2],square[3]);
printf("____|_____|____\n");
printf("    |     |    \n");
printf(" %c  |  %c  | %c \n",square[4],square[5],square[6]);
printf("____|_____|____\n");
printf("    |     |    \n");
printf(" %c  |  %c  | %c \n",square[7],square[8],square[9]);

printf("    |     |    \n");   
    
}

3. By Sammy Q

Made by Sammy Q. ( Source )

//Tic Tac Toe Game
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <windows.h>

int board[10] = {2,2,2,2,2,2,2,2,2,2};
int turn = 1,flag = 0;
int player,comp;

void menu();
void go(int n);
void start_game();
void check_draw();
void draw_board();
void player_first();
void put_X_O(char ch,int pos);
COORD coord= {0,0}; // this is global variable
//center of axis is set to the top left cornor of the screen
void gotoxy(int x,int y)
{
    coord.X=x;
    coord.Y=y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

void main()
{
    system("cls");
    menu();
    getch();

}

void menu()
{
    int choice;
    system("cls");
    printf("\n--------MENU--------");
    printf("\n1 : Play with X");
    printf("\n2 : Play with O");
    printf("\n3 : Exit");
    printf("\nEnter your choice:>");
    scanf("%d",&choice);
    turn = 1;
    switch (choice)
    {
    case 1:
        player = 1;
        comp = 0;
        player_first();
        break;
    case 2:
        player = 0;
        comp = 1;
        start_game();
        break;
    case 3:
        exit(1);
    default:
        menu();
    }
}

int make2()
{
    if(board[5] == 2)
        return 5;
    if(board[2] == 2)
        return 2;
    if(board[4] == 2)
        return 4;
    if(board[6] == 2)
        return 6;
    if(board[8] == 2)
        return 8;
    return 0;
}

int make4()
{
    if(board[1] == 2)
        return 1;
    if(board[3] == 2)
        return 3;
    if(board[7] == 2)
        return 7;
    if(board[9] == 2)
        return 9;
    return 0;
}

int posswin(int p)
{
// p==1 then X   p==0  then  O
    int i;
    int check_val,pos;

    if(p == 1)
        check_val = 18;
    else
        check_val = 50;

    i = 1;
    while(i<=9)//row check
    {
        if(board[i] * board[i+1] * board[i+2] == check_val)
        {
            if(board[i] == 2)
                return i;
            if(board[i+1] == 2)
                return i+1;
            if(board[i+2] == 2)
                return i+2;
        }
        i+=3;
    }

    i = 1;
    while(i<=3)//column check
    {
        if(board[i] * board[i+3] * board[i+6] == check_val)
        {
            if(board[i] == 2)
                return i;
            if(board[i+3] == 2)
                return i+3;
            if(board[i+6] == 2)
                return i+6;
        }
        i++;
    }

    if(board[1] * board[5] * board[9] == check_val)
    {
        if(board[1] == 2)
            return 1;
        if(board[5] == 2)
            return 5;
        if(board[9] == 2)
            return 9;
    }

    if(board[3] * board[5] * board[7] == check_val)
    {
        if(board[3] == 2)
            return 3;
        if(board[5] == 2)
            return 5;
        if(board[7] == 2)
            return 7;
    }
    return 0;
}

void go(int n)
{
    if(turn % 2)
        board[n] = 3;
    else
        board[n] = 5;
    turn++;
}

void player_first()
{
    int pos;

    check_draw();
    draw_board();
    gotoxy(30,18);
    printf("Your Turn :> ");
    scanf("%d",&pos);

    if(board[pos] != 2)
        player_first();

    if(pos == posswin(player))
    {
        go(pos);
        draw_board();
        gotoxy(30,20);
        //textcolor(128+RED);
        printf("Player Wins");
        getch();
        exit(0);
    }

    go(pos);
    draw_board();
    start_game();
}

void start_game()
{
// p==1 then X   p==0  then  O
    if(posswin(comp))
    {
        go(posswin(comp));
        flag = 1;
    }
    else if(posswin(player))
        go(posswin(player));
    else if(make2())
        go(make2());
    else
        go(make4());
    draw_board();

    if(flag)
    {
        gotoxy(30,20);
        //textcolor(128+RED);
        printf("Computer wins");
        getch();
    }
    else
        player_first();
}

void check_draw()
{
    if(turn > 9)
    {
        gotoxy(30,20);
        //textcolor(128+RED);
        printf("Game Draw");
        getch();
        exit(0);
    }
}

void draw_board()
{
    int j;

    for(j=9; j<17; j++)
    {
        gotoxy(35,j);
        printf("|       |");
    }
    gotoxy(28,11);
    printf("-----------------------");
    gotoxy(28,14);
    printf("-----------------------");

    for(j=1; j<10; j++)
    {
        if(board[j] == 3)
            put_X_O('X',j);
        else if(board[j] == 5)
            put_X_O('O',j);
    }
}

void put_X_O(char ch,int pos)
{
    int m;
    int x = 31, y = 10;

    m = pos;

    if(m > 3)
    {
        while(m > 3)
        {
            y += 3;
            m -= 3;
        }
    }
    if(pos % 3 == 0)
        x += 16;
    else
    {
        pos %= 3;
        pos--;
        while(pos)
        {
            x+=8;
            pos--;
        }
    }
    gotoxy(x,y);
    printf("%c",ch);
}

4. By Sujal M. Lakhani

Made by Sujal M. Lakhani. Basic C Tic Tac Toe game. ( Source )

#include<stdio.h>
#include<conio.h>

char sq[9]={'1','2','3','4','5','6','7','8','9'};

int iswin();
void board();

int main()
{
    int player = 0, i,choice;
    char mark;
    clrscr();
    do 
    {
        
        player++;
        player = (player%2)?1:2;
        mark = (player == 1)?'X':'O';
        board();
        printf("Player %d, Enter your choice - ",player);
        scanf("%d",&choice);
        if(choice == 1 && sq[0] == '1')
            sq[0]=mark;
            
        else if(choice == 2 && sq[1] == '2')
            sq[1]=mark;
            
        else if(choice == 3 && sq[2] == '3')
            sq[2]=mark;
            
        else if(choice == 4 && sq[3] == '4')
            sq[3]=mark;
            
        else if(choice == 5 && sq[4] == '5')
            sq[4]=mark;
          
        else if(choice == 6 && sq[5] == '6')
            sq[5]=mark;
         
        else if(choice == 7 && sq[6] == '7')
            sq[6]=mark;
            
        else if(choice == 8 && sq[7] == '8')
            sq[7]=mark;
            
        else if(choice == 9 && sq[8] == '9')
            sq[8]=mark;
            
        else 
        {
            printf("Invalid choice!");
            printf("\nPlayer %d Lost",player);
            getch();
            exit (0);
         }
       
       i=iswin();
       clrscr();
       
  }while(i==-1);
 
    board();
    if(i==1)
    {
        printf("Player %d won!",player);

      }
    
    else 
    {
        printf("Game is draw!");
    }
    
    getch();
    return 0;
    
}

int iswin()
{
    if((sq[0]==sq[1] && sq[1]==sq[2]) || 
    (sq[3]==sq[4] && sq[4]==sq[5]) || 
    (sq[6]==sq[7] && sq[7]==sq[8]) || 
    (sq[0]==sq[3] && sq[3]==sq[6]) ||     
    (sq[1]==sq[4] && sq[4]==sq[7]) ||      
    (sq[2]==sq[5] && sq[5]==sq[8]) ||   
    (sq[0]==sq[4] && sq[4]==sq[8]) || 
    (sq[2]==sq[4] && sq[4]==sq[6]))
        return 1;
        
    else if(sq[0]!='1' && sq[1]!='2' && 
    sq[2]!='3' && sq[3]!='4' && sq[4]!='5' && 
    sq[5]!='6' && sq[6]!='7' && sq[7]!='8' && 
    sq[8]!='9')
        return 2;
        
    else
        return (-1);
        
}

void board()
{
    printf("\n\nTic Tac Toe\n");
    printf("     |     |\n  %c  |  %c  |  %c  \n",sq[0],sq[1],sq[2]);
    printf("_____|_____|_____\n     |     |\n  %c  |  %c  |  %c  \n",sq[3],sq[4],sq[5]);
    printf("_____|_____|_____\n     |     |\n  %c  |  %c  |  %c  \n     |     |\n",sq[6],sq[7],sq[8]);
    
}

5. By Ashish Sharma

Made by Ashish Sharma. Enter you moves using column and rows. ( Source )

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main ()
{
    char a[3][3] , character ;
    int i , j , k , l , size=9;
    int value=0,value1=0,value2=0,value3=0;
    int s , s1 , s2 , s3;
    srand(time(0));
    printf("[11] | [12] | [13]\n---- | ---- | ----\n[21] | [22] | [23]\n---- | ---- | ----\n[31] | [32] | [33]\n");
    printf("Whaat you choose for playing game:\n");
    scanf("%c" , &character);
    for(i=0;i<size;i++){
        printf("Where did you put character enter with row and column:\n");
        scanf("%d %d" , &j , &l);
        a[j][l] = character ;
        jump:
        k=rand()%10;
        printf("%d" , k);
        switch(k){
            case 1:
            if(a[1][1]!=character && a[1][1]!='?'){
                a[1][1]=='?';
            }
            else{
                goto jump;
            }
            break;
            case 2:
            if(a[1][2]!=character && a[1][2]!='?'){
                a[1][2]=='?';
            }
             else{
                goto jump;
            }
            break;
            case 3:
            if(a[1][3]!=character && a[1][3]!='?'){
                a[1][3]=='?';
            }
             else{
                goto jump;
            }
            break;
            case 4:
            if(a[2][1]!=character && a[2][1]!='?'){
                a[2][1]=='?';
            }
             else{
                goto jump;
            }
            break;
            case 5:
            if(a[2][2]!=character && a[2][2]!='?'){
                a[2][2]=='?';
            }
             else{
                goto jump;
            }
            break;
            case 6:
            if(a[2][3]!=character && a[2][3]!='?'){
                a[2][3]=='?';
            } else{
                goto jump;
            }
            break;
            case 7:
            if(a[3][1]!=character && a[3][1]!='?'){
                a[3][1]=='?';
            }
             else{
                goto jump;
            }
            break;
            case 8:
            if(a[3][2]!=character && a[3][2]!='?'){
                a[3][2]=='?';
            }
             else{
                goto jump;
            }
            break;
            case 9:
            if(a[3][3]!=character && a[3][3]!='?'){
                a[3][3]=='?';
            }
             else{
                goto jump;
            }
            break;
            default:
            goto jump;
            break;
        }
        size-=2;
    }
    for(i=1;i<=3;i++){
        for(j=1;j<=3;j++){
            if(a[i][j]==character){
                value++;
            }
            if(i==j){
                if(a[i][j]==character){
                    value1++;
                }
                else{
                    s1++;
                }
            }
            if(a[j][i]==character){
                value2++;
            }
            if(i+j==4){
                if(a[i][j]==character){
                    value3++;
                }
                else{
                    s3++;
                }
            }
            if(a[i][j]=='?'){
                s++;
            }
            if(a[j][i]=='?'){
                s2++;
            }
        }
    }
    if(value==3 || value1==3 || value2==3 || value3==3){
        printf("You win");
    }
    else if(s==3 || s1==3 || s2==3 || s3==3){
        printf("computer wins");
    }
return 0 ;
}

6. By Rishi Pathak

Made by Rishi Pathak. ( Source )

#include <stdio.h>

int main() {
     int countO=0;
    int countX=0;
    int won=0;
    int count_space=0;
    int invalid_input=0;
    char c1,c2,c3,c4,c5,c6,c7,c8,c9;
    printf("Enter the input for the first row (O,X,?). Separate each input with a space. Example:(O X ?):\n");
    scanf("%c %c %c",&c1,&c2,&c3);
    getchar();
     printf("Enter the input for the first row (O,X,?). Separate each input with a space. Example:(O X ?):\n");
    scanf("%c %c %c",&c4,&c5,&c6);
    getchar();
     printf("Enter the input for the first row (O,X,?). Separate each input with a space. Example:(O X ?):\n");
    scanf("%c %c %c",&c7,&c8,&c9);
    if(!(c1=='O'||c1=='?'||c1=='X'))
    {
        invalid_input=1;
    }
     if(!(c2=='O'||c2=='?'||c2=='X'))
    {
        invalid_input=1;
    }
     if(!(c3=='O'||c3=='?'||c3=='X'))
    {
        invalid_input=1;
    }
     if(!(c4=='O'||c4=='?'||c4=='X'))
    {
        invalid_input=1;
    }
     if(!(c5=='O'||c5=='?'||c5=='X'))
    {
        invalid_input=1;
    }
     if(!(c6=='O'||c6=='?'||c6=='X'))
    {
        invalid_input=1;
    }
     if(!(c7=='O'||c7=='?'||c7=='X'))
    {
        invalid_input=1;
    }
     if(!(c8=='O'||c8=='?'||c8=='X'))
    {
        invalid_input=1;
    }
     if(!(c9=='O'||c9=='?'||c9=='X'))
    {
        invalid_input=1;
    }
    if(invalid_input==1)
    {
        printf("Invalid Imput!\n");
        return(0);
    }
    countO=(c1=='O')?countO+1:countO;
    countO=(c2=='O')?countO+1:countO;
    countO=(c3=='O')?countO+1:countO;
    countO=(c4=='O')?countO+1:countO;
    countO=(c5=='O')?countO+1:countO;
    countO=(c6=='O')?countO+1:countO;
    countO=(c7=='O')?countO+1:countO;
    countO=(c8=='O')?countO+1:countO;
    countO=(c9=='O')?countO+1:countO;

    countX=(c1=='X')?countX+1:countX;
    countX=(c1=='X')?countX+1:countX;
    countX=(c2=='X')?countX+1:countX;
    countX=(c3=='X')?countX+1:countX;
    countX=(c4=='X')?countX+1:countX;
    countX=(c5=='X')?countX+1:countX;
    countX=(c6=='X')?countX+1:countX;
    countX=(c7=='X')?countX+1:countX;
    countX=(c8=='X')?countX+1:countX;
    countX=(c9=='X')?countX+1:countX;

    count_space=9-(countX+countO);

    if(!((countO-countX)==1||(countX-countO)==1))
    {
        printf("Invalid input\n");
        return(0);
    }

    if(c1=='O'&&c2=='O'&&c3=='O')
    {
        won=1;
    }
     if(c4=='O'&&c5=='O'&&c6=='O')
    {
        won=1;
    }
     if(c7=='O'&&c8=='O'&&c9=='O')
    {
        won=1;
    }
     if(c1=='O'&&c4=='O'&&c7=='O')
    {
        won=1;
    }
     if(c2=='O'&&c5=='O'&&c3=='O')
    {
        won=1;
    }
    if(c3=='O'&&c6=='O'&&c9=='O')
    {
        won=1;
    }
    if(c1=='O'&&c5=='O'&&c9=='O')
    {
        won=1;
    }
    if(c3=='O'&&c5=='O'&&c7=='O')
    {
        won=1;
    }
    if(won==1)
    {
        printf("O won the game\n");
        return(0);
    }
    won=0;
    if(c1=='X'&&c2=='X'&&c3=='X')
    {
        won=1;
    }
     if(c4=='X'&&c5=='X'&&c6=='X')
    {
        won=1;
    }
     if(c7=='X'&&c8=='X'&&c9=='X')
    {
        won=1;
    }
     if(c1=='X'&&c4=='X'&&c7=='X')
    {
        won=1;
    }
     if(c2=='X'&&c5=='X'&&c3=='X')
    {
        won=1;
    }
    if(c3=='X'&&c6=='X'&&c9=='X')
    {
        won=1;
    }
    if(c1=='X'&&c5=='X'&&c9=='X')
    {
        won=1;
    }
    if(c3=='X'&&c5=='X'&&c7=='X')
    {
        won=1;
    }
    if(won==1)
    {
        printf("X won the game\n");
        return(0);
    }
    if(count_space==0)
    {
        printf("Game over!\n");
    }
    else if ((countO-countX)>0)
    {
        printf(" X gets the next chance\n");
    }
    else
    {
        printf(" O gets the next chance\n");
    }
    return 0;
}

7. By The future is now thanks to science

Made by The future is now thanks to science. ( Source )

//Please use an IDE or text editor to run this code.
#include<stdio.h>
void print(char point[])
{
     printf("%c|%c|%c\n",point[0],point[1],point[2]);
     printf("- - -\n");
     printf("%c|%c|%c\n",point[3],point[4],point[5]);
     printf("- - -\n");
     printf("%c|%c|%c\n",point[6],point[7],point[8]);
     return;
}
int main()
{
     char point[]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
     print(point);
     char psign1,psign2;
     while(1)
     {
        printf("What will be the sign of first player?\n");
        scanf(" %c",&psign1);
        printf("What will be the sign of second player?\n");
        scanf(" %c",&psign2);

        if(psign1==psign2)
        {
            printf("Enter a valid character\n");

        }
        else
        {
            break;
        }
     }
     printf("The sequence of the box are like this\n");
     printf("%c|%c|%c\n",'1','2','3');
     printf("- - -\n");
     printf("%c|%c|%c\n",'4','5','6');
     printf("- - -\n");
     printf("%c|%c|%c\n",'7','8','9');
     printf("So if you enter 1 it will count for 1st box\n");
     int p1,p2,d=0;
     int i=1;
     while(i<10)
     {
         if(i%2==1)
         {
             printf("Enter the number player 1\n");
             scanf("%d",&p1);
             if(point[p1-1]==' ')
             {
                 point[p1-1]=psign1;
                 i++;
                print(point);
             }
             else{
                printf("Not a valid place\n");
                continue;
             }
              if((point[0]==point[1]&&point[1]==point[2]&&point[0]==psign1)||(point[3]==point[4]&&point[4]==point[5]&&point[3]==psign1)||(point[6]==point[7]&&point[7]==point[8]&&point[6]==psign1)||(point[0]==point[3]&&point[3]==point[6]&&point[0]==psign1)||(point[1]==point[4]&&point[4]==point[7]&&point[1]==psign1)||(point[2]==point[5]&&point[5]==point[8]&&point[2]==psign1)||(point[2]==point[4]&&point[4]==point[6]&&point[2]==psign1))
              {
                   d=1;
                   printf("Player 1 wins");
                   break;
              }
          }
          else
         {
             printf("Enter the number player 2\n");
             scanf("%d",&p2);
             if(point[p2-1]==' ')
             {
                 point[p2-1]=psign2;
                 i++;
                 print(point);
             }
             else{
                printf("Not a valid place\n");
                continue;
             }
              if((point[0]==point[1]&&point[1]==point[2]&&point[0]==psign2)||(point[3]==point[4]&&point[4]==point[5]&&point[3]==psign2)||(point[6]==point[7]&&point[7]==point[8]&&point[6]==psign2)||(point[0]==point[3]&&point[3]==point[6]&&point[0]==psign2)||(point[1]==point[4]&&point[4]==point[7]&&point[1]==psign2)||(point[2]==point[5]&&point[5]==point[8]&&point[2]==psign2)||(point[2]==point[4]&&point[4]==point[6]&&point[2]==psign2))
              {
                   printf("Player 2 wins");
                   d=1;
                   break;

              }
          }

}
     if(d==0)
     {
         printf("It's a draw");
     }
}

8. By Luki

Made by Luki. A Simple Tic Tac Toe game mad using C Language. ( Source )

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

// #define LOG
#define LENGTH(x) (sizeof(x) / sizeof(x[0]))
#define NEWLINE printf("\n");
#define BOARD_SIZE 9
#define PLAYER1 'X'
#define PLAYER2 'O'

/* Board
+-----+-----+-----+
|  0  |  1  |  2  |
+-----+-----+-----+
|  3  |  4  |  5  |
+-----+-----+-----+
|  6  |  7  |  8  |
+-----+-----+-----+
*/

const int wins[] = {
    0, 1, 2,
    0, 3, 6,
    0, 4, 8,
    1, 4, 7,
    2, 5, 8,
    2, 4, 6,
    3, 4, 5,
    6, 7, 8
};

// Checks if the board still have some unmarked spaces.
int checkForSpace(char* board);

// Checks if there's a win/three-in-a-row.
_Bool checkForWin(char* board);

// Marks the board.
int mark(char* board, char mark);

// Draws the board.
void drawBoard(char* board);

int main(void) {
    // Declares the necessary things.
    int pos = -1;
    _Bool currentPlayer = 0;
    char board[BOARD_SIZE];
    
    printf("         Tic Tac Toe\n");
    printf("Player 1 = X     Player 2 = O\n");
    
    do {
        pos = mark(board, currentPlayer ? PLAYER2 : PLAYER1);
        if (!pos) continue;
        
        printf("\n      Player %d picked %d\n", currentPlayer + 1, pos);
        drawBoard(board);
        
        if (checkForWin(board))
            break;
        
        currentPlayer = !currentPlayer;
    } while(checkForSpace(board));
    
    NEWLINE
    
    if (checkForWin(board))
        printf("   Player %d is the winner!", currentPlayer + 1);
    else printf("         It's a tie!");
    
    return 0;
}

int checkForSpace(char* board) {
    int count = 0;
    for (int i = 0; i < BOARD_SIZE; i++) {
        if (board[i] != PLAYER1 &&
            board[i] != PLAYER2) count++;
    }
    
    return count;
}

_Bool checkForWin(char* board) {
    int a, b, c;
    for (int i = 0; i < LENGTH(wins); i += 3) {
        a = wins[i];
        b = wins[i + 1];
        c = wins[i + 2];
        
        if (board[a] == PLAYER1 ||
            board[a] == PLAYER2 ) {
            if (board[a] == board[b] &&
                board[b] == board[c]) return 1;
        }
    }
    
    return 0;
}

int mark(char* board, char mark) {
    srand(time(NULL));

    int length = checkForSpace(board);
    if (!length)
        return 0;
    
    int pos[length];
    
    for (int i = 0, j = 0; i < BOARD_SIZE; i++) {
        if (board[i] != PLAYER1 &&
            board[i] != PLAYER2)
            pos[j++] = i;
    }
    
    int rnd = rand() % length;
    int index = pos[rnd];
    board[index] = mark;
    
#ifdef LOG
    printf("\nPos: ");
    for (int i = 0; i < length; i++) {
        printf("%d, ", pos[i]);
    }
    
    printf("\nLENGTH: %d\n", LENGTH(pos));
    printf("Rand: %d\n", rnd);
    printf("Index: %d\n", index);
#endif
    
    return index + 1;
}

void drawBoard(char* board) {
    printf("     +-----+-----+-----+\n");
    
    for (int i = 0; i < BOARD_SIZE; i += 3) {
        printf("     |");
        
        for (int j = 0; j < 3; j++) {
            if (board[i + j] != PLAYER1 &&
                board[i + j] != PLAYER2)
                printf("  %d  |", i + j + 1);
            else printf("  %c  |", board[i + j]);
        }
        
        printf("\n     +-----+-----+-----+\n");
        
    }
}

9. By Vicky Kumar ๐Ÿ‡ฎ๐Ÿ‡ณ

Made by Vicky Kumar ๐Ÿ‡ฎ๐Ÿ‡ณ. ( Source )

#include <stdio.h>
#include <conio.h>

char square[10] = {'o', '1', '2', '3', '4', '5', '6', '7', '8', '9'}, name[2][7];

int checkwin();
void board();

int main()
{
	int player = 1, i, choice;
	char mark;

	printf("\nPlayer 1, enter your name: ");
	scanf("%s", name[0]);

	printf("\nPlayer 2, enter your name: ");
	scanf("%s", name[1]);

	printf("\nOK LET'S GO.... ENTER ANY CHARACTER TO START GAME\n\n");
	getch();
	getch();

	do
	{
		board();
		player = (player % 2) ? 1 : 2;

		mark = (player == 1) ? 'X' : 'O';

		printf("%s (%c), choose your box :  ", name[player - 1], mark);
		scanf("%d", &choice);

		if (choice == 1 && square[1] == '1')
			square[1] = mark;

		else if (choice == 2 && square[2] == '2')
			square[2] = mark;

		else if (choice == 3 && square[3] == '3')
			square[3] = mark;

		else if (choice == 4 && square[4] == '4')
			square[4] = mark;

		else if (choice == 5 && square[5] == '5')
			square[5] = mark;

		else if (choice == 6 && square[6] == '6')
			square[6] = mark;

		else if (choice == 7 && square[7] == '7')
			square[7] = mark;

		else if (choice == 8 && square[8] == '8')
			square[8] = mark;

		else if (choice == 9 && square[9] == '9')
			square[9] = mark;

		else
		{
			printf("Invalid move ");

			player--;
			getch();
		}
		i = checkwin();

		player++;
	} while (i == -1);

	board();

	if (i == 1)
		printf("==>\a  %s win\n", name[(--player) - 1]);
	else
		printf("==>\a  Game draw\n");

	getch();

	return 0;
}

/*********************************************

FUNCTION TO RETURN GAME STATUS
1 FOR GAME IS OVER WITH RESULT
-1 FOR GAME IS IN PROGRESS
0 FOR GAME IS OVER AND NO RESULT
 **********************************************/

int checkwin()
{
	if (square[1] == square[2] && square[2] == square[3])
		return 1;

	else if (square[4] == square[5] && square[5] == square[6])
		return 1;

	else if (square[7] == square[8] && square[8] == square[9])
		return 1;

	else if (square[1] == square[4] && square[4] == square[7])
		return 1;

	else if (square[2] == square[5] && square[5] == square[8])
		return 1;

	else if (square[3] == square[6] && square[6] == square[9])
		return 1;

	else if (square[1] == square[5] && square[5] == square[9])
		return 1;

	else if (square[3] == square[5] && square[5] == square[7])
		return 1;

	else if (square[1] != '1' && square[2] != '2' && square[3] != '3' &&
			 square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9')

		return 0;
	else
		return -1;
}

/*******************************************************************
FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
 ********************************************************************/

void board()
{
	clrscr();
	printf("\n\n GAME: (TIC TAC TOE)\n\n");

	printf("     |     |     \n");
	printf("  %c  |  %c  |  %c \n", square[1], square[2], square[3]);

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

	printf("  %c  |  %c  |  %c \n", square[4], square[5], square[6]);

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

	printf("  %c  |  %c  |  %c \n", square[7], square[8], square[9]);

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

/*******************************************************************
END OF PROJECT
 ********************************************************************/

10. By Abhinit Kumar Singh

Made by Abhinit Kumar Singh. ( Source )

#include<stdio.h>
#include<conio.h>
void cheack(char,char);

char a[9]={'1', '2', '3', '4', '5', '6', '7', '8','9'};
void Show()
{

    printf("\n\t\t\t___|___|___");
    printf("\n\t\t\t %c | %c | %c  ",a[0],a[1],a[2]);
    printf("\n\t\t\t___|___|___");
    printf("\n\t\t\t %c | %c | %c  ",a[3],a[4],a[5]);
    printf("\n\t\t\t___|___|___");
    printf("\n\t\t\t %c | %c | %c  ",a[6],a[7],a[8]);
     printf("\n\t\t\t___|___|___");




}
void name()
{

    printf("\n\t\tTic Tac Toe Game . . .\n");
    printf("\tWoner-- ABHINIT KUMAR\n\n");
}
void inputsymbol()
{
    printf("\n\nPlayer 1 symbol: 'X'");
    printf("\n\nPlayer 2 Symbol: '0'\n");

}

int count=0;

void play()
{

    char p,s;
    printf("\nEnter Position Number and Symbol:\n");
    fflush(stdin);
    scanf("%c",&p);
    fflush(stdin);
    scanf("%c",&s);
    count++;

    cheack(p,s);
}

void cheack(char P,char S)
{

    int i;
    for(i=0; i<=8; i++)
    {
        if(a[i]==P)
        {
            a[i]=S;
        }
    }


}
int end()
{
    if((a[0]=='x' && a[1]=='x' && a[2]=='x') || (a[0]=='x' && a[3]=='x' && a[6]=='x') || (a[0]=='x' && a[4]=='x' && a[8]=='x'))

        return(100);

       else if((a[0]=='0' && a[1]=='0' && a[2]=='0') || (a[0]=='0' && a[3]=='0' && a[6]=='0') || (a[0]=='0' && a[4]=='0' && a[8]=='0'))

         return(200);

    else if(a[1]=='x' && a[4]=='x' && a[7]=='x')
        return(100);
    else if(a[1]=='0' && a[4]=='0' && a[7]=='0')
        return(200);
        else if(a[2]=='x' && a[5]=='x' && a[8]=='x')
        return(100);
    else if(a[2]=='0' && a[5]=='0' && a[8]=='0')
        return(200);
        else if(a[3]=='x' && a[4]=='x' && a[5]=='x')
        return(100);
    else if(a[3]=='0' && a[4]=='0' && a[5]=='0')
        return(200);

        else if(a[2]=='x' && a[4]=='x' && a[6]=='x')
        return(100);
    else if(a[2]=='0' && a[4]=='0' && a[6]=='0')
        return(200);
        else if(a[6]=='x' && a[7]=='x' && a[8]=='x')
        return(100);
    else if(a[6]=='0' && a[7]=='0' && a[8]=='0')
        return(200);
    return(300);



}




int main()
{
    int k;
    char ch;
labell:
system("cls");
name();
Show();
inputsymbol();

play();

label:
system("cls");
name();
Show();
play();
k=end();
system("cls");
name();
Show();

if(count<9)
{


if(k==100)
{


    printf("\n \nCongratulations !! Player '1' Won The Game:\n");
    count=0;
}
else if(k==200){
    printf("\n\nCongratulations !! Player '2' Won The Game\n");
    count=0;

}

else
    goto label;
}
else{
    printf("\n Game Draw !!\n");
    count=0;
}

printf("\n Do you want to play Again . . .\n\n Enter Y/y for yes or Enter N/n for No\n");
fflush(stdin);
scanf("%c", &ch);
if(ch=='y' || ch=='Y')
{
   a[0]='1';
   a[1]='2';
   a[2]='3';
   a[3]='4';
   a[4]='5';
   a[5]='6';
   a[6]='7';
   a[7]='8';
   a[8]='9';

    goto labell;

}


getch();
return(0);

}

11. By Natasha

Made by Natasha. ( Source )


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

char square[10]={'0','1','2','3','4','5','6','7','8','9'};
int choice,player;
int check_for_win();
void display_board();
void mark_board(char mark);

int main()
{
    int gamestatus;
    char mark;
    player=1;

    do
    {
        display_board();
        player=(player%2)?1:2;
        printf("\nplayer %d , enter a no.\n",player);
        scanf("%d",&choice);
        mark=(player==1)?'x':'o';
        mark_board(mark);
        gamestatus=check_for_win();
        player++;
    }while(gamestatus==-1);
    if (gamestatus==1)
        printf("\n==>\aPlayer %d win\n",--player);
    else
        printf("\n==>\aGame draw\n");
    return 0;
}
/** function to return game status
1 for game is over with result
-1 for game is in progress
0 game is over and no result **/
int check_for_win()
{
   int returnvalue=0;
   if(square[1]==square[2]&&square[2]==square[3])
        returnvalue=1;
   else  if(square[4]==square[5]&&square[5]==square[6])
        returnvalue=1;
   else  if(square[7]==square[8]&&square[8]==square[9])
        returnvalue=1;
   else  if(square[1]==square[4]&&square[4]==square[7])
        returnvalue=1;
   else  if(square[2]==square[5]&&square[5]==square[8])
        returnvalue=1;
   else  if(square[3]==square[6]&&square[6]==square[9])
        returnvalue=1;
   else  if(square[1]==square[5]&&square[5]==square[9])
        returnvalue=1;
   else  if(square[3]==square[5]&&square[5]==square[7])
        returnvalue=1;
   else  if(square[1]!='1'&&square[2]!='2'&&square[3]!='3'&&square[4]!='4'&&square[5]!='5'&&square[6]!='6'&&square[7]!='7'&&square[8]!='8'&&square[9]!='9')
        returnvalue=0;
   else
        returnvalue=-1;
   return returnvalue;
   }

   /** function to display board**/
   void display_board()
   {
     system("cls");
      printf("\n\nTic Tac Toe\n\n");
     printf("player1 (x)-Player2 (o)\n\n");
     printf("       |     |   \n");
     printf("  %c  |  %c |  %c \n",square[1],square[2],square[3]);
     printf("___ |___|___\n");
     printf("      |      |   \n");
     printf("  %c  |  %c |  %c \n",square[4],square[5],square[6]);
     printf(" ___|___|___\n");
     printf("      |      |   \n");
     printf("  %c  |  %c |  %c \n",square[7],square[8],square[9]);
     printf("      |      |   \n");
  }

  /**set the board with correct char  x or o**/
  void mark_board(char mark)
  {
        if(choice==1&&square[1]=='1')
           square[1]=mark;
        else if(choice==2&&square[2]=='2')
           square[2]=mark;
        else if(choice==3&&square[3]=='3')
           square[3]=mark;
        else if(choice==4&&square[4]=='4')
           square[4]=mark;
        else if(choice==5&&square[5]=='5')
           square[5]=mark;
        else if(choice==6&&square[6]=='6')
           square[6]=mark;
        else if(choice==7&&square[7]=='7')
           square[7]=mark;
        else if(choice==8&&square[8]=='8')
           square[8]=mark;
        else if(choice==9&&square[9]=='9')
           square[9]=mark;
        else 
        {
            printf("invalid no.\n");
            player--;
            getch();
        }
  }

12. By John Piper

Made by John Piper. C Tic Tac Toe game. ( Source )

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAXINDEX 10

void printBoard(char arr[10]);

// clears the screen to show updated array of choice
void clrscr();

// switch function to call functions below
bool checkWinner(char arr[10], char letter, int numberSquare);

// single square check for winner
bool checkOne(char arr[10], char letter);
bool checkTwo(char arr[10], char letter);
bool checkThree(char arr[10], char letter);
bool checkFour(char arr[10], char letter);
bool checkFive(char arr[10], char letter);
bool checkSix(char arr[10], char letter);
bool checkSeven(char arr[10], char letter);
bool checkEight(char arr[10], char letter);
bool checkNine(char arr[10], char letter);

// check if there is a valid square left to play
bool placesLeft(char arr[10]);

// checks and selects a valid column
int playerPick(char arr[10], char playerChar);

// main game logic which calls the logic check functions
char mainGame();

// checks move is valid
bool checkMove(char arr[10], int num);

// check if entry is in the range 1-9
bool checkEntry(int input);

int main() {

    int printResult;

    char gameResult = mainGame();

    // checks how the result will be printed at the end of the game
    if (gameResult != 'D') {
        printResult = (gameResult == 'X') ? 1 : 2;
        printf("player %d wins", printResult);
    }
    else {
        printf("Its a draw");
    }
	return 0;
}
// D = draw X = player one wins O = player Two wins
char mainGame() {
    char board[10] = {'0','1','2','3','4','5','6','7','8','9'};
    bool placeLeft = true;
    char playerChar = 'X';
    int indexChange;
    printBoard(board);
    // continues game unless a player gets 3 in a row or all the squares are taken
    while (placeLeft) {
        indexChange = playerPick(board, playerChar);
        board[indexChange] = playerChar;
        clrscr();
        printBoard(board);
        if (checkWinner(board, playerChar, indexChange)) {
            placeLeft = false;
        }
        else if (placesLeft(board) == false) {
            playerChar = 'D';
            placeLeft = false;
        }
        else {
            playerChar = (playerChar == 'X') ? 'O' : 'X';
        }
    }
    return playerChar;
}

// checks and writes the players pick
int playerPick(char arr[10], char letter) {
    int player;
    int choice;
    bool validChoice = false;
    if (letter = 'X') {
        player = 1;
    }
    else {
        player = 2;
    }
    while (validChoice == false) {
        printf("player %d please select a number: ", player);
        scanf("%d", &choice);
        if (checkEntry(choice) == false)
            printf("Invalid number input plase choose again: ");
        else {
            validChoice = checkMove(arr, choice);
                if(validChoice == false) {
                printf("Invalid Move please choose again: ");
            }
        }
    }
    return choice;
}

// prints the updated array in tic tac toe format
void printBoard(char arr[10]) {

    // print title and player headers
    printf("\n\t\tTic Tac Toe\n\n");
    printf("\tplayer 1 (X) = player 2 (O)\n\n\n");

    int index = 1;

    while(index < MAXINDEX) {

        // boarder no num y axis
        printf("\t \t|\t \t|\n");
        // boarder num y axis
        printf("\t%c\t|\t%c\t|\t%c\n", arr[index], arr[index+1], arr[index+2]);
        // boarder no num y axis
        printf("\t \t|\t \t|\n");

        // to avoid an extra x axis line on the grid
        if (index <= 4) {
        // boarder x axis
        printf("________________|_______________|_______________\n");
        }
        index +=3;
    }

    return;
}

// clears the screen to allow updated array
void clrscr()
{
    system("@cls||clear");
}

bool checkMove(char arr[10], int num) {
    bool legalMove = true;
    if (arr[num] == 'X' || arr[num] == 'O') {
        legalMove = false;
    }
    return legalMove;
}

bool checkEntry(int input) {
    bool validInput = false;
    int numArray[9] = {1,2,3,4,5,6,7,8,9};
    for (int i=0; i<9; i++) {
        if (numArray[i] == input) {
            validInput = true;
            break;
        }
    }
    return validInput;
}

bool placesLeft(char arr[10]) {
    bool placeLeft = false;
    for (int i=1; i<MAXINDEX; i++) {
        if (arr[i] != 'X' && arr[i] != 'O') {
            placeLeft = true;
            break;
        }
    }
    return placeLeft;
}

// checks from the current played position if 3 of the same are in a row
bool checkWinner(char arr[10], char letter, int numberSquare) {
    // winner boolean
    bool isWinner;
    // switch to check move is winner
    switch (numberSquare) {
    case 1:
        isWinner = checkOne(arr, letter);
        break;
    case 2:
        isWinner = checkTwo(arr, letter);
        break;
    case 3:
        isWinner = checkThree(arr, letter);
        break;
    case 4:
        isWinner = checkFour(arr, letter);
        break;
    case 5:
        isWinner = checkFive(arr, letter);
        break;
    case 6:
        isWinner = checkSix(arr, letter);
        break;
    case 7:
        isWinner = checkSeven(arr, letter);
        break;
    case 8:
        isWinner = checkEight(arr, letter);
        break;
    case 9:
        isWinner = checkNine(arr, letter);
        break;
    }
    return isWinner;
}

// logic for checking 3 in a row
bool checkOne(char arr[10], char letter) {
    bool isWinner = false;
    if (letter == arr[2] && arr[2] == arr[3] || letter == arr[4] && arr[4] == arr[7] || letter == arr[5] && arr[5] == arr[9]) {
        isWinner = true;
    }
    return isWinner;
}

bool checkTwo(char arr[10], char letter) {
    bool isWinner = false;
    if (letter == arr[5] && arr[5] == arr[8]) {
            isWinner = true;
    }
    return isWinner;
}

bool checkThree(char arr[10], char letter) {
    bool isWinner = false;
    if (letter == arr[1] && arr[1] == arr[2] || letter == arr[5] && arr[5] == arr[7] || letter == arr[6] && arr[6] == arr[9]) {
        isWinner = true;
    }
    return isWinner;
}

bool checkFour(char arr[10], char letter) {
    bool isWinner = false;
    if (letter == arr[5] && arr[5] == arr[6]) {
        isWinner = true;
    }
    return isWinner;
}

bool checkFive(char arr[10], char letter) {
    bool isWinner = false;
    if (letter == arr[1] && arr[1] == arr[9] || letter == arr[2] && arr[2] == arr[8] || letter == arr[3] && arr[3] == arr[7] || letter == arr[4] && arr[4] == arr[6]) {
        isWinner = true;
    }
    return isWinner;
}

bool checkSix(char arr[10], char letter) {
    bool isWinner = false;
    if (letter == arr[4] && arr[4] == arr[5] || letter == arr[3] && arr[3] == arr[9]) {
        isWinner = true;
    }
    return isWinner;
}

bool checkSeven(char arr[10], char letter) {
    bool isWinner = false;
    if (letter == arr[1] && arr[1] == arr[4] || letter == arr[5] && arr[5] == arr[3] || letter == arr[8] && arr[8] == arr[9]) {
        isWinner = true;
    }
    return isWinner;
}

bool checkEight(char arr[10], char letter) {
    bool isWinner = false;
    if (letter == arr[5] && arr[5] == arr[2]) {
        isWinner = true;
    }
    return isWinner;
}

bool checkNine(char arr[10], char letter) {
    bool isWinner = false;
    if (letter == arr[1] && arr[1] == arr[5] || letter == arr[7] && arr[7] == arr[8] || letter == arr[3] && arr[3] == arr[6]) {
        isWinner = true;
    }
    return isWinner;
}

13. By William Owens

Made by William Owens. Enter one number, one player at a time. ( Source )

/*
1 | 2 | 3
---------
4 | 5 | 6     << is the setup of the board
---------
7 | 8 | 9
Example:
1       << This would be Player 1
2       << This would be Player 2
5       << This would be Player 1
3       << This would be Player 2
9       << This would be Player 1
The above example would run the program with Player 1 as a winner.
Created by William R. Owens "owensjrw", An old dog tring to learn a new trick.
Edits and collaboration with Moriarty1982.
*/

#include <stdio.h>

int main()
{
//Declare variables
    char board[] = {'1','2','3','4','5','6','7','8','9'};
    int player = 1;
    int moves = 0;
    int game_end = 0;
    int input;

//Set up Players
    puts("Two player console Tic-Tac-Toe");
    puts("Player one is 'X'");
    puts("Player two is '@'");
    puts("\n");

//Start Game
    do {

//Print Board
        puts("\n");
        printf("%c | %c | %c\n", board[0], board[1], board[2]);
        puts("---------");
        printf("%c | %c | %c\n", board[3], board[4], board[5]);
        puts("---------");
        printf("%c | %c | %c\n\n", board[6], board[7], board[8]);

//Check for game_end annouce winner.
        if (game_end == 1) {
            printf("** Congragulations Player %d. You have won the game. **\n", player);
            break;
        }

//Get user input
        int is_num;
        do {
            printf("Player %d what square do you want? ", player);
            is_num = scanf(" %d", &input);
            while(getchar() != '\n');
        } while (is_num != 1 || (input < 1 || input > 9));  //User can still enter a float type number, digit prior to '.' will be tested
        input--;  //Equate var square to indices of board

//Check if square is already used
        if (board[input] == 'X' || board[input] == '@') {
            puts("That square is already taken, please choose again.\n");
            continue;
        }

//If square is open assign user token to square.
        board[input] = player == 1 ? 'X' : '@';
        moves++;

//Test for win, tie, or player swap.
        if (
            (board[0] == board[1] && board[1] == board[2]) ||
            (board[3] == board[4] && board[4] == board[5]) ||
            (board[6] == board[7] && board[7] == board[8]) ||
            (board[0] == board[3] && board[3] == board[6]) ||
            (board[1] == board[4] && board[4] == board[7]) ||
            (board[2] == board[5] && board[5] == board[8]) ||
            (board[0] == board[4] && board[4] == board[8]) ||
            (board[2] == board[4] && board[4] == board[6])
        ){
            game_end = 1;
            continue;
        } else if (moves == 9) {
            puts("Tie Game. Better luck next time.\n");
            break;
        } else {
            player = player == 1 ? 2 : 1;
        }
    } while (1);
}

14. By @coder_rahult

Made by @coder_rahult. ( Source )

#include<stdio.h>
int main()

{
  int i,j,b,x,a[10],c[10]={0};
  printf(" Tic-tac-toe Game:\n\n");
  printf("Order of positions:\n");
  for(i=1;i<=9;i++)
  {
    a[i]=' ';
    printf("%3d",i);
    if(i%3==0)
    printf("\n\n");
  }
  for(j=1;j<=9;j++){
  if(j%2!=0){
    printf("\nplayer A's Turn\n");
    x='A';
  }
  else{
    printf("\nplayer B's Turn\n");
    x='B';
  }
   scanf("%d",&b);
   if(b>9){
     printf("Invalid move\n");
   }
   for(i=1;i<=9;i++){
      if(b==c[i]){
         printf("You are cheating");
         goto stop;
      }
   }
   c[j]=b;
   if(j%2!=0){
     a[b]='X';
   }
   else{
     a[b]='O';
   }
    
   for(i=1;i<=9;i++){
       if(i==1)
          printf("\n-------------\n");
          printf("|%2c ",a[i]);
       if(i%3==0){
          printf("|");
          printf("\n-------------\n");
       }
     }
     if(a[1]==a[2]&&a[1]==a[3]&&a[1]!=' '){
        printf("%c won",x);
        goto stop;
     }
     else if(a[4]==a[5]&&a[4]==a[6]&&a[4]!=' '){
        printf("%c won",x);
        goto stop;
     }
     else if(a[7]==a[8]&&a[7]==a[9]&&a[7]!=' '){
        printf("%c won",x);
        goto stop;
     }
     else if(a[1]==a[4]&&a[1]==a[7]&&a[1]!=' '){
        printf("%c won",x);
        goto stop;
     }
     else if(a[2]==a[5]&&a[2]==a[8]&&a[2]!=' '){
        printf("%c won",x);
        goto stop;
     }
     else if(a[3]==a[6]&&a[3]==a[9]&&a[3]!=' '){
        printf("%c won",x);
        goto stop;
     }
     else if(a[1]==a[5]&&a[1]==a[9]&&a[1]!=' '){
        printf("%c won",x);
        goto stop;
     }
     else if(a[3]==a[5]&&a[3]==a[7]&&a[3]!=' '){
        printf("%c won",x);
        goto stop;
     }
   }
   printf("Draw");
   stop:
   {
    printf("\nGame over");
    }
}

15. By PATEL UMANGKUMAR KANUBHAI

Made by PATEL UMANGKUMAR KANUBHAI. ( Source )

/*  TIC TAC TOE */
/* RUN THIS PROGRAM TO CODEBLOCK OR OTHER COMPILER */

include <stdio.h>
char square[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int checkwin();
void box();


int main()
{
    int player = 1, i, choice;
    char mark;
    do
    {
        box();
        player=(player%2)?1:2;
        printf("Player %d, enter a number:  ", player);
        scanf("%d", &choice);
        mark = (player == 1) ? 'X' : 'O';
   if (choice == 1 && square[1] == '1')
            square[1] = mark;          
        else if (choice == 2 && square[2] == '2')
            square[2] = mark;           
        else if (choice == 3 && square[3] == '3')
            square[3] = mark;            
        else if (choice == 4 && square[4] == '4')
            square[4] = mark;            
        else if (choice == 5 && square[5] == '5')
            square[5] = mark;            
        else if (choice == 6 && square[6] == '6')
            square[6] = mark;            
        else if (choice == 7 && square[7] == '7')
            square[7] = mark;           
        else if (choice == 8 && square[8] == '8')
            square[8] = mark;            
        else if (choice == 9 && square[9] == '9')
            square[9] = mark;            
        else
        {
            printf("Invalid move ");
            player--;
            getch();
        }
        i = checkwin();
        player++;
    }while (i ==  - 1);    
    box();    
    if (i == 1)
        printf("==>\aPlayer %d win ", --player);
    else
        printf("==>\aGame draw");
    getch();
    return 0;
}


int checkwin()
{
    if (square[1] == square[2] && square[2] == square[3])
        return 1;        
    else if (square[4] == square[5] && square[5] == square[6])
        return 1;        
    else if (square[7] == square[8] && square[8] == square[9])
        return 1;        
    else if (square[1] == square[4] && square[4] == square[7])
        return 1;      
    else if (square[2] == square[5] && square[5] == square[8])
        return 1;    
    else if (square[3] == square[6] && square[6] == square[9])
        return 1;        
    else if (square[1] == square[5] && square[5] == square[9])
        return 1;        
    else if (square[3] == square[5] && square[5] == square[7])
        return 1;       
    else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9')
        return 0;
    else
        return  - 1;
}


void box()
{    
    printf("\n\n\tTic Tac Toe\n\n");
    printf("Player 1 (X)  -  Player 2 (O)\n\n\n");
    printf("     |     |     \n");
    printf("  %c  |  %c  |  %c \n", square[1], square[2], square[3]);
    printf("_____|_____|_____\n");
    printf("     |     |     \n");
    printf("  %c  |  %c  |  %c \n", square[4], square[5], square[6]);
    printf("_____|_____|_____\n");
    printf("     |     |     \n");
    printf("  %c  |  %c  |  %c \n", square[7], square[8], square[9]);
    printf("     |     |     \n\n");
}

16. By Samuel Gauthier

Made by Samuel Gauthier. ( Source )

#include <stdio.h>
#include <stdlib.h>

/// RUN IN AN IDE

#define O 'O'
#define X 'X'
#define BLANK ' '
#define NB_SQR 9

void clrs(){
    system("cls");
}

#define CHECK_WIN(p)        if(square[0] == p && square[1] == p && square[2] == p){\
                                win = p;\
                                break;\
                            } else if(square[3] == p && square[4] == p && square[5] == p) {\
                                win = p;\
                                break;\
                            } else if(square[6] == p && square[7] == p && square[8] == p) {\
                                win = p;\
                                break;\
                            } else if(square[0] == p && square[3] == p && square[6] == p) {\
                                win = p;\
                                break;\
                            } else if(square[1] == p && square[4] == p && square[7] == p) {\
                                win = p;\
                                break;\
                            } else if(square[2] == p && square[5] == p && square[8] == p) {\
                                win = p;\
                                break;\
                            } else if(square[0] == p && square[4] == p && square[8] == p) {\
                                win = p;\
                                break;\
                            } else if(square[2] == p && square[4] == p && square[6] == p) {\
                                win = p;\
                                break;\
                            }

int main()
{
    char square[NB_SQR] = {BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK};
    int num_square = 0;
    char player = O, win;

    for(int i=0; i<NB_SQR; i++){
    printf("======= TIC TAC TOE =======\n\n\nPlayer: %c\nTurns: %d\n\n\n   %c | %c | %c  \n ----+---+----\n   %c | %c | %c \n ----+---+----\n   %c | %c | %c  \n\n",
            player, i, square[0], square[1], square[2], square[3], square[4], square[5], square[6], square[7], square[8]);

    scanf("%d", &num_square);

    if(i%2 == 0){
        if(square[num_square-1] == BLANK){
            square[num_square-1] = O;
            player = X;
        } else
            i--;
    }
    else{
        if(square[num_square-1] == BLANK){
            square[num_square-1] = X;
            player = O;
        }
        else
            i--;
    }

    clrs();

    CHECK_WIN(O)
    CHECK_WIN(X)
    }

    if(win == O)
        printf("PLAYER O WIN!!!");
    if(win == X)
        printf("PLAYER X WIN!!!");

    return 0;
}

17. By Tajender Singh

Made by Tajender Singh. ( Source )

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int skip=0,p1=0,p2=0,draw=0;

void input(int X[3][3], int k, int l, int p)
{

        for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            
            if(X[i][j]==5)
            {
            
                if((i==k && j==l)&&p==1)
                    X[i][j]=1;
                
                else if((i==k && j==l)&&p==0)
                    X[i][j]=0;
                    
                else
                    continue;
                
            }
            
        }
        


    }
        
}



void disp(int Y[3][3])
{    
    printf("\n\n");
    for(int x=0; x<3; x++)
    {
        for(int y=0; y<3; y++)
        {
            if(Y[x][y]==1)
            {
                printf("X ");
                printf("(%d,%d)",x,y);
                if(y<2)
                    printf(" | ");
            }
            
            
            else if(Y[x][y]==0)
            {
                printf("O ");
                printf("(%d,%d)",x,y);
                if(y<2)
                    printf(" | ");
            }
            
            else 
            {
                printf("- ");
                printf("(%d,%d)",x,y);
                if(y<2)
                    printf(" | ");
            }    
            
        }
        
        if(x<2)
            printf("\n _______________________\n\n");
            
    }
    
        printf("\n\n");
}


void disp2(int Y[3][3])
{
    for(int x=0; x<3; x++)
    {
        printf("\t\t\t\t\t\t\t");
        for(int y=0; y<3; y++)
        {
            if(Y[x][y]==1)
            {
                printf("X ");
                if(y<2)
                    printf(" | ");
            }
            
            
            else if(Y[x][y]==0)
            {
                printf("O ");
                if(y<2)
                    printf(" | ");
            }
            
            else 
            {
                printf("  ");
                if(y<2)
                    printf(" | ");
            }
            
        }
        if(x<2)
            printf("\n    _______________\n\n");
    }
}


void win(int p)
{
    
    if(p==1)
    {    
        printf("\n\n\t\tPLAYER 1 WINS!!!");
        skip=1;
        p1++;
        
        getch();
    
        for( int i=0; i<10; i++)
        {    
            printf("\n");
            for(int j=100-i; j>0; j-- )
                printf("\tPLAYER 1!!!");
        }
        
    }
    
    else if(p==0)
    {
        printf("\n\n\t\tPLAYER 2 WINS!!!");
        skip=1;
        p2++;
        
        getch();
    
        for( int i=0; i<10; i++)
        {    
            printf("\n");
            for(int j=100-i; j>0; j-- )
                printf("\tPLAYER 2!!!");
        }
        
    }
    
        
}


void check_row(int Z[3][3])
{
    for(int i=0; i<3; i++)
    {    
        int n=0, m=0;
        for(int j=0; j<3; j++)
        {
            if(Z[i][j]==1)
                n++;
                
            else if(Z[i][j]==0)
                m++;
    
        }
        
        if(n==3)
            win(1);
        
        if(m==3)
            win(0);    
            
    }
    
}


void check_col(int Z[3][3])
{
    for(int i=0; i<3; i++)
    {    
        int n=0, m=0;
        for(int j=0; j<3; j++)
        {
            if(Z[j][i]==1)
                n++;
                
            else if(Z[j][i]==0)
                m++;
    
        }
        
        if(n==3)
            win(1);
        
        if(m==3)
            win(0);    
            
    }
    
}



void check_dia1(int Z[3][3])
{
    int n=0, m=0;
    for(int i=0; i<3; i++)
    {    
    
        for(int j=0; j<3; j++)
        {
            
            if(i+j==2)
            {
                if (Z[i][j]==1)
                    n++;
                
                else if(Z[i][j]==0)
                    m++;
            }
            
        }
        
        if(n==3)
            win(1);
        
        if(m==3)
            win(0);    
            
    }
    
}

void check_dia2(int Z[3][3])
{    
    int n=0, m=0;
    for(int i=0; i<3; i++)
    {    
        
        for(int j=0; j<3; j++)
        {
            if(i==j)
            {
                if(Z[i][j]==1)
                    n++;
            
                else if(Z[i][j]==0)
                    m++;
    
            }
        }
        
        if(n==3)
            win(1);
        
        if(m==3)
            win(0);    
            
    }
    
}


void score()
{
        printf("\n\n***SCORE BOARD***\n\n");
    
    printf("\nPLAYER 1 : %d\n", p1);
    printf("\nPLAYER 2 : %d\n", p2);
    printf("\nDRAWS : %d\n", draw);
    printf("\n\n*****************\n\n");
}


int main()
{    
    int A[3][3],a,b,m,count;
    char ch='y';
    
    printf("\n\tTIC TAC TOE\n\n\n");
    
    
    
    do
    {
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
                A[i][j]=5;
        }

    }
    
    printf("\nREFERENCE\n\n");
    
    disp(A);
    
    printf("\nPLAYER 1 : X\n");
    printf("\nPLAYER 2 : O\n\n");
    
    score();
    
    
    for( int d=1; d<=9; d++)
    {    
        count++;
        if(d%2!=0)
        {    m=1;
            do{
                printf("\nPLAYER 1'S TURN - X (x,y) : ");
                scanf("%d %d", &a, &b);
                
                if(A[a][b]!=5)
                    printf("\n OVERWRITING NOT ALLOWED!\n");
                
                else if(((a>2)||(b>2)) || (A[a][b]!=5))
                {
                    printf("\n INVALID INPUT! TRY AGAIN!\n");
                    A[a][b]=-1;
                }
                
            
                    
              }
              while(A[a][b]!=5);
              
              input(A,a,b,m);
        }
        
        else 
        {    m=0;
            do{
                printf("\nPLAYER 2'S TURN - O (x,y) : ");
                scanf("%d %d", &a, &b);
                
                if(A[a][b]!=5)
                    printf("\n OVERWRITING NOT ALLOWED!\n");
                
                else if(((a>2)||(b>2)) || (A[a][b]!=5))
                {
                    printf("\n INVALID INPUT! TRY AGAIN!\n");
                    A[a][b]=-1;
                }
                
                
              }
              while(A[a][b]!=5);
              
              input(A,a,b,m);
        }
        
        printf("\n\n");
        disp2(A);
        disp(A);
        printf("\n\n");
        
        check_row(A);
        check_col(A);
        check_dia1(A);
        check_dia2(A);
        
        if(skip==1)
            break;        
    }
    if(count==9)
    {
        printf("\nDRAW MATCH!");
        draw++;
    }
    printf("\n\nWant to do another MATCH? (y/n) : ");
    ch=getch();
    
    if(ch=='y')
        skip=0;
    
    }while(ch=='y');    
    
    score();
    
    return 0;
}