Tic Tac Toe Game in C | C Project - codeitwise

Here we are going to build tic tac toe game in c language. It is going to be a tic tac toe c program against computer i.e., you will be playing against computer. Tic Tac Toe game is one of the best c project for you to build if you are a beginner.

We have used vs code for writing c program for tic tac toe. Below you will find the complete  tic tac toe c code but before proceeding let us know some important things.


tic tac toe codeitwise



We are going to use 2D array as two-dimensional arrays are commonly used to simulate board game matrices. The computer plays very simple game. When it is computer's turn, it uses get_computer_move() function to scan the matrix, looking for an empty cell. When it finds one, it puts O there. get_player_move() function asks you where you want to put X. You will be asked to input x,y co-ordinates. The upper-left corner is 1,1, upper-middle is 1,2, upper-right corner is 1,3, middle-left is 2,1, middle-middle is 2,2, middle-right is 2,3, lower-left is 3,1, lower-middle is 3,2, and lower-right is 3,3.

Each time a move has been made, the program calls check() function. This function returns space if there is no winner, X if you have won, or O if computer has won. It scans the rows, columns, and the diagonals looking for one that contains wither all X's or all O's.

The disp_matrix() function displays the current state of the game.

Now that you have understood the logic let's write tic tac toe c program and in this program we will use functions and array. So it can also be said that it is a tic tac toe c program using array or tic tac toe c program using function.


Tic Tac Toe Game in C-


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

char matrix[3][3];
char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);

int main(void)
{
    char done;

    printf("This is the game of tic tac toe \n");
    printf("You will be playing against computer \n");

    done= ' ';
    init_matrix();

    do
    {
        disp_matrix();
        get_player_move();
        done=check(); //see if winner
        if(done!= ' ') break; //winner!
        get_computer_move();
        done=check(); //see if winner
    } while (done== ' ');
    
    if(done=='X') printf("You Won! \n");
    else printf("I Won! \n");
    disp_matrix(); //show final position

    return 0;
}

//initialize the matrix
void init_matrix(void)
{
    int i,j;

    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            matrix[i][j]= ' ';
        }
    }
}

//get a players move
void get_player_move(void)
{
    int x,y;
    printf("Enter X Y coordinate for your move: ");
    scanf("%d%*c%d",&x,&y);
    x--;
    y--;

    if(matrix[x][y]!= ' ')
    {
        printf("Invalid move, try again. \n");
        get_player_move();
    }
    else
    {
        matrix[x][y]='X';
    }
}
//get a move from computer
void get_computer_move(void)
{
    int i,j;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            if(matrix[i][j]==' ') break;

        }
        if(matrix[i][j]==' ') break;
    }

    if(i*j==9)
    {
        printf("draw! \n");
        exit(0);
    }
    else{
        matrix[i][j]='O';
    }
}

//display matrix on screen

void disp_matrix(void)
{
    int t;
    for(t=0;t<3;t++)
    {
        printf("%c | %c | %c ",matrix[t][0],matrix[t][1],matrix[t][2]);
        if(t!=2)
        {
            printf("\n--|---|--\n");
        }
    }
    printf("\n");
}

//see if there is a winner

char check(void)
{
    int i;
    for(i=0;i<3;i++) //check rows
    {
        if(matrix[i][0]==matrix[i][1] && matrix[i][0]==matrix[i][2])
        {
            return matrix[i][0];
        }
    }

    for(i=0;i<3;i++) //check columns
    {
        if(matrix[0][i]==matrix[1][i] && matrix[0][i]==matrix[2][i])
        {
            return matrix[0][i];
        }
    }

    //test diagonals
    if(matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) return matrix[0][0];

    if(matrix[0][2]==matrix[1][1] && matrix[1][1]==matrix[2][0]) return matrix[0][2];

    return ' ';
}

Output-


This is the game of tic tac toe 
You will be playing against computer 
  |   |   
--|---|--
  |   |   
--|---|--
  |   |
Enter X Y coordinate for your move: 11
11
Invalid move, try again. 
Enter X Y coordinate for your move: 1 1
X | O |   
--|---|--
  |   |
--|---|--
  |   |   
Enter X Y coordinate for your move: 1 2
Invalid move, try again. 
Enter X Y coordinate for your move: 1 3
X | O | X 
--|---|--
O |   |   
--|---|--
  |   |   
Enter X Y coordinate for your move: 2 2
X | O | X 
--|---|--
O | X | O 
--|---|--
  |   |
Enter X Y coordinate for your move: 3 1
You Won! 
X | O | X 
--|---|--
O | X | O
--|---|--
X |   |     


Post a Comment

If you have any doubts, please let me know

Previous Post Next Post