Advertisement
Radoan_Ahmed

game

Sep 27th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.39 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<windows.h>
  5.  
  6. int board[10] = {2,2,2,2,2,2,2,2,2,2};
  7. int turn = 1,flag = 0;
  8. int player,comp;
  9.  
  10. void menu();
  11. void go(int n);
  12. void start_game();
  13. void check_draw();
  14. void draw_board();
  15. void player_first();
  16. void put_X_O(char ch,int pos);
  17. COORD coord= {0,0};
  18. void gotoxy(int x,int y)
  19. {
  20.     coord.X = x;
  21.     coord.Y = y;
  22.  
  23.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
  24. }
  25.  
  26. void main()
  27. {
  28.     system("cls");
  29.     menu();
  30.     getch();
  31. }
  32.  
  33. void menu()
  34. {
  35.     int choice;
  36.     system("cls");
  37.     printf("\n--------MENU------");
  38.     printf("\n1 : Play with X");
  39.     printf("\n2 : Play with O");
  40.     printf("\n3 : Exit");
  41.     printf("\nEnter your choice: ");
  42.     scanf("%d",&choice);
  43.     turn = 1;
  44.     switch(choice)
  45.     {
  46.     case 1:
  47.         player =1;
  48.         comp = 0;
  49.         player_first();
  50.         break;
  51.     case 2:
  52.         player = 0;
  53.         comp = 1;
  54.         start_game();
  55.         break;
  56.     case 3:
  57.         exit(1);
  58.     default:
  59.         menu();
  60.     }
  61. }
  62.  
  63. int make2()
  64. {
  65.     if(board[5]==2)
  66.         return 5;
  67.     if(board[2]==2)
  68.         return 2;
  69.     if(board[4]==2)
  70.         return 4;
  71.     if(board[6]==2)
  72.         return 6;
  73.     if(board[8]==2)
  74.         return 8;
  75.     return 0;
  76. }
  77.  
  78. int make4()
  79. {
  80.     if(board[1]==2)
  81.         return 1;
  82.     if(board[3]==2)
  83.         return 3;
  84.     if(board[7]==2)
  85.         return 7;
  86.     if(board[9]==2)
  87.         return 9;
  88.     return 0;
  89. }
  90.  
  91. int posswin(int p)
  92. {
  93.     int i;
  94.     int check_val,pos;
  95.  
  96.     if(p == 1)
  97.         check_val = 18;
  98.     else
  99.         check_val = 50;
  100.  
  101.     i = 1;
  102.     while(i<=9)
  103.     {
  104.         if(board[i]*board[i+1]*board[i+2] == check_val)
  105.         {
  106.             if(board[i] == 2)
  107.                 return i;
  108.             if(board[i+1] == 2)
  109.                 return i+1;
  110.             if(board[i+2] == 2)
  111.                 return i+2;
  112.  
  113.         }
  114.         i+=3;
  115.     }
  116.  
  117.     i = 1;
  118.     while(i<=3)
  119.     {
  120.         if(board[i]*board[i+3]*board[i+6] == check_val)
  121.         {
  122.             if(board[i] == 2)
  123.                 return i;
  124.             if(board[i+3] == 2)
  125.                 return i+3;
  126.             if(board[i+6] == 2)
  127.                 return i+6;
  128.  
  129.         }
  130.         i++;
  131.     }
  132.  
  133.      if(board[3]*board[5]*board[7] == check_val)
  134.         {
  135.             if(board[3] == 2)
  136.                 return 3;
  137.             if(board[5] == 2)
  138.                 return 5;
  139.             if(board[i+7] == 2)
  140.                 return 7;
  141.  
  142.         }
  143.         return 0;
  144. }
  145.  
  146. void go(int n)
  147. {
  148.     if(turn % 2)
  149.         board[n] = 3;
  150.     else
  151.         board[n] = 5;
  152.     turn++;
  153. }
  154.  
  155. void player_first()
  156. {
  157.     int pos;
  158.  
  159.     check_draw();
  160.     draw_board();
  161.     gotoxy(30,18);
  162.     printf("Your Turn :");
  163.     scanf("%d",&pos);
  164.  
  165.     if(board[pos] != 2)
  166.         player_first();
  167.  
  168.     if(pos == posswin(player))
  169.     {
  170.         go(pos);
  171.         draw_board();
  172.         gotoxy(30,20);
  173.         printf("Player Wins");
  174.         getch();
  175.         exit(0);
  176.     }
  177.  
  178.     go(pos);
  179.     draw_board();
  180.     start_game();
  181. }
  182.  
  183. void start_game()
  184. {
  185.     if(posswin(comp))
  186.     {
  187.         go(posswin(comp));
  188.         flag = 1;
  189.     }
  190.     else if(posswin(player))
  191.         go(posswin(player));
  192.     else if(make2())
  193.         go(make2());
  194.     else
  195.         go(make4());
  196.     draw_board();
  197.  
  198.     if(flag)
  199.     {
  200.         gotoxy(30,20);
  201.         printf("Computer wins");
  202.         getch();
  203.     }
  204.     else
  205.         player_first();
  206. }
  207.  
  208. void check_draw()
  209. {
  210.     if(turn>9)
  211.     {
  212.         gotoxy(30,20);
  213.         printf("Game Draw");
  214.         getch();
  215.         exit(0);
  216.     }
  217. }
  218.  
  219. void draw_board()
  220. {
  221.     int j;
  222.  
  223.     for(j=9;j<17;j++)
  224.     {
  225.         gotoxy(35,j);
  226.         printf("|   |");
  227.     }
  228.     gotoxy(28,11);
  229.     printf("----------------");
  230.     gotoxy(28,14);
  231.     printf("----------------");
  232.  
  233.  
  234.     for(j=1;j<10;j++)
  235.     {
  236.         if(board[j] == 3)
  237.             put_X_O('X',j);
  238.         else if(board[j] == 5)
  239.             put_X_O('O',j);
  240.     }
  241. }
  242.  
  243. void put_X_O(char ch,int pos)
  244. {
  245.     int m;
  246.     int x = 31,y = 10;
  247.  
  248.     m = pos;
  249.  
  250.     if(m>3)
  251.     {
  252.         while(m>3)
  253.         {
  254.             y += 3;
  255.             m -=3;
  256.         }
  257.     }
  258.     if(pos % 3 == 0)
  259.         x += 16;
  260.     else
  261.     {
  262.         pos %= 3;
  263.         pos--;
  264.         while(pos)
  265.         {
  266.             x+=8;
  267.             pos--;
  268.         }
  269.     }
  270.     gotoxy(x,y);
  271.     printf("%c",ch);
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement