Advertisement
Guest User

Tic-Tac-Toe

a guest
Dec 20th, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.62 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.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
  23. }
  24.  
  25. void main()
  26. {
  27.     system("cls");
  28.     menu();
  29.     getch();
  30.  
  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.         i+=3;
  114.     }
  115.  
  116.     i = 1;
  117.     while(i<=3)
  118.     {
  119.         if(board[i] * board[i+3] * board[i+6] == check_val)
  120.         {
  121.             if(board[i] == 2)
  122.                 return i;
  123.             if(board[i+3] == 2)
  124.                 return i+3;
  125.             if(board[i+6] == 2)
  126.                 return i+6;
  127.         }
  128.         i++;
  129.     }
  130.  
  131.     if(board[1] * board[5] * board[9] == check_val)
  132.     {
  133.         if(board[1] == 2)
  134.             return 1;
  135.         if(board[5] == 2)
  136.             return 5;
  137.         if(board[9] == 2)
  138.             return 9;
  139.     }
  140.  
  141.     if(board[3] * board[5] * board[7] == check_val)
  142.     {
  143.         if(board[3] == 2)
  144.             return 3;
  145.         if(board[5] == 2)
  146.             return 5;
  147.         if(board[7] == 2)
  148.             return 7;
  149.     }
  150.     return 0;
  151. }
  152.  
  153. void go(int n)
  154. {
  155.     if(turn % 2)
  156.         board[n] = 3;
  157.     else
  158.         board[n] = 5;
  159.     turn++;
  160. }
  161.  
  162. void player_first()
  163. {
  164.     int pos;
  165.  
  166.     check_draw();
  167.     draw_board();
  168.     gotoxy(30,18);
  169.     printf("Your Turn :> ");
  170.     scanf("%d",&pos);
  171.  
  172.     if(board[pos] != 2)
  173.         player_first();
  174.  
  175.     if(pos == posswin(player))
  176.     {
  177.         go(pos);
  178.         draw_board();
  179.         gotoxy(30,20);
  180.         printf("Player Wins");
  181.         getch();
  182.         exit(0);
  183.     }
  184.  
  185.     go(pos);
  186.     draw_board();
  187.     start_game();
  188. }
  189.  
  190. void start_game()
  191. {
  192.     if(posswin(comp))
  193.     {
  194.         go(posswin(comp));
  195.         flag = 1;
  196.     }
  197.     else if(posswin(player))
  198.         go(posswin(player));
  199.     else if(make2())
  200.         go(make2());
  201.     else
  202.         go(make4());
  203.     draw_board();
  204.  
  205.     if(flag)
  206.     {
  207.         gotoxy(30,20);
  208.         printf("Computer wins");
  209.         getch();
  210.     }
  211.     else
  212.         player_first();
  213. }
  214.  
  215. void check_draw()
  216. {
  217.     if(turn > 9)
  218.     {
  219.         gotoxy(30,20);
  220.         printf("Game Draw");
  221.         getch();
  222.         exit(0);
  223.     }
  224. }
  225.  
  226. void draw_board()
  227. {
  228.     int j;
  229.  
  230.     for(j=9; j<17; j++)
  231.     {
  232.         gotoxy(35,j);
  233.         printf("|       |");
  234.     }
  235.     gotoxy(28,11);
  236.     printf("-----------------------");
  237.     gotoxy(28,14);
  238.     printf("-----------------------");
  239.  
  240.     for(j=1; j<10; j++)
  241.     {
  242.         if(board[j] == 3)
  243.             put_X_O('X',j);
  244.         else if(board[j] == 5)
  245.             put_X_O('O',j);
  246.     }
  247. }
  248.  
  249. void put_X_O(char ch,int pos)
  250. {
  251.     int m;
  252.     int x = 31, y = 10;
  253.  
  254.     m = pos;
  255.  
  256.     if(m > 3)
  257.     {
  258.         while(m > 3)
  259.         {
  260.             y += 3;
  261.             m -= 3;
  262.         }
  263.     }
  264.     if(pos % 3 == 0)
  265.         x += 16;
  266.     else
  267.     {
  268.         pos %= 3;
  269.         pos--;
  270.         while(pos)
  271.         {
  272.             x+=8;
  273.             pos--;
  274.         }
  275.     }
  276.     gotoxy(x,y);
  277.     printf("%c",ch);
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement