Tic Tac Toe Code in C Programming with Source Code

The Tic Tac Toe Code In C Programming is developed using C programming language. This game is played on a 3×3 grid by two players.

A tic tac toe game c program first player uses a circle to mark his movements, while the second uses a cross. The tic tac toe using c player who has made a three-mark horizontal, vertical, or diagonal series wins.

Additionally, both players must enter a particular number one to nine based on the grid position in order to make a mark X or O.

About The Project

The tic tac toe program in c 3×3 winner will be the first player to successfully position three of their marks in a horizontal, vertical, or diagonal) row.

This Console Application also includes a downloadable code for tic tac toe in c for free, just find the downloadable source code below and click to start downloading.

To run this tic tac toe program in c with output make sure that you have a Code Blocks or any platform of C++ installed in your computer.

Tic Tac Toe In C Programming Project Details

ABOUT PROJECTPROJECT DETAILS
Project Name :tic tac toe algorithm
Project Platform :C/C++
Programming Language Used:C Programming Language
Developer Name :itsourcecode.com
IDE Tool (Recommended):Dev-C++/Codeblocks
Project Type :Desktop Application
Database:Stores data in .DAT file
Tic Tac Toe In C Programming With Source Code Information

Tic Tac Toe In C Programming With Source Code Steps On How To Run The Project

Time needed: 5 minutes

These are the steps on how to run Tic Tac Toe In C Programming With Source Code

  • Step 1: Download

    First, download the source code given below.

    Tic-Tac-Toe-Game-In-C-Step-1

  • Step 2: Extract file.

    Second, after you finished download the source code, extract the zip file.
    Tic-Tac-Toe-Game-In-C-Step-2

  • Step 3: Open CodeBlocks

    Third, open “CodeBlocks IDE”.

  • Step 4: Open Project.

    Fourth, open file tab and Open File after that open folder TicTacToeC then click the “tictactoec.c“.

    Tic-Tac-Toe-Game-In-C-Step-4

  • Step 5: Run Project

    Fifth, run the project.
    Tic-Tac-Toe-Game-Program-In-C-Step-5

  • Step 6: The actual code.

    You are free to copy the given source code below or download the downloadable source code given.

The Code Given Below is the Actual Code of System

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

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

int checkwin();
void board();

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

    char mark;
    do
    {
        board();
        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);

    board();

    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;
}


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


void board()
{
    system("cls");
    printf("\n\n   \xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\n");
    printf("\t   Tic Tac Toe");
    printf("\n   \xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\n\n");

    printf("   Player 1 (X)  -  Player 2 (O)\n\n");


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

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

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

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

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

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

 

Downloadable Source Code

Conclusion

This tic tac toe algorithm in c is only a project made for school requirement purposes only. You can download this source code and modify it to suit your client requirements, since this is a student project it means you cannot really expect 100% functionality from this.

Leave a Comment