Question

Using C Programming: (use printf, scanf)

Image for 18. Tic-Tac-Toc Game Write a program that allows two players to play a game of tic-tac-toc. Use a two- dimensi

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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("\n\t\t\t\tTIC TAC TOE");
   printf("\n\t\t\t\t");
   for(i=1;i<=11;i++)
   {
       delay(10000);
       printf("*");
   }*/
   do
   {
       clrscr();
       printf("\n\t\t\t\tTIC TAC TOE");
       printf("\n\t\t\t\t");
       for(i=1;i<=11;i++)
       {
           delay(10000);
           printf("*");
       }
       printf("\n1.Start The Game");
       printf("\n2.Quit The Game");
       printf("\nEnter 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("\n\n");
               clrscr();
               printf("\nEnter the name of the player playing for \'X\': ");
               fflush(stdin);
               gets(name_X);
               printf("\nEnter the name of the player playing for \'O\': ");
               fflush(stdin);
               gets(name_O);
               Board();
               for(;;)
               {
                   if(win==1)
                       break;
                   check();
                   if(chk==9)
                   {
                       printf("\n\t\t\tMATCH DRAWS!!");
                       printf("\nPress any key....");
                       break;
                   }
                   else
                       chk=0;
                   printf("\nTURN FOR %s:",name_X);
                   PlayerX();
                   do
                   {
                       if(wrong_X!=1)
                           break;
                       wrong_X=0;
                       printf("\nTURN FOR %s:",name_X);
                       PlayerX();
                   }while(wrong_X==1);
                   check();
                   if(chk==9)
                   {
                       printf("\n\t\t\tMATCH DRAWS");
                       printf("\nPress any key....");
                       break;
                   }
                   else
                       chk=0;
                   printf("\nTURN FOR %s:",name_O);
                   PlayerO();
                   do
                   {
                       if(wrong_O!=1)
                           break;
                       wrong_O=0;
                       printf("\nTURN FOR %s:",name_O);
                       PlayerO();
                   }while(wrong_O==1);

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


void Board()
{
   int i,j;
   clrscr();
   printf("\n\t\t\t\tTIC TAC TOE BOARD");
   printf("\n\t\t\t\t*****************");
   printf("\n\n\n");
   printf("\n\t\t\t    1\t      2\t        3");
   for(i=1;i<=3;i++)
   {
       printf("\n \t\t\t _____________________________");
       printf("\n \t\t\t¦\t ¦\t   ¦\t     ¦");
       printf("\n\t\t%d\t",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("\n\t\t\t¦\t ¦\t   ¦\t     ¦");
   }
   printf("\n\t\t\t------------------------------");
   Player_win();
}


void PlayerX()
{
   int row,col;
   if(win==1)
       return;
   printf("\nEnter 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("\nWRONG 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("\nEnter 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("\nWRONG 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("\n\nRESULT: %s wins!!",name_X);
           printf("\nPress 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("\n\nRESULT: %s wins!!",name_X);
           printf("\nPress 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("\n\nRESULTL: %s wins!!",name_X);
       printf("\nPress 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("\n\nRESULT: %s wins!!",name_X);
                printf("\nPress 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("\n\nRESULT: %s wins!!",name_O);
                        printf("\nPress 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("\n\nRESULT: %s wins!!",name_O);
                        printf("\nPress 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("\n\nRESULT: %s wins!!",name_O);
       printf("\nPress 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("\n\nRESULT: %s wins!!",name_O);
                printf("\nPress 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;
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

    18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use 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 does the following: Write . 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...

  • I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program...

    I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional String 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 does the following: a. Displays the contents of the board array. b. Allows player 1 to select a location...

  • Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use...

    Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. 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 does the following: 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...

  • Use Java language to create this program Write a program that allows two players to play...

    Use Java language to create this program Write a program that allows two players to play a game of tic-tac-toe. Using a two-dimensional array with three rows and three columns as the game board. Each element of the array should be initialized with a number from 1 - 9 (like below): 1 2 3 4 5 6 7 8 9 The program should run a loop that Displays the contents of the board array allows player 1 to select the...

  • Please help me write a Pseudocode (please do NOT answer in JAVA or Python - I...

    Please help me write a Pseudocode (please do NOT answer in JAVA or Python - I will not be able to use those). Please ALSO create a flowchart using Flowgarithm program. Thank you so much, if answer is provided in Pseudocode and Flowchart, I will provide good feedback and thumbs up to the resonder. Thank you! Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional char array with three rows...

  • Write a program to Simulate a game of tic tac toe in c#

    Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1)    ...

  • (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an...

    (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...

  • Java project In a game of tic-tac-toe, two players take turns marking an available cell in...

    Java project In a game of tic-tac-toe, two players take turns marking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Create...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT