Tic Tac Toe Game

Write a C Program to play tic tac game(circle and cross or ‘O’ and ‘X’).

The tic-tac-toe game is played on a 3×3 grid with ‘O'(circle) and ‘X'(cross) symbol. This game is played by two players, who take turns. if the first player marks move with a circle, then the second marks move with a cross and if the first player chooses cross then the second player will have a circle as his mark. The player who has formed a horizontal, vertical, or diagonal sequence of three similar marks either crosses or circles wins the game.

So in this code, you have to draw a game board, ask the user for the coordinates between 0 to 9 for the next mark, change the players after every successful move based on the coordinates passed, if he selects invalid move you can prompt to the user for informing about the invalid move, and at last, you can say the game status win or tie and can ask if the user wants to play it again or not.

Try it by yourself first, rather than looking for code directly.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
 
int mark = 0;
char a[11] = "x         ";
char p1,p2;
 
enum game_state{NONE,PLAYER_1,PLAYER_2,GAME_WON,GAME_TIE,GAME_RUNNING,GAME_EXIT};
 
void display();
int set_choices_for_players();
int win_tied_check(char);
int place_XorO_to_position(int pos,char c);
int players_turn(int);
 
int main()
{
    printf("Welcome to the TIC TAC TOE in C Programming\n");
    mark = set_choices_for_players();
    printf("User %d will go first\n",mark+1);
    sleep(1);
    printf("\n");
    display();
 
    if(mark == 0)   
    {
        while(1)
        {
            while(GAME_EXIT != players_turn(PLAYER_1) && GAME_EXIT != players_turn(PLAYER_2));
            return EXIT_FAILURE;
                 
        }
    }
    else if(mark==1)
    {   
        while(1)
        {
            while(GAME_EXIT != players_turn(PLAYER_2) && GAME_EXIT != players_turn(PLAYER_1));
            return EXIT_FAILURE;
        }
    }
    return EXIT_SUCCESS;
}
 
int place_XorO_to_position(int pos,char c)
{
    int i;
    system("clear");
    printf("\n");
     
    a[pos] = c;
    display();
 
    return win_tied_check(c);   
     
    return 1;
}
 
int win_tied_check(char c)
{
    if((a[1] == a[2] && a[2] == a[3] && a[1] != ' ' && a[2] != ' ' && a[3] != ' ')\
    || (a[4] == a[5] && a[5] == a[6] && a[4] != ' ' && a[5] != ' ' && a[6] != ' ')\
    || (a[7] == a[8] && a[8] == a[9] && a[7] != ' ' && a[8] != ' ' && a[9] != ' ')\
    || (a[1] == a[4] && a[4] == a[7] && a[1] != ' ' && a[4] != ' ' && a[7] != ' ')\
    || (a[2] == a[5] && a[5] == a[8] && a[2] != ' ' && a[5] != ' ' && a[8] != ' ')\
    || (a[3] == a[6] && a[6] == a[9] && a[3] != ' ' && a[6] != ' ' && a[9] != ' ')\
    || (a[1] == a[5] && a[5] == a[9] && a[1] != ' ' && a[5] != ' ' && a[9] != ' ')\
    || (a[3] == a[5] && a[5] == a[7] && a[3] != ' ' && a[5] != ' ' && a[7] != ' '))
    {
        return GAME_WON;
    }
    else if(a[1] != ' ' && a[2] !=' ' && a[3] !=' '
    && a[4] != ' ' && a[5] !=' ' && a[6] !=' '
    && a[7] != ' ' && a[8] !=' ' && a[9] !=' ')
        return GAME_TIE;
    else
        return GAME_RUNNING;
}
 
void display()
{
    system("clear");
    printf("\n");
    printf("|    %c    |    %c    |    %c    |\n\n\n\n",a[1],a[2],a[3]);
    printf("|    %c    |    %c    |    %c    |\n\n\n\n",a[4],a[5],a[6]);
    printf("|    %c    |    %c    |    %c    |\n\n\n\n",a[7],a[8],a[9]);
}
 
int set_choices_for_players()
{
    char ch;
    int m;
    printf("Do you want to be \'X\' or \'O\' :  ");
    scanf(" %c",&ch);
    if(ch == 'X' || ch == 'x')
    {   
        p1 = 'X';
        p2 = 'O';
    }   
    else if(ch == 'O' || ch == 'o')
    {
        p1 = 'O';
        p2 = 'X';
    }
    else
    {
        printf("Invalid selection\n");
    }   
    m = rand()%2;
    return m;
}
 
int players_turn(int val)
{
    int res;
    int pos,op;
    if(val == PLAYER_1)
        printf("Enter Position User 1: ");
    else
        printf("Enter Position User 2: ");
         
    scanf("%d",&pos);
 
    while(pos<=0 || pos >9 ||  a[pos] != ' ')
    {
        printf("Invalid move\n");
        usleep(500000);
        display();
     
        if(val == 1)
            printf("Enter Position User 1: ");
        else
            printf("Enter Position User 2: ");
         
        scanf("%d",&pos);
    }
         
    if(val == PLAYER_1)
        res = place_XorO_to_position(pos,p1);
    else
        res = place_XorO_to_position(pos,p2);
    if(res == GAME_WON || res == GAME_TIE)
    {       
        if(res == GAME_WON && val == PLAYER_1)
            printf("%c (User 1) has won\n",p1);
        else if(res == GAME_WON && val == PLAYER_2)
            printf("%c (User 2) has won\n",p1);
        else
            printf("Game tied\n");
        printf("Do you want to replay the game ? \n0] No 1] Yes\n");
        scanf("%d",&op);
        if(op == 0)
        {
            printf("Thanks for playing\nGood bye...\n");
            return GAME_EXIT;
        }
        strcpy(a,"x         ");
        printf("\n");
        mark = set_choices_for_players();
        printf("User %d will go first\n",mark+1);
        sleep(1);
        display();
    }
    return 0;
 
}

Output