0

I am making a tic tac toe game in C language but it won't run

I don't know where is the error at. The compiler just freeze without any error messages this is my code below if anyone could point out the errors and the way of writing clean code i am a new programmer who is still learning and this is my first project.

#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>

void BuildGround;
void first_player_choice;
void second_player_choice;
void ai_choice;
char result;
void AiVsP;
void PVP;
int count_ground;

int main()
{
    char ground[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
    int mode;
    printf("choose game mode (1 or 2): ");
    printf("1. PVP ");
    printf("2. AI vs P: ");
    scanf("%d",&mode);
    switch(mode){
case 1 :
    PVP();
    break;
case 2:
    AiVsP();
    break;

default:
    printf("Invalid input: ");
    break;

    }
}

int count_ground(char symbole){
    int total=0;
    for(int i =0 ; i<9 ; i++){
        if(ground[i]== symbole)
            total=+ 1;
    }
}

void AiVsP(void){
char name[100];
printf("Enter your name : ");
gets(name);
while(1==1){
    system("cls");
    BuildGround();
    if (count_ground('X') == count_ground('O')){
        puts("Turn: ");
        puts(name);
        fisrt_player_choice();
    }
    else{
        ai_choice;
    }
    char winner == result();
    if (winner == 'X'){
        system("cls");
        BuildGround();
        puts("The winner is: ");
        puts(name);
        break;
    }
    else if
        (winner == 'O'){
        system("cls");
        BuildGround();
        printf("AI wins ")
        break;
    }
    else winner == 'D'
    printf("The Game is Draw ")
    break;

}

}

void PVP(void){
char name1[100];
char name2[100];
printf("Enter the name of the first player : ");
gets(name1);
printf("Enter the name of the second player : ");
gets(name2);
while(1==1){
    system("cls");
    BuildGround;
        if (count_ground('X') == count_ground('O')){
        puts("Turn: ");
        puts(name1);
        first_player_choice();
}
    else{

        puts("Turn: ");
        puts(name2);
        second_player_choice();
    }
     char winner == result();
    if (winner == 'X'){
        system("cls");
        BuildGround();
        puts("The winner is: ");
        puts(name1);
        break;
    }
    else if
        (winner == 'O'){
        system("cls");
        BuildGround();
        puts("The winner is: ");
        puts(name2);
        break;
    }
    else winner == 'D'
    printf("The Game is Draw ")
    break;

}
}
char result(char ground[9]){
    int i;
    for(i = 0;i<=6; i+=3)//it's for row
        if(ground[i] == ground[i+1]== && ground[i+1]== ground[i+2]&& ground[i] != ' ' ){
            return ground[0];
        }
    for(i = 0;i<3; i++)//it's for column
        if(ground[i]== ground[i+3]== && ground[i+3]== ground[i+6] && ground[i]!=' ' ){
           return ground[1];
        }
    if(ground[0] == && ground[4] == && ground[4]== ground[8] && ground[0]!=' '){
            return  ground[4];
        }
    else if(ground[2] == && ground[4] == && ground[4]== ground[6] && ground[2]!=' '){
            return ground[6];
        }
    else if(count_ground('X')+ count_ground('O') < 9){
        return 'C';
    }else return 'D';
}

void ai_choice(void){
    srand(time(0));
    do{
        int c;
        c = rand()%10;
        }
        while(ground[c]!= ' ');
        ground(c)='O';
}

void first_player_choice(char ground[9]){
    while(1==1){
         int position;
        printf("Choose your position from 0-8 (column wise): ");
        scanf("%d", &position);
        if(position < 0 || position > 8 ){
            printf("Invalid number please try again: ");
        }
        else if(ground[position]!=' '){
             printf("Position is already taken please try again: ");
        }
        else
            ground[c]='X';
        break;

    }
}

void second_player_choice(char ground[9]){
    while(1==1){
         int position;
        printf("Choose your position from 0-8 (column wise): ");
        scanf("%d", &position);
        if(position < 0 || position > 8 ){
            printf("Invalid number please try again: ");
        }
        else if(ground[position]!=' '){
             printf("Position is already taken please try again: ");
        }
        else
            ground[c]='O';
        break;

    }
}

void BuildGround(char ground[9]){

    printf("\t\t\t\tT i c   t a c   t o e");
    printf("\nPlayers 1 Symbol: X");
    printf("\nPlayers 2 Symbol: O");
    printf("\n\t\t\t       |       |       ");
    printf("\n\t\t\t   %c   |   %c   |   %c   ",ground[0],ground[1],ground[2]);
    printf("\n\t\t\t_______|_______|_______");
    printf("\n\t\t\t   %c   |   %c   |   %c   ",ground[3],ground[4],ground[5]);
    printf("\n\t\t\t_______|_______|_______");
    printf("\n\t\t\t   %c   |   %c   |   %c   ",ground[6],ground[7],ground[8]);
    printf("\n\t\t\t       |       |       ");
}
Andy
  • 49,085
  • 60
  • 166
  • 233

1 Answers1

2

You have multiple issues, like

case 1 :
    PVP;  //this is not a function call.
    break;

should use the call like

 PVP();

or rather, pvp(); , as uppercase letters usually carry special meaning, not a good idea to use them for user-defined code.

That said, function definitions like

void ai_choice{
    srand(time(0));

and

void PVP{
char name1[100];
char name2[100];

are wrong syntax, you have to use the parenthesis, like

void PVP (void){
    char name1[100];
    char name2[100];

That said, scanf("%d",& mode); is usually written as scanf("%d",&mode); (no space between operand and operator, but that's just a choice).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261