Please fix the code with explaining:        #include #include char box[10]={'0','1','2','3','4','5','6','7','8','9'}; void create_board(); void make_board(int, char); int check_win(); int main() {     int choice,player=1,i;     char mark;     do{         create_board();         player= (player % 2) ? 1: 2;         printf("Player %d, enter a number: ",player);         scanf("%d",&choice);         mark = (player==1) ? 'X' : 'O';         make_board(choice,mark);         i=check_for_win();         player++;     }while(i == -1);    create_board();     if(i==1)         printf("Player %d  you have won the game",--player);     else         printf("<               Draw             >");     return 0; } void create_board() {     clear_screen;     printf("\n\n\tTic Tac Toe\n\n");     printf("Player 1 (X) -- Player 2 (O)\n\n");     printf("     |     |     \n");     printf(" %c   | %c   | %c   \n",box[1],box[2],box[3]);     printf("__________|________|__\n");     printf("     |     |      \n");     printf(" %c   | %c   | %c   \n",box[4],box[5],box[6]);     printf("__________|__________|__\n");     printf("     |     |      \n");     printf(" %c   | %c   | %c   \n",box[7],box[8],box[9]);     printf("____________|___________|__\n");     printf("     |     |      \n"); } void make_board( int choice, char mark) {     if(choice==1 && box[1]=='1')         box[1]=mark;     else if(choice==2 && box[2]=='2')         box[2]=mark;     else if(choice==3 && box[3]=='3')         box[3]=mark;     else if(choice==4 && box[4]=='4')         box[4]=mark;     else if (choice==5 && box[5]=='5')         box[5]=mark;     else if (choice==6 && box[6]=='6')         box[6]=mark;     else if (choice==7 && box[7]=='7')         box[7]=mark;     else if (choice==8 && box[8]=='8')         box[8]=mark;     else if (choice==9 && box[9]=='9')         box[9]=mark;     else     {         printf("Invalid move");     } } int check_win() {     if(box[1]==box[2] && box[2]==box[3])         return 1;     else if(box[4]==box[5] && box[5]==box[6])         return 1;                                   // horizontal match     else if (box[7]==box[8] && box[8]==box[9])         return 1;     else if (box[1]==box[4] && box[4]==box[7])         return 1;     else if(box[2]==box[5] && box[5]==box[8])           // vertical match         return 1;     else if(box[3]==box[6] && box[6]==box[9])         return 1;     else if(box[1]==box[5] && box[5]==box[9])         return 1;     else if(box[3]==box[5] && box[5]==box[7])           //diagonal match         return 1;     else if(box[1]!= '1' && box[2]!= '2' && box[3]!= '3' && box[4]!= '4'&& box[5]!= '5' && box[6]!= '6'&& box[7]!= '7' && box[8]!= '8' && box[9]!='9')  //no match         return 0;     else         return -1; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Please fix the code with explaining: 

 

 

 

#include <stdio.h>
#include <term.h>
char box[10]={'0','1','2','3','4','5','6','7','8','9'};
void create_board();
void make_board(int, char);
int check_win();
int main()
{
    int choice,player=1,i;
    char mark;
    do{
        create_board();
        player= (player % 2) ? 1: 2;

        printf("Player %d, enter a number: ",player);
        scanf("%d",&choice);

        mark = (player==1) ? 'X' : 'O';
        make_board(choice,mark);

        i=check_for_win();
        player++;

    }while(i == -1);

   create_board();

    if(i==1)
        printf("Player %d  you have won the game",--player);
    else
        printf("<               Draw             >");


    return 0;
}
void create_board()
{
    clear_screen;
    printf("\n\n\tTic Tac Toe\n\n");
    printf("Player 1 (X) -- Player 2 (O)\n\n");
    printf("     |     |     \n");
    printf(" %c   | %c   | %c   \n",box[1],box[2],box[3]);
    printf("__________|________|__\n");
    printf("     |     |      \n");
    printf(" %c   | %c   | %c   \n",box[4],box[5],box[6]);
    printf("__________|__________|__\n");
    printf("     |     |      \n");
    printf(" %c   | %c   | %c   \n",box[7],box[8],box[9]);
    printf("____________|___________|__\n");
    printf("     |     |      \n");


}
void make_board( int choice, char mark)
{
    if(choice==1 && box[1]=='1')
        box[1]=mark;
    else if(choice==2 && box[2]=='2')
        box[2]=mark;
    else if(choice==3 && box[3]=='3')
        box[3]=mark;
    else if(choice==4 && box[4]=='4')
        box[4]=mark;
    else if (choice==5 && box[5]=='5')
        box[5]=mark;
    else if (choice==6 && box[6]=='6')
        box[6]=mark;
    else if (choice==7 && box[7]=='7')
        box[7]=mark;
    else if (choice==8 && box[8]=='8')
        box[8]=mark;
    else if (choice==9 && box[9]=='9')
        box[9]=mark;
    else
    {
        printf("Invalid move");
    }
}

int check_win()
{
    if(box[1]==box[2] && box[2]==box[3])
        return 1;
    else if(box[4]==box[5] && box[5]==box[6])
        return 1;                                   // horizontal match
    else if (box[7]==box[8] && box[8]==box[9])
        return 1;

    else if (box[1]==box[4] && box[4]==box[7])
        return 1;
    else if(box[2]==box[5] && box[5]==box[8])           // vertical match
        return 1;
    else if(box[3]==box[6] && box[6]==box[9])
        return 1;


    else if(box[1]==box[5] && box[5]==box[9])
        return 1;
    else if(box[3]==box[5] && box[5]==box[7])           //diagonal match
        return 1;

    else if(box[1]!= '1' && box[2]!= '2' && box[3]!= '3' && box[4]!= '4'&& box[5]!= '5' && box[6]!= '6'&& box[7]!= '7' && box[8]!= '8' && box[9]!='9')  //no match
        return 0;

    else
        return -1;


}

 
Expert Solution
steps

Step by step

Solved in 3 steps with 6 images

Blurred answer
Knowledge Booster
Program on Numbers
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education