Advertisement
Plabon_dutta

Tic Tac Toe Game (Updated)

Mar 29th, 2021 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.92 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. char position[11], p1[20], p2[20];
  6. int t=1, g=1, l=0, u=0;
  7.  
  8. void interface();
  9. void start_game();
  10. int Win_or_not();
  11. void red ();
  12. void green();
  13. void blue();
  14. void yellow();
  15. void purple();
  16. void reset ();
  17.  
  18. int main() {
  19.     int player = 1, i=-1, input;
  20.     char mark;
  21.     position[1] = '1';
  22.     position[2] = '2';
  23.     position[3] = '3';
  24.     position[4] = '4';
  25.     position[5] = '5';
  26.     position[6] = '6';
  27.     position[7] = '7';
  28.     position[8] = '8';
  29.     position[9] = '9';
  30.  
  31.     if(u==0) start_game();
  32.     u++;
  33.  
  34.     if(g==1) {
  35.         printf("\nEnter the name of Player 1: ");
  36.         scanf("%s", &p1);
  37.         printf("\n");
  38.         printf("Enter the name of Player 2: ");
  39.         scanf("%s", &p2);
  40.         printf("\n");
  41.         g++;
  42.     }
  43.  
  44.     while(i == -1) {
  45.         interface();
  46.         if(player%2!=0) player=1;
  47.         else player=2;
  48.         if(player==1) printf("\n\t%s, enter the choice : ",p1);
  49.         else printf("\n\t%s, enter the choice : ",p2);
  50.         scanf("%d",&input);
  51.         mark='O';
  52.         if(player==1) mark='X';
  53.         if(input == 1 && position[1] == '1')
  54.             position[1] = mark;
  55.         else if(input == 2 && position[2] == '2')
  56.             position[2] = mark;
  57.             else if(input == 3 && position[3] == '3')
  58.             position[3] = mark;
  59.             else if(input == 4 && position[4] == '4')
  60.             position[4] = mark;
  61.             else if(input == 5 && position[5] == '5')
  62.             position[5] = mark;
  63.             else if(input == 6 && position[6] == '6')
  64.             position[6] = mark;
  65.             else if(input == 7 && position[7] == '7')
  66.             position[7] = mark;
  67.             else if(input == 8 && position[8] == '8')
  68.             position[8] = mark;
  69.             else if(input == 9 && position[9] == '9')
  70.             position[9] = mark;
  71.  
  72.             else {
  73.                 red();
  74.                 printf("Invalid option!!!");
  75.                 reset();
  76.                 player--;
  77.                 getch();
  78.             }
  79.             i = Win_or_not();
  80.             player++;
  81.     }
  82.  
  83.     interface();
  84.     if(i==1){
  85.         player--;
  86.         ///Result printing
  87.         green();
  88.         if(player==1) printf("\n\n\t\tResult:- %s Won The Match\n\n",p1);
  89.         else printf("\n\n\t\tResult:- %s Won The Match\n\n",p2);
  90.         reset();
  91.         ///wining Line
  92.         yellow();
  93.         printf("\t\tWinning Line : ");
  94.         reset();
  95.         blue();
  96.         if(l>=1 && l<=3) printf("Row %d\n", l);
  97.         else if(l>=4 && l<=6) printf("Column %d\n", l-3);
  98.         else if(l==7) printf("Main Diagonal\n");
  99.         else if(l==8) printf("Minor Diagonal\n");
  100.         reset();
  101.         l=0;
  102.     }
  103.     else {
  104.         ///draw
  105.         printf("\n\n\t\tResult:- Game draw\n");
  106.     }
  107.     int st;
  108.     while(1) {
  109.         printf("\n\n\t\tWant To Save The Game Result? For Yes(1) - No(0): ");
  110.         scanf("%d", &st);
  111.         if(st==1) {
  112.             ///file write
  113.             FILE *result=fopen("Result.txt", "a");
  114.             if(i==1) {
  115.                 if(player==1)fprintf(result, "%s %s %s\n", p1, p2, p1);
  116.                 else fprintf(result, "%s %s %s\n", p1, p2, p2);
  117.             }
  118.             else fprintf(result, "%s %s Draw\n", p1, p2);
  119.  
  120.             fclose(result);
  121.             printf("\n\t\tPlease wait While Saving!");
  122.             printf("\n\t\t\tSaving");
  123.             for(i=0; i<=3; i++) {
  124.                 printf(". ");
  125.                 Sleep(500);
  126.             }
  127.             printf("\n");
  128.             green();
  129.             printf("\n\t\tThe Result Saved Successfully!!!");
  130.             reset();
  131.             break;
  132.         }
  133.         else if(st==0){
  134.             yellow();
  135.             printf("\n\t\tThe Result Not Saved!!!");
  136.             reset();
  137.             break;
  138.         }
  139.         else {
  140.             red();
  141.             printf("\n\t\tEnter '0' or '1'\n");
  142.             reset();
  143.         }
  144.     }
  145.  
  146.     int n;
  147.     while(1) {
  148.         printf("\n\n\t\tWant To Play Again? For Yes(1) - No(0): ");
  149.         scanf("%d", &n);
  150.         if(n==1) {
  151.             main();
  152.             break;
  153.         }
  154.         else if(n==0){
  155.             purple();
  156.             printf("\n\n\t\t*************************\n");
  157.             printf("\t\t***Thanks For Playing.***\n");
  158.             printf("\t\t*************************\n");
  159.             reset();
  160.             break;
  161.         }
  162.         else {
  163.             red();
  164.             printf("\n\t\tEnter '0' or '1'");
  165.             reset();
  166.         }
  167.     }
  168.     return 0;
  169. }
  170.  
  171. void start_game() {
  172.     int start, i;
  173.     while(1) {
  174.         printf("\t\tTic Tac Toe Game\n\n");
  175.         printf("\t\t   Start Game(1)\n\n\t\tSee Statistics(2)\n\n\t\t    Exit(0)\n");
  176.         printf("\nEnter Your Choice: ");
  177.         scanf("%d", &start);
  178.         if(start==0) return 0;
  179.         else if(start==1) {
  180.             printf("\n\t\tLoading");
  181.             for(i=0; i<=3; i++) {
  182.                 printf(". ");
  183.                 Sleep(500);
  184.             }
  185.             printf("\n");
  186.             return;
  187.         }
  188.         else if(start==2) {
  189.             ///file read
  190.             FILE *result=fopen("Result.txt", "r");
  191.         }
  192.         else {
  193.             red();
  194.             printf("\n\t\tEnter '1' or '2' or '0'\n");
  195.             reset();
  196.         }
  197.     }
  198. }
  199.  
  200. void interface(){
  201.     system("cls");
  202.     ///whole interface coloring (status- off)
  203.     //if(t%2!=0) red ();
  204.     //else yellow();
  205.     //t++;
  206.     printf("\n\n\t\t Tic Tac Toe \n\n");
  207.     printf("\t     %s (X) - %s (O) \n\n\n", p1, p2);
  208.     printf("\t\t     |     |     \n");
  209.     printf("\t\t  %c  |  %c  |  %c  \n", position[1], position[2], position[3]);
  210.     printf("\t\t_____|_____|_____\n");
  211.     printf("\t\t     |     |     \n");
  212.     printf("\t\t  %c  |  %c  |  %c  \n", position[4], position[5], position[6]);
  213.     printf("\t\t_____|_____|_____\n");
  214.     printf("\t\t     |     |     \n");
  215.     printf("\t\t  %c  |  %c  |  %c  \n", position[7], position[8], position[9]);
  216.     printf("\t\t     |     |     \n");
  217. }
  218.  
  219. int Win_or_not(){
  220.     if(position[1] == position[2] && position[2] == position[3]) {
  221.         l=1;
  222.         return 1;
  223.     }
  224.     else if (position[4] == position[5] && position[5] == position[6]) {
  225.         l=2;
  226.         return 1;
  227.     }
  228.     else if(position[7] == position[8] && position[8] == position[9]) {
  229.         l=3;
  230.         return 1;
  231.     }
  232.     else if(position[1] == position[4] && position[4] == position[7]) {
  233.         l=4;
  234.         return 1;
  235.     }
  236.     else if(position[2] == position[5] && position[5] == position[8]) {
  237.         l=5;
  238.         return 1;
  239.     }
  240.     else if(position[3] == position[6] && position[6] == position[9]) {
  241.         l=6;
  242.         return 1;
  243.     }
  244.     else if(position[1] == position[5] && position[5] == position[9]) {
  245.         l=7;
  246.         return 1;
  247.     }
  248.     else if(position[3] == position[5] && position[5] == position[7]) {
  249.         l=8;
  250.         return 1;
  251.     }
  252.     else if(position[1] != '1' && position[2] != '2' && position[3] != '3' && position[4] !='4' && position[5] != '5' && position[6] != '6' && position[7] != '7' && position[8] != '8' && position[9] != '9')
  253.         return 0;
  254.     else
  255.         return -1;
  256. }
  257.  
  258. void red () {
  259.   printf("\033[1;31m");
  260. }
  261.  
  262. void green () {
  263.   printf("\033[1;32m");
  264. }
  265.  
  266. void blue () {
  267.   printf("\033[1;34m");
  268. }
  269.  
  270. void yellow () {
  271.   printf("\033[1;33m");
  272. }
  273.  
  274. void purple () {
  275.   printf("\033[1;35m");
  276. }
  277.  
  278. void reset () {
  279.   printf("\033[0m");
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement