Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program th

ID: 3760135 • Letter: U

Question

Using C Programming: (use printf, scanf)

18. Tic-Tac-Toc Game Write a program that allows two players to play a game of tic-tac-toc. Use a two- dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that Displays the contents of the board array Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and column number. Arrays Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row and column number. Determines whether a player has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should say so and end. Player 1 wins when there are three Xs in a row on the game board. The Xs can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner.

Explanation / Answer

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

void Board();
void PlayerX();
void PlayerO();
void Player_win();
void check();
int win=0,wrong_X=0,wrong_O=0,chk=0;

char name_X[30];
char name_O[30];
int pos_for_X[3][3];
int pos_for_O[3][3];
int pos_marked[3][3];

void main()
{
   int i,ch,j;
   char ans;
/*   clrscr();
   printf(" TIC TAC TOE");
   printf(" ");
   for(i=1;i<=11;i++)
   {
       delay(10000);
       printf("*");
   }*/
   do
   {
       clrscr();
       printf(" TIC TAC TOE");
       printf(" ");
       for(i=1;i<=11;i++)
       {
           delay(10000);
           printf("*");
       }
       printf(" 1.Start The Game");
       printf(" 2.Quit The Game");
       printf(" Enter your choice(1-2) : ");
       scanf("%d",&ch);
       switch(ch)
       {
           case 1:
               chk=0;
               win=0;
               for(i=1;i<=3;i++)
               {
                   for(j=1;j<=3;j++)
                   {
                       pos_for_X[i][j]=0;
                       pos_for_O[i][j]=0;
                       pos_marked[i][j]=0;
                   }
               }
               printf(" ");
               clrscr();
               printf(" Enter the name of the player playing for 'X': ");
               fflush(stdin);
               gets(name_X);
               printf(" Enter the name of the player playing for 'O': ");
               fflush(stdin);
               gets(name_O);
               Board();
               for(;;)
               {
                   if(win==1)
                       break;
                   check();
                   if(chk==9)
                   {
                       printf(" MATCH DRAWS!!");
                       printf(" Press any key....");
                       break;
                   }
                   else
                       chk=0;
                   printf(" TURN FOR %s:",name_X);
                   PlayerX();
                   do
                   {
                       if(wrong_X!=1)
                           break;
                       wrong_X=0;
                       printf(" TURN FOR %s:",name_X);
                       PlayerX();
                   }while(wrong_X==1);
                   check();
                   if(chk==9)
                   {
                       printf(" MATCH DRAWS");
                       printf(" Press any key....");
                       break;
                   }
                   else
                       chk=0;
                   printf(" TURN FOR %s:",name_O);
                   PlayerO();
                   do
                   {
                       if(wrong_O!=1)
                           break;
                       wrong_O=0;
                       printf(" TURN FOR %s:",name_O);
                       PlayerO();
                   }while(wrong_O==1);

                   }
               Board();
               if(win!=1)
               {
                   printf(" MATCH DRAWS!!");
                   printf(" Press any key.......");
               }
               getch();
               break;
           case 2:
               printf(" Thank You For Playing The Game.");
               printf(" ###############################");
               getch();
               exit(1);
               break;
       }
       printf(" Want To Play(Y/N) ? ");
       fflush(stdin);
       scanf("%c",&ans);
   }while(ans=='y' || ans=='Y');
}


void Board()
{
   int i,j;
   clrscr();
   printf(" TIC TAC TOE BOARD");
   printf(" *****************");
   printf(" ");
   printf("     1       2         3");
   for(i=1;i<=3;i++)
   {
       printf(" _____________________________");
       printf(" ¦ ¦    ¦      ¦");
       printf(" %d ",i);
       for(j=1;j<=3;j++)
       {

           if(pos_for_X[i][j]==1)
           {
               printf("    X");
               printf("     ");
           }
           else if(pos_for_O[i][j]==1)
           {
               printf("    O");
               printf("     ");
           }
           else
           {
               printf("          ");
               continue;
           }
       }
       printf(" ¦ ¦    ¦      ¦");
   }
   printf(" ------------------------------");
   Player_win();
}


void PlayerX()
{
   int row,col;
   if(win==1)
       return;
   printf(" Enter the row no. : ");
   fflush(stdin);
   scanf("%d",&row);
   printf("Enter the column no. : ");
   fflush(stdin);
   scanf("%d",&col);
   if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
   {
       printf(" WRONG POSITION!! Press any key.....");
       wrong_X=1;
       getch();
       Board();
   }
   else
   {
       pos_for_X[row][col]=1;
       pos_marked[row][col]=1;
       Board();
   }
}
void PlayerO()
{
   int row,col;
   if(win==1)
       return;
   printf(" Enter the row no. : ");
   scanf("%d",&row);
   printf("Enter the column no. : ");
   scanf("%d",&col);
   if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
   {
       printf(" WRONG POSITION!! Press any key....");
       wrong_O=1;
       getch();
       Board();
   }
   else
   {
       pos_for_O[row][col]=1;
       pos_marked[row][col]=1;
       Board();
   }
}
void Player_win()
{
   int i;
   for(i=1;i<=3;i++)
   {
       if(pos_for_X[i][1]==1 && pos_for_X[i][2]==1 && pos_for_X[i][3]==1)
       {
           win=1;
           printf(" RESULT: %s wins!!",name_X);
           printf(" Press any key............");
           return;
       }
   }
   for(i=1;i<=3;i++)
   {
       if(pos_for_X[1][i]==1 && pos_for_X[2][i]==1 && pos_for_X[3][i]==1)
       {
           win=1;
           printf(" RESULT: %s wins!!",name_X);
           printf(" Press any key............");
           return;
       }
   }
   if(pos_for_X[1][1]==1 && pos_for_X[2][2]==1 && pos_for_X[3][3]==1)
   {
       win=1;
       printf(" RESULTL: %s wins!!",name_X);
       printf(" Press any key......");
       return;
   }
   else if(pos_for_X[1][3]==1 && pos_for_X[2][2]==1 &&
pos_for_X[3][1]==1)
   {
           win=1;
       printf(" RESULT: %s wins!!",name_X);
                printf(" Press any key.....");
       return;
   }

        for(i=1;i<=3;i++)
   {
       if(pos_for_O[i][1]==1 && pos_for_O[i][2]==1 && pos_for_O[i][3]==1)
       {
           win=1;
           printf(" RESULT: %s wins!!",name_O);
                        printf(" Press any key.....");
           return;
       }
   }
   for(i=1;i<=3;i++)
   {
       if(pos_for_O[1][i]==1 && pos_for_O[2][i]==1 && pos_for_O[3][i]==1)
       {
           win=1;
           printf(" RESULT: %s wins!!",name_O);
                        printf(" Press any key.....");
           return;
       }
   }
   if(pos_for_O[1][1]==1 && pos_for_O[2][2]==1 && pos_for_O[3][3]==1)
   {
       win=1;
       printf(" RESULT: %s wins!!",name_O);
       printf(" Press any key.....");
       return;
   }
   else if(pos_for_O[1][3]==1 && pos_for_O[2][2]==1 &&
pos_for_O[3][1]==1)
   {
           win=1;
       printf(" RESULT: %s wins!!",name_O);
                printf(" Press any key.....");
       return;
   }
}
void check()
{
   int i,j;
   for(i=1;i<=3;i++)
   {
       for(j=1;j<=3;j++)
       {
           if(pos_marked[i][j]==1)
               chk++;
           else
               continue;
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote