# Tic tac toe game

Today I will show you how we can make our childhood game, Tic tac toe using c programming. The tic-tac-toe game is played on a 3×3 grid the game is played by two players, who take turns. The first player marks move with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark, change the players after every successful move, and pronounce the winner. every successful move, and pronounce the winner.

### Below is the source code of tic tac toe

``````#include <stdio.h>
#include <conio.h>
char square = { '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')
square = mark;

else if (choice == 2 && square == '2')
square = mark;

else if (choice == 3 && square == '3')
square = mark;

else if (choice == 4 && square == '4')
square = mark;

else if (choice == 5 && square == '5')
square = mark;

else if (choice == 6 && square == '6')
square = mark;

else if (choice == 7 && square == '7')
square = mark;

else if (choice == 8 && square == '8')
square = mark;

else if (choice == 9 && square == '9')
square = 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;
}
/*********************************************
FUNCTION TO RETURN GAME STATUS
1 FOR GAME IS OVER WITH RESULT
-1 FOR GAME IS IN PROGRESS
O GAME IS OVER AND NO RESULT
**********************************************/
int checkwin()
{
if (square == square && square == square)
return 1;

else if (square == square && square == square)
return 1;

else if (square == square && square == square)
return 1;

else if (square == square && square == square)
return 1;

else if (square == square && square == square)
return 1;

else if (square == square && square == square)
return 1;

else if (square == square && square == square)
return 1;

else if (square == square && square == square)
return 1;

else if (square != '1' && square != '2' && square != '3' &&
square != '4' && square != '5' && square != '6' && square
!= '7' && square != '8' && square != '9')
return 0;
else
return  - 1;
}
/*******************************************************************
FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
********************************************************************/
void board()
{
system("cls");
printf("\n\n\tTic Tac Toe\n\n");
printf("Player 1 (X)  -  Player 2 (O)\n\n\n");
printf("     |     |     \n");
printf("  %c  |  %c  |  %c \n", square, square, square);
printf("_____|_____|_____\n");
printf("     |     |     \n");
printf("  %c  |  %c  |  %c \n", square, square, square);
printf("_____|_____|_____\n");
printf("     |     |     \n");
printf("  %c  |  %c  |  %c \n", square, square, square);
printf("     |     |     \n\n");
}
/*******************************************************************
END OF PROJECT
********************************************************************/``````
0 0 vote
Article Rating 