LOVEGUN

Tic Tac Toe (Personal Project)

Oct 12th, 2021 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int checkwin (char t[10], char c)
  5. {
  6.     int x,i;
  7.     x=0;
  8.     if ((t[1]==t[5]) &&  (t[5]==t[9]) && (t[9]==c))
  9.     {
  10.         x=1;
  11.         return x;
  12.     }
  13.  
  14.     if ((t[3]==t[5]) &&  (t[5]==t[7]) && (t[7]==c))
  15.     {
  16.         x=1;
  17.         return x;
  18.     }
  19.  
  20.     for (i=1;i<=3;i++)
  21.     {
  22.         if ((t[i]==t[i+3]) &&  (t[i+3]==t[i+6]) && (t[i+3]==c))
  23.         {
  24.             x=1;
  25.             return x;
  26.         }
  27.     }
  28.  
  29.     for (i=1;i<=7;i=i+3)
  30.     {
  31.         if ((t[i]==t[i+1]) &&  (t[i+1]==t[i+2]) && (t[i+2]==c))
  32.         {
  33.             x=1;
  34.             return x;
  35.             printf ("ahla");
  36.         }
  37.     }
  38.     return x;
  39. }
  40. int update (char t[10] ,int n,int i)
  41. {
  42.     if (i==1)
  43.     {
  44.         t[n]='X';
  45.     }else
  46.     {
  47.         t[n]='O';
  48.     }
  49.     return t;
  50. }
  51. int turn (int i)
  52. {
  53.     if (i==1)
  54.     {
  55.         i=2;
  56.     }else
  57.     {
  58.         i=1;
  59.     }
  60.     return i;
  61. }
  62. void gamepad (char t[10])
  63. {
  64.     printf ("       |        |\n");
  65.     printf ("   %c   |   %c    |   %c   \n",t[1],t[2],t[3]);
  66.     printf ("       |        |\n");
  67.     printf ("-------------------------\n");
  68.     printf ("       |        |\n");
  69.     printf ("   %c   |   %c    |   %c   \n",t[4],t[5],t[6]);
  70.     printf ("       |        |\n");
  71.     printf ("-------------------------\n");
  72.     printf ("       |        |\n");
  73.     printf ("   %c   |   %c    |   %c   \n",t[7],t[8],t[9]);
  74.     printf ("       |        |\n");
  75. }
  76. int reset (char t[10])
  77. {
  78.     int i;
  79.     for (i=1;i<=9;i++)
  80.     {
  81.         t[i]=i+48;
  82.     }
  83. }
  84. int draw (char t[10])
  85. {
  86.     int i,test;
  87.     i=0;
  88.     test=1;
  89.     for (i=1;i<=9;i++)
  90.     {
  91.         if (t[i]!='X' && t[i]!='O')
  92.         {
  93.             test=0;
  94.         }
  95.     }
  96.     return test;
  97. }
  98.  
  99. int main()
  100. {
  101.     char t[10] = {'0','1','2','3','4','5','6','7','8','9'};
  102.     int i,n,win1,win2,win0;
  103.     char res;
  104.     i=1;
  105.     do{
  106.         do{
  107.             printf ("\n        Welcome to TicTacToe      \n");
  108.             printf ("\nPlayer 1: X ///// Player 2: O\n");
  109.             gamepad (t);
  110.             do{
  111.                 printf ("\nPlayer %d choose a column: ",i);
  112.                 scanf ("%d",&n);
  113.             }while (t[n]=='X' || t[n]=='O');
  114.             update(t,n,i);
  115.             if (checkwin (t,'X')==1)
  116.             {
  117.                 win1=1;
  118.             }
  119.             if (checkwin (t,'O')==1)
  120.             {
  121.                 win2=1;
  122.             }
  123.             i=turn(i);
  124.             win0=draw(t);
  125.             system("cls");
  126.        } while (win1==0 && win2==0 && win0==0);
  127.        if (win1==1)
  128.        {
  129.            system("color B");
  130.            gamepad (t);
  131.            win1=0;
  132.            printf ("Player 1 won good job");
  133.        }
  134.        if (win2==1)
  135.        {
  136.            system("color A");
  137.            gamepad (t);
  138.            win2=0;
  139.            printf ("Player 2 won good job");
  140.        }
  141.        if (win0==1)
  142.        {
  143.            system("color C");
  144.            gamepad (t);
  145.            win0=0;
  146.            printf ("Draw");
  147.        }
  148.        do{
  149.             printf ("\nReplay another game? (Y/N)");
  150.             scanf ("%c",&res);
  151.        }while((toupper(res)!='N') && (toupper(res)!='Y'));
  152.        system("cls");
  153.        reset (t);
  154.      }while (toupper(res)!='N');
  155.      printf ("Thanks for using my program <3");
  156. }
  157.  
Add Comment
Please, Sign In to add comment