Subscribe Us

Tic Tac Toe Game In C

Tic Tac Toe Game In C

Tic-Tac-Toe
is a classic two-player game played on a grid of 3x3 squares. The goal of the game is to be the first player to form a line of three matching symbols (typically 'X' or 'O') in a horizontal, vertical, or diagonal row on the grid.

Here are the basic rules of Tic-Tac-Toe:

  1. The game is played on a 3x3 grid.
  2. Two players take turns marking an empty cell on the grid with their respective symbols ('X' and 'O').
  3. Player 1 usually plays with 'X', and Player 2 plays with 'O'.
  4. Players alternate turns until either one player wins or the game ends in a draw.
  5. A player wins the game if they have three of their symbols in a row (horizontally, vertically, or diagonally).
  6. If all cells on the grid are filled and no player has won, the game ends in a draw.

Tic-Tac-Toe is a simple yet strategic game that requires players to think ahead and plan their moves to block the opponent's line while trying to create their own line of symbols. It can be played on a physical board or using a computer program, with the objective of achieving three symbols in a row before the opponent.

Here's a basic algorithm outline for a Tic-Tac-Toe game where two human players can play against each other in a console program:

  1. Create a 3x3 grid as the game board to represent the Tic-Tac-Toe layout.
  2. Initialize the board with empty cells or a default value to indicate no moves have been made yet.
  3. Create a variable to track the current player, starting with Player 1.
  4. Display the initial empty game board.
  5. Implement a loop that continues until the game is over or a draw is reached:
    1. rompt the current player to enter their move (row and column) on the board.
    2. Check if the move is valid:
      • The chosen cell must be within the boundaries of the board.
      • The chosen cell must be empty (not already marked by a player).
    3. If the move is valid, update the board with the player's symbol (e.g., 'X' or 'O') at the chosen cell.
    4. Display the updated game board.
    5. Check if the current player has won the game:
      • Check for three consecutive symbols in a row, column, or diagonal.
    6. If the current player has won, display a victory message and end the game.
    7. If the game board is full and no player has won, display a draw message and end the game.
    8. Switch the current player to the other player (e.g., from Player 1 to Player 2).
  6. After the game ends, ask the players if they want to play again. If yes, reset the board and start a new game. If not, exit the program.

Note: This is a basic algorithm outline, and you can extend it to include more advanced features such as error handling, input validation, and user interface enhancements.

#include<stdio.h>
#include<conio.h>
int  printPats(int mat[3][3],char name1[],char name2[])
 {
  int Rows,Cols;
  int status=0;
  textcolor(YELLOW);
  gotoxy(34,2); cprintf("TIC TAC TOE GAME ");
  gotoxy(34,7);cprintf("%c",mat[0][0]);
  gotoxy(40,7);cprintf("%c",mat[0][1]);
  gotoxy(46,7);cprintf("%c",mat[0][2]);

  gotoxy(34,9);cprintf("%c",mat[1][0]);
  gotoxy(40,9);cprintf("%c",mat[1][1]);
  gotoxy(46,9);cprintf("%c",mat[1][2]);


  gotoxy(34,11);cprintf("%c",mat[2][0]);
  gotoxy(40,11);cprintf("%c",mat[2][1]);
  gotoxy(46,11);cprintf("%c",mat[2][2]);


  for(Rows=5;Rows<=13;Rows++)
    {
     textcolor(GREEN);
     gotoxy(37,Rows);cprintf("||");
     gotoxy(43,Rows);cprintf("||");
    }

  for(Cols=30;Cols<=50;Cols++)
    {
     gotoxy(Cols,8); cprintf("=");
     gotoxy(Cols,10);cprintf("=");
    }

  // Player One - Probability of player 1 Beating Player 2
  if( mat[0][0]==mat[0][1] && mat[0][1]==mat[0][2] && mat[0][2]==88)
    {
     clrscr();
     gotoxy(32,12);printf("%s Won the Match ! ",name1);
     getch();
     status=1;
    }
  if( mat[1][0]==mat[1][1] && mat[1][1]==mat[1][2] && mat[1][2]==88)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name1);
         getch();
         status=1;
       }
  if( mat[2][0]==mat[2][1] && mat[2][1]==mat[2][2] && mat[2][2]==88)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name1);
         getch();
         status=1;
       }

  if( mat[0][0]==mat[1][0] && mat[1][0]==mat[2][0] && mat[2][0]==88)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name1);
         getch();
         status=1;
       }

  if( mat[0][1]==mat[1][1] && mat[1][1]==mat[2][1] && mat[2][1]==88)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name1);
         getch();
         status=1;
       }
  if( mat[0][2]==mat[1][2] && mat[1][2]==mat[2][2] && mat[2][2]==88)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name1);
         getch();
         status=1;
       }
  if( mat[0][0]==mat[1][1] && mat[1][1]==mat[2][2] && mat[2][2]==88)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name1);
         getch();
         status=1;
       }

  if( mat[0][2]==mat[1][1] && mat[1][1]==mat[2][0] && mat[2][0]==88)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name1);
         getch();
         status=1;
       }

  // Player Two - Probability of player 2 Beating Player 1
  if( mat[0][0]==mat[0][1] && mat[0][1]==mat[0][2] && mat[0][2]==48)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name2);
         getch();
         status=1;
       }
  if( mat[1][0]==mat[1][1] && mat[1][1]==mat[1][2] && mat[1][2]==48)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name2);
         getch();
         status=1;
       }
  if( mat[2][0]==mat[2][1] && mat[2][1]==mat[2][2] && mat[2][2]==48)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name2);
         getch();
         status=1;
     }

  if( mat[0][0]==mat[1][0] && mat[1][0]==mat[2][0] && mat[2][0]==48)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name2);
         getch();
         status=1;
       }

  if( mat[0][1]==mat[1][1] && mat[1][1]==mat[2][1] && mat[2][1]==48)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name2);
         getch();
         status=1;
       }
  if( mat[0][2]==mat[1][2] && mat[1][2]==mat[2][2] && mat[2][2]==48)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name2);
         getch();
         status=1;
       }
  if( mat[0][0]==mat[1][1] && mat[1][1]==mat[2][2] && mat[2][2]==48)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name2);
         getch();
         status=1;
       }

  if( mat[0][2]==mat[1][1] && mat[1][1]==mat[2][0] && mat[2][0]==48)
       {
         clrscr();
         gotoxy(32,12);printf("%s Won the Match ! ",name2);
         getch();
         status=1;
       }
     return status;
 } // End of "printPats" Function
  void main()
    {
      char name1[25]="Aditi",name2[25]="Madhusudan";
      int mat[3][3],Rows,Cols;
      int val=49;
      int times ;
      int choice1 , choice2;
      int status;
      // Accept The Name of Player 1 and Player 2
      textcolor(YELLOW);
      gotoxy(20,5);cprintf("Enter The Name of ");
      textcolor(GREEN);
      gotoxy(20,6);cprintf("---------------------------------------");
      textcolor(WHITE);
      gotoxy(24,7);printf("Player 1 : ");
      gotoxy(35,7);gets(name1);
      gotoxy(24,9);printf("Player 2 : ");
      gotoxy(35,9);gets(name2);
      textcolor(GREEN);
      gotoxy(20,10);cprintf("---------------------------------------");
      textcolor(YELLOW);
      gotoxy(20,11);cprintf("Press Any Key To Continue ...");
      getch();

      // Initialization of Matrices (Values Between 1 to 9 ) - ASCII CODE
      for(Rows=0;Rows<3;Rows++)
         {
           for(Cols=0;Cols<3;Cols++)
              {
                mat[Rows][Cols]=val;
                val++;
              }
         }
      for(times=1;times<=5;times++)
         {
            // Player One
            PlayerOne:
            clrscr();
            status=printPats(mat,name1,name2);
            if(status!=0)
              return;
            gotoxy(25,17);
            //printf("Player One Enter Your Choice : ");
            printf("%s Enter Your Choice : ",name1);
            scanf("%d",&choice1);

            if(choice1>=1 && choice1<=9)
               {
                 int count=0;

                 // Check The value of Choice1 with Matrices Value
                  for(Rows=0;Rows<3;Rows++)
                     {
                       for(Cols=0;Cols<3;Cols++)
                          {
                             if( (choice1+48)==mat[Rows][Cols])
                               {
                                  mat[Rows][Cols]=88;
                                  break;
                               }
                             else
                               count++;
                          }
                     }

                if(count==9)
                  {
                     clrscr();
                     gotoxy(32,12);printf("Already Taken ! ");
                     getch();
                     goto PlayerOne;
                  }
               }
            else
               {
                 clrscr();
                 gotoxy(32,12); printf("Invalid Choice ");
                 getch();
                 goto PlayerOne;
               }
            // End of Player One

            // Player Two
            PlayerTwo:
            clrscr();
            status=printPats(mat,name1,name2);
            if(status!=0)
              return;
            if(status==0 && times==5)
              {
                clrscr();
                gotoxy(34,12);printf("Match Tie ! ");
                getch();
                return;
              }
            gotoxy(25,17);
            //printf("Player Two Enter Your Choice : ");
            printf("%s Enter Your Choice : ",name2);
            scanf("%d",&choice2);

            if(choice2>=1 && choice2<=9)
               {
                 int count=0;

                 // Check The value of Choice2 with Matrices Value
                  for(Rows=0;Rows<3;Rows++)
                     {
                       for(Cols=0;Cols<3;Cols++)
                          {
                             if( (choice2+48)==mat[Rows][Cols])
                               {
                                  mat[Rows][Cols]=48;
                                  break;
                               }
                             else
                               count++;
                          }
                     }

                if(count==9)
                  {
                     clrscr();
                     gotoxy(32,12);printf("Already Taken ! ");
                     getch();
                     goto PlayerTwo;
                  }
               }
            else
               {
                 clrscr();
                 gotoxy(32,12); printf("Invalid Choice ");
                 getch();
                 goto PlayerTwo;
               }
             // End of Player Two
         } // End of For Loop (Times)
}// End of Main Function


// Compiler : Borland C++ Version 5.02