0% found this document useful (0 votes)
70 views3 pages

Tic Tac Toe

This C program implements a tic-tac-toe game with two players. It uses functions to read player input, display the game board, check for a winner, and allow the game to be restarted. The main function calls the run function in a loop, allowing players to alternate turns until there is a winner or a draw. The run function handles one round of player turns by getting input, updating the board, and checking for a winner.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views3 pages

Tic Tac Toe

This C program implements a tic-tac-toe game with two players. It uses functions to read player input, display the game board, check for a winner, and allow the game to be restarted. The main function calls the run function in a loop, allowing players to alternate turns until there is a winner or a draw. The run function handles one round of player turns by getting input, updating the board, and checking for a winner.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
struct game
{
int i;
char ch;
}read();

void run();
int check(char sym[9],char ch,int count);
struct game read(char sym[9],int count);
void Display(char sym[9]);

void main()
{
char reStart;
again:
run();
printf("\nIf You Want To Play Again Press 1: \nElse Any:");
scanf("%s",&reStart);
if(reStart == '1')
{
system("cls");
goto again;
}
else
exit(0);
}

void run()
{
int count = 0;
struct game info;
char symbol[9] = {'1','2','3','4','5','6','7','8','9'};
Display(symbol);
again:
info = read(symbol,count);
symbol[info.i] = info.ch;
system("cls");
Display(symbol);
if(check(symbol,info.ch,count)==1);
else{
count++;
goto again;
}
}
int check(char sym[9],char ch,int count)
{
int i;
for(i = 0;i<=6; i+=3)//it's for row
if(sym[i] == ch && sym[i+1]==ch&&sym[i+2]==ch){
printf("\n\n\t\t\tThe Winner is : %c\n",ch);return 1;
}
for(i = 0;i<3; i++)//it's for column
if(sym[i]==ch && sym[i+3]==ch&&sym[i+6]==ch){
printf("\n\n\t\t\tThe Winner is : %c\n",ch);return 1;
}
if(sym[0]==ch && sym[4]==ch&&sym[8]==ch){
printf("\n\n\t\t\tThe Winner is : %c\n",ch);return 1;
}
else if(sym[2]==ch && sym[4]==ch && sym[6]==ch){
printf("\n\n\t\t\tThe Winner is : %c\n",ch);return 1;
}
else if(count==8){
printf("\n\n\t\t\tThe Game is DRAW\n");
return 1;
}else return 0;
}
struct game read(char sym[9],int count)
{
char value;
int i;
struct game info;
inputAgain:
if(count%2 == 0){
printf("\nEnter Your Choice X:");
}else{
printf("\nEnter Your Choice O:");
}
scanf("%s",&value);
for(i=0;i<9;i++){

if(value == sym[i]){
info.i = i;
if(count%2 == 0)
info.ch = 'X';
else
info.ch = 'O';
break;
}else{
info.i = -1;
info.ch = ' ';
}
}
if(info.i == -1){
printf("\nInput is not Valid");
goto inputAgain;
}
return info;
}

void Display(char sym[9])


{
printf("\t\t\t\tT i c t a c t o e");
printf("\n\nInstructions:\n1.This game involves two player mode\n2.To enter
your choice/element i.e,X or O enter the respective location number\n3.To win the
game you have to make a group of 3 i.e, X or O either horizontally or vertically or
diagonally\n\n");
printf("\nPlayers 1 Symbol: X");
printf("\nPlayers 2 Symbol: O");
printf("\n\t\t\t | | ");
printf("\n\t\t\t %c | %c | %c ",sym[0],sym[1],sym[2]);
printf("\n\t\t\t-------|-------|-------");
printf("\n\t\t\t %c | %c | %c ",sym[3],sym[4],sym[5]);
printf("\n\t\t\t-------|-------|-------");
printf("\n\t\t\t %c | %c | %c ",sym[6],sym[7],sym[8]);
printf("\n\t\t\t | | ");
}

You might also like