TIC TAC TOE Game in C Language


Project Name
TIC TAC TOE Game

Project Description
In this project you can register name of Player 1 and Player 2 and can play ,the sign of player 1 is [X] and player2 is [0]. After completing the game you will get winner player name.

Project Functionality
1.ADD PLAYER DETAILS
2.PLAY

Here is the complete code of this project just copy and paste it to any editor of C language
 
#include <stdio.h>
#include<stdio.h>
#include<stdlib.h>
char square[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int checkWinner();
void gameBoard(char*,char*);
int main()
{
    int player = 1, i, choice;
    char mark;
    char p1[200],p2[200];
    printf("---------------Player Registration---------------\n");
    printf("Enter Player 1 Name:");
    scanf("%s",&p1);
    printf("Enter Player 2 Name:");
    scanf("%s",&p2);
    do
    {
        gameBoard(p1,p2);
        player = (player % 2) ? 1 : 2;
        
        if(player==1)
        printf("%s, enter a number:  ", p1);
        else
        printf("%s, enter a number:  ", p2);
        
        scanf("%d", &choice);
        mark = (player == 1) ? 'X' : 'O';
        if (choice == 1 && square[1] == '1')
            square[1] = mark;            
        else if (choice == 2 && square[2] == '2')
            square[2] = mark;            
        else if (choice == 3 && square[3] == '3')
            square[3] = mark;            
        else if (choice == 4 && square[4] == '4')
            square[4] = mark;            
        else if (choice == 5 && square[5] == '5')
            square[5] = mark;            
        else if (choice == 6 && square[6] == '6')
            square[6] = mark;            
        else if (choice == 7 && square[7] == '7')
            square[7] = mark;            
        else if (choice == 8 && square[8] == '8')
            square[8] = mark;            
        else if (choice == 9 && square[9] == '9')
            square[9] = mark;            
        else
        {
            printf("Invalid move ");
            player--;
        }
        i = checkWinner();
        player++;
    }while (i ==  - 1); 
	//Calling gameBoard function   
     gameBoard(p1,p2);    
    if (i == 1)
    {
      --player;
	  if(player==1)
	  printf("Winner is %s",p1);
	  else
	  printf("Winner is %s",p2);	
    }        
    else
        printf("==>Game draw");
    return 0;
}

int checkWinner()
{
    if (square[1] == square[2] && square[2] == square[3])
        return 1;   //for game is over with result     
    else if (square[4] == square[5] && square[5] == square[6])
        return 1;        
    else if (square[7] == square[8] && square[8] == square[9])
        return 1;        
    else if (square[1] == square[4] && square[4] == square[7])
        return 1;        
    else if (square[2] == square[5] && square[5] == square[8])
        return 1;        
    else if (square[3] == square[6] && square[6] == square[9])
        return 1;        
    else if (square[1] == square[5] && square[5] == square[9])
        return 1;        
    else if (square[3] == square[5] && square[5] == square[7])
        return 1;        
    else if (square[1] != '1' && square[2] != '2' && square[3] != '3' &&
        square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7] 
        != '7' && square[8] != '8' && square[9] != '9')
        return 0;//for game is over with no result
    else
        return  - 1;//for game is in progress
}
/*Function to generate Game Board*/
void gameBoard(char p1[200],char p2[200])
{
    system("cls");
    printf("\n\n\tTic Tac Toe\n\n");
    printf("%s's sign is [X]  and   %s's sign is  [0]\n\n\n",p1,p2);
    printf("     |     |     \n");
    printf("  %c  |  %c  |  %c \n", square[1], square[2], square[3]);
    printf("_____|_____|_____\n");
    printf("     |     |     \n");
    printf("  %c  |  %c  |  %c \n", square[4], square[5], square[6]);
    printf("_____|_____|_____\n");
    printf("     |     |     \n");
    printf("  %c  |  %c  |  %c \n", square[7], square[8], square[9]);
    printf("     |     |     \n\n");
}

Screenshot of project
Registration




Game Board
Winner



Post a Comment

0 Comments