Advertisement
SwarupSaha

Tic Tac Toe Game

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