Advertisement
rohits134

Tic Tac Toe: C++

Jul 8th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.69 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<graphics.h>
  4. #include<dos.h>
  5.  
  6.  
  7. void red();
  8. void green();
  9. void position1(int,int);
  10. void position2(int,int);
  11. void check();
  12. int turn=1,win=0,xc,yc;char game[3][3];
  13.  
  14. union REGS in,out;
  15.  
  16. void main()
  17. {
  18. int gd=DETECT,gm,errorcode;
  19. initgraph(&gd,&gm,"D:\\tcc\\BGI");
  20.  
  21. in.x.ax=4;
  22. in.x.cx=0;
  23. in.x.dx=0;
  24. int86(0X33,&in,&out);
  25. in.x.ax=1;
  26. int86(0X33,&in,&out);
  27.  
  28.  
  29.  
  30. outtextxy(100,20,"player 1");
  31. rectangle(100,30,150,80);
  32. outtextxy(180,20,"player 2");
  33. rectangle(180,30,230,80);
  34.  
  35.  
  36.  red();
  37.  green();
  38.  
  39. line(100,100,100,200);//vertical 1
  40. line(130,100,130,200);//vertical 2
  41. line(160,100,160,200);//vertical 3
  42. line(190,100,190,200);//vertical 4
  43. line(100,130,190,130);//horizontal 1
  44. line(100,160,190,160);//horizontal 2
  45.  
  46.  
  47.  
  48. while((turn<=9)&&(win==0))
  49.  { int  click=0;
  50.     while(click==0)
  51.      {
  52.        in.x.ax=3;
  53.     int86(51,&in,&out);
  54.  
  55.     if(out.x.bx==1)
  56.     {
  57.      click=1;
  58.  
  59.     xc=out.x.cx;
  60.     yc=out.x.dx;
  61.  
  62.        if(turn%2!=0)//player 1
  63.       {
  64.        setcolor(GREEN);
  65.       circle(xc,yc,3);
  66.  
  67.      position1(xc,yc);
  68.  
  69.     }
  70.  
  71.     else if(turn%2==0)//player 2
  72.     {
  73.        setcolor(RED);
  74.       circle(xc,yc,3);
  75.       position2(xc,yc);
  76.  
  77.      }
  78.  
  79.       }
  80.     }//end of inner while
  81.  
  82.   if(turn>=5)
  83.   check();
  84.   turn++;
  85.   delay(1000);
  86.  }//end of outer while
  87.  
  88. if(win==0)
  89. outtext("draw");
  90.  
  91.  // delay(3000);
  92.    getch();
  93.   closegraph();
  94.  
  95. // printf("....GAME OVER....");
  96. //getch();
  97. }//end of main
  98.  
  99.  
  100.  
  101.     void red()
  102.     {
  103.      setcolor(RED);
  104.      circle(205,55,15);
  105.     }
  106.  
  107.     void green()
  108.     {
  109.      setcolor(GREEN);
  110.      circle(125,55,15);
  111.      setcolor(WHITE);
  112.       }
  113.  
  114.     void position1(int xc,int yc)
  115.        {
  116.         if(((100<xc)&&(xc<130))&&((100<yc)&&(yc<130)))
  117.         game[0][0]='g';
  118.          else if(((130<xc)&&(xc<160))&&((100<yc)&&(yc<130)))
  119.          game[0][1]='g';
  120.          else if(((160<xc)&&(xc<190))&&((100<yc)&&(yc<130)))
  121.          game[0][2]='g';
  122.           else if(((100<xc)&&(xc<130))&&((130<yc)&&(yc<160)))
  123.           game[1][0]='g';
  124.         else if(((130<xc)&&(xc<160))&&((130<yc)&&(yc<160)))
  125.        game[1][1]='g';
  126.         else if(((160<xc)&&(xc<190))&&((130<yc)&&(yc<160)))
  127.        game[1][2]='g';
  128.         else if(((100<xc)&&(xc<130))&&((160<yc)&&(yc<190)))
  129.        game[2][0]='g';
  130.         else if(((130<xc)&&(xc<160))&&((160<yc)&&(yc<190)))
  131.        game[2][1]='g';
  132.         else if(((160<xc)&&(xc<190))&&((160<yc)&&(yc<190)))
  133.        game[2][2]='g';
  134.  
  135.        }
  136.       void position2(int xc,int yc)
  137.     {
  138.      if(((100<xc)&&(xc<130))&&((100<yc)&&(yc<130)))
  139.       game[0][0]='r';
  140.       else if(((130<xc)&&(xc<160))&&((100<yc)&&(yc<130)))
  141.       game[0][1]='r';
  142.      else if(((160<xc)&&(xc<190))&&((100<yc)&&(yc<130)))
  143.          game[0][2]='r';
  144.           else if(((100<xc)&&(xc<130))&&((130<yc)&&(yc<160)))
  145.           game[1][0]='r';
  146.         else if(((130<xc)&&(xc<160))&&((130<yc)&&(yc<160)))
  147.        game[1][1]='r';
  148.         else if(((160<xc)&&(xc<190))&&((130<yc)&&(yc<160)))
  149.        game[1][2]='r';
  150.         else if(((100<xc)&&(xc<130))&&((160<yc)&&(yc<190)))
  151.        game[2][0]='r';
  152.         else if(((130<xc)&&(xc<160))&&((160<yc)&&(yc<190)))
  153.        game[2][1]='r';
  154.         else if(((160<xc)&&(xc<190))&&((160<yc)&&(yc<190)))
  155.        game[2][2]='r';
  156.  
  157.      }
  158.  
  159.      void check()
  160.  
  161.     {
  162.       if(turn%2!=0)
  163.         {
  164.           if((game[0][0]==game[0][1])&&(game[0][0]==game[0][2]))//first row
  165.           {
  166.            if(game[0][0]=='g')
  167.            {outtext("player 1 wins");
  168.            win=1;}
  169.           }
  170.         else if((game[0][0]==game[1][1])&&(game[0][0]==game[2][2]))//leading diagonal
  171.      {
  172.       if(game[0][0]=='g')
  173.      { outtext("player 1 wins");
  174.       win=1;  }
  175.       }
  176.           else if((game[0][0]==game[1][0])&&(game[0][0]==game[2][0]))//first column
  177.      {
  178.       if(game[0][0]=='g')
  179.      { outtext("player 1 wins");
  180.       win=1;}
  181.       }
  182.           else    if((game[1][0]==game[1][1])&&(game[1][0]==game[1][2]))//2nd row
  183.      {
  184.       if(game[1][0]=='g')
  185.      { outtext("player 1 wins");
  186.       win=1;}
  187.       }
  188.         else if((game[2][0]==game[2][1])&&(game[2][0]==game[2][2])) //3rd row
  189.      {
  190.       if(game[2][0]=='g')
  191.      { outtext("player 1 wins");
  192.       win=1; }
  193.       }
  194.         else if((game[0][2]==game[1][1])&&(game[0][2]==game[2][0])) //other diagonal
  195.      {
  196.       if(game[0][2]=='g')
  197.      { outtext("player 1 wins");
  198.       win=1;}
  199.       }
  200.  
  201.         else if((game[0][1]==game[1][1])&&(game[0][1]==game[2][1])) //2nd column
  202.      {
  203.       if(game[0][1]=='g')
  204.      { outtext("player 1 wins");
  205.       win=1; }
  206.       }
  207.         else if((game[0][2]==game[1][2])&&(game[0][2]==game[2][2])) //3rd column
  208.      {
  209.       if(game[0][2]=='g')
  210.      { outtext("player 1 wins");
  211.       win=1;}
  212.       }
  213.      }
  214.  
  215.      else if(turn%2==0)
  216.     {
  217.           if((game[0][0]==game[0][1])&&(game[0][0]==game[0][2]))//1st row
  218.           {
  219.            if(game[0][0]=='r')
  220.          {  outtext("red wins");
  221.            win=1;}
  222.           }
  223.         else if((game[0][0]==game[1][1])&&(game[0][0]==game[2][2]))//leading diagonal
  224.      {
  225.       if(game[0][0]=='r')
  226.     {  outtext("player 2 wins");
  227.       win=1;}
  228.       }
  229.           else  if((game[0][0]==game[1][0])&&(game[0][0]==game[2][0])) //1st column
  230.      {
  231.       if(game[0][0]=='r')
  232.       {outtext("player 2 wins");
  233.       win=1;}
  234.       }
  235.          else   if((game[1][0]==game[1][1])&&(game[1][0]==game[1][2])) //2nd row
  236.      {
  237.       if(game[1][0]=='r')
  238.      { outtext("player 2 wins");
  239.       win=1; }
  240.       }
  241.            else if((game[2][0]==game[2][1])&&(game[2][0]==game[2][2]))//3rd row
  242.      {
  243.       if(game[2][0]=='r')
  244.       {outtext("player 2 wins");
  245.       win=1;}
  246.       }
  247.     else if((game[0][2]==game[1][1])&&(game[0][2]==game[2][0])) //other diagonal
  248.      {
  249.       if(game[0][2]=='r')
  250.      { outtext("player 2 wins");
  251.       win=1;}
  252.       }
  253.  
  254.         else if((game[0][1]==game[1][1])&&(game[0][1]==game[2][1])) //2nd column
  255.      {
  256.       if(game[0][1]=='r')
  257.      { outtext("player 2 wins");
  258.       win=1;}
  259.       }
  260.         else if((game[0][2]==game[1][2])&&(game[0][2]==game[2][2])) //3rd column
  261.      {
  262.       if(game[0][2]=='r')
  263.      { outtext("player 2 wins");
  264.       win=1;}
  265.       }
  266.     }
  267.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement