Tictactoe (Main)
Tictactoe (Main)
Programming Language: C
Compiler: GCC or any C compiler
IDE: Code::Blocks, Dev-C++, or any text
editor
5. Testing
The game was tested for various scenarios,
including:
Valid moves.
Invalid moves (out of bounds and
occupied spaces).
Winning conditions for both players.
Draw conditions.
6. Implementation
Program Structure
The program is structured into several
functions to handle various tasks:
7. Code Implementation
Below is a simplified version of the Tic Tac Toe game code in C
Header Files
#include<stdio.h>
#include<conio.h>
* #include<stdio.h>:This includes the standard input-output library,which allows the program to use
function like printf and scanf.
* #include<conio.h>:This includes the console input-output library, which provides function like getch and
clrscr.
Function Declaration
void printBoard();
int checkWin();
void system();
* void printBoard();: Declares a function to print the Tic Tac Toe board.
* int checkWin();: Declares a function to check the game status(win, draw, or ongoing).
* void system();: Declares a function to excutes system commands (like clearing the screen).
Global Variables
char board[]={‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’};
* char board[]: An array representing the Tic Tac Toe board with initial from ‘0’ to ‘9’.
Main Function
void main(){
int player=1,input,status=-1;
printBoard();
* void main(): The main function where the program execution starts.
* int input,status=-1: Initializes input for player moves and status to -1(game ongoing).
Game Loop
while (status==-1)
player=(player%2==0) ? 2 : 1,
scanf(“%d”,&input);
* char mark=(player==1)? 'X': '0';: Sets the mark to 'X' for Player 1 and 'O' for Player 2.
* printf("Please Enter Number For Player %d\n",player);: Prompts the player to enter number.
if(input<1 || input>9){
printf("invalid input");
board[input]=mark;
printBoard();
int result-checkWin();
if(result==1){
return;
}else if(result==0){
printf("draw");
return;}
player++;
void printBoard()
{system("cls");
printf("\n\n");
printf(" | | \n”);
printf(“____|_____|_____\n");
printf(“____|_____|_____\n");
printf(“____|_____|_____\n");
printf(“\n\n”);
* The following printf statements print the Tic Tac Toe board with the current marks.
int checkwin(){
return 1;
}
return 1;
return 1;
return 1;
return 1;
return 1;
return 1;
return 1;
int count=0;
if(board[i]=='X' || board[1]-'0'){
count++;
}
if(count==9){
return 0;
return -1;
I hope this helps! Let me know if you have any questions or need further clarification.
8. Conclusion
FutureThe Tic Tac Toe game
implemented in C demonstrates
fundamental programming
concepts such as arrays, loops,
conditionals, and functions. It
serves as an excellent project for
beginners to understand game
development and basic C
programming.
9. Bibliography:
Google – For learning about the various functions used in the
project and researching about calculators
https://www.geeksforgeeks.org – For learning about the structure and
working of the code.