C Program to Implement a TIC TAC TOE game

Tic Tac Toe Program in C

In this tutorial, we are going to learn the famous game which everyone usually enjoys to play Tic Tac Toe Program in C.

This program generally not asked in an interview or exam it is only for entertainment purpose.

What is tic tac toe game?

Tic tac toe is simple technical game for two players. The game consists of 3 × 3 matrix in which each player marks one space at a time in a grid and the player wins who makes the marking in 3 places horizontal,  vertical or diagonally.

how to play tic tac toe game?

As we give 3×3 matrix and 2 people can only this.

As the game starts first player marks with any symbol in grid anywhere.and after that second player marks in a grid with another symbol.this continue till any player makes marks in 3 successive grid horizontal,  vertically or diagonally. The player which makes such 3 marks wins the game.

TIC TAC TOE game

 Strategy to win tic tac toe game?

If you are the first player then make your first mark on the corners. Marking corner makes more possibility to win.

Now we know the what’s is game and how to play games let’s see an implementation of this program in c.

C Program to Implement a TIC TAC TOE game

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343#include<stdio.h>#include<string.h>void main(){char choice1,choice2;int player1,player2,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(“game format\n”);printf(” ______________\n”);printf(“| 0 | 1 | 2 |\n”);printf(“|__|__|__|\n”);printf(“| 3| 4 | 5 |\n”);printf(“|__|__|__|\n”);printf(“| 6 | 7 | 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’){player1=1;player2=0;}else{player1=0;player2=1;}while(move<3){ count=0; if(move==1) { printf(“Player1 Enter the position\n”); scanf(“%d”,&position); if(a[position]==3) { a[position]=player1; 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]==player1&&a[1]==player1&&a[2]==player1)||(a[3]==player1&&a[4]==player1&&a[5]==player1)||(a[6]==player1&&a[7]==player1&&a[8]==player1)||(a[0]==player1&&a[3]==player1&&a[6]==player1)||(a[1]==player1&&a[4]==player1&&a[7]==player1)||(a[2]==player1&&a[5]==player1&&a[8]==player1)||(a[0]==player1&&a[4]==player1&&a[8]==player1)||(a[2]==player1&&a[4]==player1&&a[6]==player1)) { 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]=player2; 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]==player2&&a[1]==player2&&a[2]==player2)||(a[3]==player2&&a[4]==player2&&a[5]==player2)||(a[6]==player2&&a[7]==player2&&a[8]==player2)||(a[0]==player2&&a[3]==player2&&a[6]==player2)||(a[1]==player2&&a[4]==player2&&a[7]==player2)||(a[2]==player2&&a[5]==player2&&a[8]==player2)||(a[0]==player2&&a[4]==player2&&a[8]==player2)||(a[2]==player2&&a[4]==player2&&a[6]==player2)) { 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.Pleasegive another position\n”); move=2; } }}if(x!=1&&y!=1){printf(“Draw Match\n”);printf(“Thank you for playing this game\n”);}}

Output

 Welcome to play TIC-TAC-TOE world

Game format

 ______________

| 0 | 1 | 2 |

|__|__|__|

| 3 | 4 | 5 |

|__|__|__|

| 6 | 7 | 8 |

|__|__|__|

Enter X or O for Player1

O

Player2 choice is X

Player1 Enter the position

4

        –       –       –

        –       O       –

        –       –       –

Player2 Enter position

2

        –       –       X

        –       O       –

        –       –       –

Player1 Enter the position

0

        O       –       X

        –       O       –

        –       –       –

Player2 Enter position

5

        O       –       X

        –       O       X

        –       –       –

Player1 Enter the position

8

        O       –       X

        –       O       X

        –       –       O

Player 1 wins

Thank you for playing this game