Advertisement
Guest User

T3 Game by Kartik

a guest
Jun 26th, 2011
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 20.26 KB | None | 0 0
  1. /*
  2. >>>>>>>>>>>>>>>>>>>>Program: Tic Tac Toe<<<<<<<<<<<<<<<<<<<<<
  3. >>>>>>>>>>>>>>>>>Programmer: Kartik Singhal<<<<<<<<<<<<<<<<<<
  4. >>>>>>>>>>>>>>Contact: kartiksinghal@gmail.com<<<<<<<<<<<<<<<
  5. */
  6.  
  7.  
  8. //List of Header Files used
  9. #include<graphics.h>
  10. #include<conio.h>
  11. #include<alloc.h>
  12. #include<dos.h>
  13. #include<stdlib.h>
  14.  
  15.  
  16. //List of Global Variables
  17. int ch,x,y,i,j,n;
  18. char xoro,winp[2],wong;
  19. char *buffo, *buffx, *buffch;
  20. int maxx,maxy;
  21. char mat[3][3]={' ',' ',' ',' ',' ',' ',' ',' ',' '};
  22. //Background Pattern showing the T3 Symbol
  23. char pattern[]={0xF8,0x20,0x2E,0x22,0x26,0x22,0xE,0x0};
  24.  
  25.  
  26. //List of Functions used
  27. void start(void);
  28. void boundary(void);
  29. void mainscreen(void);
  30. void instruction(void);
  31. void game(void);
  32. void puto(void);
  33. void putx(void);
  34. void matchdraw(void);
  35. void win(char);
  36. void result(void);
  37. void music(int);
  38.  
  39.  
  40. //Main function
  41. void main(void)
  42. {
  43.    int driver=DETECT,mode;
  44.    int areao,areax,areach;
  45.  
  46.  
  47.    //Initialization of graphics
  48.    initgraph(&driver,&mode,"c:\\tc\\bgi");
  49.  
  50.  
  51.    //Store screensize in terms of x and y
  52.    maxx=getmaxx();
  53.    maxy=getmaxy();
  54.  
  55.  
  56.    setlinestyle(0,0,3);
  57.  
  58.  
  59.    /*Storing images in memory :start*/
  60.    //Green Circle
  61.    setcolor(2);
  62.    circle(maxx/2+150,maxy/2,55);
  63.    areao=imagesize(maxx/2+90,maxy/2-60,maxx/2+210,maxy/2+60);
  64.    buffo=(char*)malloc(areao);
  65.    getimage(maxx/2+90,maxy/2-60,maxx/2+210,maxy/2+60,buffo);
  66.  
  67.  
  68.    //Lightblue Cross
  69.    setcolor(9);
  70.    line(maxx/2-49,maxy/2-49,maxx/2+49,maxy/2+49);
  71.    line(maxx/2-49,maxy/2+49,maxx/2+49,maxy/2-49);
  72.    areax=imagesize(maxx/2-50,maxy/2-50,maxx/2+50,maxy/2+50);
  73.    buffx=(char*)malloc(areax);
  74.    getimage(maxx/2-50,maxy/2-50,maxx/2+50,maxy/2+50,buffx);
  75.  
  76.  
  77.    //Red Tick Mark
  78.    setcolor(RED);
  79.    line(maxx/2,maxy/2+110,maxx/2+10,maxy/2+120);
  80.    line(maxx/2+10,maxy/2+120,maxx/2+40,maxy/2+100);
  81.    areach=imagesize(maxx/2-1,maxy/2+99,maxx/2+41,maxy/2+121);
  82.    buffch=(char*)malloc(areach);
  83.    getimage(maxx/2-1,maxy/2+99,maxx/2+41,maxy/2+121,buffch);
  84.    /*Storing images in memory :end*/
  85.  
  86.  
  87.    cleardevice();
  88.    start();
  89.    mainscreen();
  90. }
  91.  
  92.  
  93. //Shows the first screen
  94. void start(void)
  95. {
  96.    setcolor(15);
  97.    settextstyle(7,0,9);
  98.  
  99.  
  100.    setfillpattern(pattern,1);
  101.    bar(0,0,maxx,maxy);
  102.    outtextxy(maxx/2-(textwidth("Tic Tac Toe")/2),maxy/2-(textheight("Tic Tac Toe")/2)-100,"Tic Tac Toe");
  103.  
  104.  
  105.    settextstyle(2,HORIZ_DIR,7);
  106.    outtextxy(maxx-textwidth("Developed by:")-30,maxy-7*textheight("Developed by:"),"Developed by:");
  107.    outtextxy(maxx-textwidth("Kartik Singhal")-30,maxy-5.5*textheight("Kartik Singhal"),"Kartik Singhal");
  108.    outtextxy(maxx-textwidth("XI A")-30,maxy-4*textheight("XI A"),"XI A");
  109.    outtextxy(maxx-textwidth("DAV Public School")-30,maxy-2.5*textheight("DAV Public School"),"DAV Public School");
  110.    boundary();
  111.  
  112.  
  113.    settextstyle(0,HORIZ_DIR,1);
  114.    music(3);
  115. }
  116.  
  117.  
  118. //Makes boundary over the current screen
  119. void boundary(void)
  120. {
  121.    setcolor(6);
  122.    rectangle(0,0,maxx,maxy);
  123.    setcolor(15);
  124. }
  125.  
  126.  
  127. //Shows the main menu
  128. void mainscreen(void)
  129. {
  130.    cleardevice();
  131.  
  132.  
  133.    setfillpattern(pattern,1);
  134.    bar(0,0,maxx,maxy);
  135.  
  136.  
  137.    setcolor(15);
  138.    settextstyle(7,0,9);
  139.    outtextxy(maxx/2-(textwidth("Tic Tac Toe")/2),maxy/2-(textheight("Tic Tac Toe")/2)-150,"Tic Tac Toe");
  140.    boundary();
  141.  
  142.  
  143.    settextstyle(3,HORIZ_DIR,5);
  144.    outtextxy(maxx/2-100,maxy/2-textheight("P"),"Play Game");
  145.    outtextxy(maxx/2-100,maxy/2+textheight("I"),"Instructions");
  146.    outtextxy(maxx/2-100,maxy/2+textheight("E")*3,"Exit");
  147.  
  148.  
  149.    y=maxy/2-textheight("P")/2;
  150.    putimage(maxx/2-160,y,buffch,XOR_PUT);
  151.  
  152.  
  153.    //To move the red tick mark to choose the main options
  154.    while(1)
  155.    {
  156.       if(kbhit())
  157.       {
  158.          putimage(maxx/2-160,y,buffch,XOR_PUT);
  159.          delay(0);
  160.  
  161.  
  162.          ch=getch();
  163.          if(ch==80 || ch=='s' || ch=='S')
  164.             y+=2*textheight("I");
  165.          else if(ch==72 || ch=='w' || ch=='W')
  166.             y-=2*textheight("I");
  167.          else if(ch==27)
  168.          {
  169.             closegraph();
  170.             exit(0);
  171.          }
  172.          else if(ch=='\r' || ch==' ')
  173.          {
  174.             if(y>maxy/2-textheight("P") && y<maxy/2)
  175.             {
  176.                music(1);
  177.                game();
  178.             }
  179.             else if(y>maxy/2 && y<maxy/2+textheight("E")*3)
  180.             {
  181.                music(1);
  182.                instruction();
  183.             }
  184.             else if(y>maxy/2+textheight("E")*3)
  185.             {
  186.                music(1);
  187.                closegraph();
  188.                exit(0);
  189.             }
  190.          }
  191.          music(5);
  192.          if(y<maxy/2-textheight("P"))
  193.             y+=2*textheight("I");
  194.          else if(y>maxy/2+textheight("E")*4)
  195.             y-=2*textheight("I");
  196.          putimage(maxx/2-160,y,buffch,XOR_PUT);
  197.          delay(0);
  198.  
  199.  
  200.       }
  201.  
  202.  
  203.    }
  204.  
  205.  
  206. }
  207.  
  208.  
  209. //Shows the instruction screen
  210. void instruction(void)
  211. {
  212.    cleardevice();
  213.  
  214.  
  215.    setfillpattern(pattern,1);
  216.    bar(0,0,maxx,maxy);
  217.  
  218.  
  219.    setcolor(15);
  220.    settextstyle(7,0,8);
  221.    outtextxy(maxx/2-(textwidth("Instructions")/2),maxy/2-(textheight("Instructions")/2)-150,"Instructions");
  222.    boundary();
  223.    settextstyle(3,HORIZ_DIR,3);
  224.    outtextxy(50,maxy/2-50,"This 2 player game is a computerised version");
  225.    outtextxy(50,maxy/2,"of the TIC TAC TOE game you play using pen");
  226.    outtextxy(50,maxy/2+50,"and paper. Use arrow keys or 'w', 'a', 's' &");
  227.    outtextxy(50,maxy/2+100,"'d' keys to place the cross or zero to desired");
  228.    outtextxy(50,maxy/2+150,"place. Use 'Enter' or 'Space' key to select place.");
  229.  
  230.  
  231.    getch();
  232.    music(1);
  233.    mainscreen();
  234. }
  235.  
  236.  
  237. //Actually begins the game and controls it
  238. void game(void)
  239. {
  240.    //Reinitialisation of variables
  241.    n=4;
  242.    for(i=0;i<3;i++)
  243.       for(j=0;j<3;j++)
  244.          mat[i][j]=' ';
  245.  
  246.  
  247.    cleardevice();
  248.    boundary();
  249.  
  250.  
  251.    settextstyle(3,0,4);
  252.    setcolor(15);
  253.    outtextxy(maxx/2-textwidth("Player 1! Choose your option:")/2,maxy/2-150,"Player 1! Choose your option:");
  254.    putimage(maxx/2-200,maxy/2,buffx,XOR_PUT);
  255.    putimage(maxx/2+100,maxy/2-10,buffo,XOR_PUT);
  256.    x=maxx/2-170;
  257.    putimage(x,maxy/2+130,buffch,XOR_PUT);
  258.  
  259.  
  260.    //To move the red tick mark which allows to choose between X & O
  261.    while(1)
  262.    {
  263.       if(kbhit())
  264.       {
  265.          putimage(x,maxy/2+130,buffch,XOR_PUT);
  266.          delay(0);
  267.          ch=getch();
  268.          if(ch==75 || ch=='a' || ch=='A')
  269.             x=maxx/2-170;
  270.          else if(ch==77 || ch=='d' || ch=='D')
  271.             x=maxx/2+140;
  272.          else if(ch==27)
  273.          {
  274.             music(1);
  275.             mainscreen();
  276.          }
  277.          else if(ch=='\r' || ch==' ')
  278.          {
  279.                music(1);
  280.                break;
  281.          }
  282.          music(5);
  283.          putimage(x,maxy/2+130,buffch,XOR_PUT);
  284.          delay(0);
  285.  
  286.  
  287.       }
  288.    }
  289.    putimage(x,maxy/2+130,buffch,XOR_PUT);
  290.  
  291.  
  292.    if(x<maxx/2)
  293.       xoro='x';
  294.    else xoro='o';
  295.  
  296.  
  297.    cleardevice();
  298.  
  299.  
  300.    //Makes the grid that makes up the board used to play the game
  301.    line(maxx/2-75,20,maxx/2-75,maxy-10);
  302.    line(maxx/2+75,20,maxx/2+75,maxy-10);
  303.    line(maxx/2-225,maxy/2-75,maxx/2+225,maxy/2-75);
  304.    line(maxx/2-225,maxy/2+75,maxx/2+225,maxy/2+75);
  305.  
  306.  
  307.    boundary();
  308.    delay(500);
  309.    music(2);
  310.    delay(200);
  311.    if(xoro=='x')
  312.    {
  313.       putx();
  314.       do
  315.       {
  316.          puto();
  317.          putx();
  318.       }while(--n);
  319.    }
  320.    else if(xoro=='o')
  321.    {
  322.       puto();
  323.       do
  324.       {
  325.          putx();
  326.          puto();
  327.       }while(--n);
  328.    }
  329.    matchdraw();
  330. }
  331.  
  332.  
  333. //Draws and allows movement of O
  334. void puto(void)
  335. {
  336.    x=10;
  337.    y=350;
  338.    music(1);
  339.    putimage(x,y,buffo,XOR_PUT);
  340.    while(1)
  341.    {
  342.       if(kbhit())
  343.       {
  344.  
  345.  
  346.          putimage(x,y,buffo,XOR_PUT);
  347.          delay(0);
  348.          ch=getch();
  349.          if(ch==77 || ch=='d' || ch=='D')
  350.             x+=5;
  351.          else if(ch==75 || ch=='a' || ch=='A')
  352.             x-=5;
  353.          else if(ch==72 || ch=='w' || ch=='W')
  354.             y-=5;
  355.          else if(ch==80 || ch=='s' || ch=='S')
  356.             y+=5;
  357.          else if(ch==27)
  358.          {
  359.             music(1);
  360.             mainscreen();
  361.          }
  362.          else if(ch=='\r' || ch==' ')
  363.          {
  364.             if(x+60<=maxx/2-75 && y+60<=maxy/2-75 && mat[0][0]==' ')
  365.             {
  366.                putimage(maxx/2-208,maxy/2-210,buffo,XOR_PUT);
  367.                music(4);
  368.                delay(500);
  369.                mat[0][0]='o';
  370.                if(((mat[0][0]==mat[1][1]) && (mat[0][0]==mat[2][2])) || ((mat[0][0]==mat[0][1]) && (mat[0][0]==mat[0][2])) || ((mat[0][0]==mat[1][0]) && (mat[0][0]==mat[2][0])))
  371.                   win('o');
  372.             }
  373.             else if(x+60>=maxx/2+75 && y+60<=maxy/2-75 && mat[0][2]==' ')
  374.             {
  375.                putimage(maxx/2+93,maxy/2-210,buffo,XOR_PUT);
  376.                music(4);
  377.                delay(500);
  378.                mat[0][2]='o';
  379.                if(((mat[0][2]==mat[0][0]) && (mat[0][2]==mat[0][1])) || ((mat[0][2]==mat[1][2]) && (mat[0][2]==mat[2][2])) || ((mat[0][2]==mat[1][1]) && (mat[0][2]==mat[2][0])))
  380.                   win('o');
  381.             }
  382.             else if(x+60>=maxx/2-75 && x+60<=maxx/2+75 && y+60<=maxy/2-75 && mat[0][1]==' ')
  383.             {
  384.                putimage(maxx/2-60,maxy/2-210,buffo,XOR_PUT);
  385.                music(4);
  386.                delay(500);
  387.                mat[0][1]='o';
  388.                if(((mat[0][1]==mat[0][0]) && (mat[0][1]==mat[0][2])) || ((mat[0][1]==mat[1][1]) && (mat[0][1]==mat[2][1])))
  389.                   win('o');
  390.             }
  391.             else if(x+60<=maxx/2-75 && y+60>=maxy/2+75 && mat[2][0]==' ')
  392.             {
  393.                putimage(maxx/2-208,maxy/2+90,buffo,XOR_PUT);
  394.                music(4);
  395.                delay(500);
  396.                mat[2][0]='o';
  397.                if(((mat[2][0]==mat[0][0]) && (mat[2][0]==mat[1][0])) || ((mat[2][0]==mat[1][1]) && (mat[2][0]==mat[0][2])) || ((mat[2][0]==mat[2][1]) && (mat[2][0]==mat[2][2])))
  398.                   win('o');
  399.  
  400.  
  401.             }
  402.             else if(x+60>=maxx/2+75 && y+60>=maxy/2+75 && mat[2][2]==' ')
  403.             {
  404.                putimage(maxx/2+93,maxy/2+90,buffo,XOR_PUT);
  405.                music(4);
  406.                delay(500);
  407.                mat[2][2]='o';
  408.                if(((mat[2][2]==mat[0][0]) && (mat[2][2]==mat[1][1])) || ((mat[2][2]==mat[2][0]) && (mat[2][2]==mat[2][1])) || ((mat[2][2]==mat[0][2]) && (mat[2][2]==mat[1][2])))
  409.                   win('o');
  410.             }
  411.             else if(x+60>=maxx/2-75 && x+60<=maxx/2+75 && y+60>=maxy/2+75 && mat[2][1]==' ')
  412.             {
  413.                putimage(maxx/2-60,maxy/2+90,buffo,XOR_PUT);
  414.                music(4);
  415.                delay(500);
  416.                mat[2][1]='o';
  417.                if(((mat[2][1]==mat[0][1]) && (mat[1][1]==mat[2][1])) || ((mat[2][1]==mat[2][0]) && (mat[2][1]==mat[2][2])))
  418.                   win('o');
  419.             }
  420.             else if(x+60<=maxx/2-75 && y+60>=maxy/2-75 && y+60<=maxy/2+75 && mat[1][0]==' ')
  421.             {
  422.                putimage(maxx/2-208,maxy/2-60,buffo,XOR_PUT);
  423.                music(4);
  424.                delay(500);
  425.                mat[1][0]='o';
  426.                if(((mat[1][0]==mat[1][1]) && (mat[1][0]==mat[1][2])) || ((mat[1][0]==mat[0][0]) && (mat[1][0]==mat[2][0])))
  427.                   win('o');
  428.             }
  429.             else if(x+60>=maxx/2+75 && y+60>=maxy/2-75 && y+60<=maxy/2+75 && mat[1][2]==' ')
  430.             {
  431.                putimage(maxx/2+93,maxy/2-60,buffo,XOR_PUT);
  432.                music(4);
  433.                delay(500);
  434.                mat[1][2]='o';
  435.                if(((mat[1][2]==mat[0][2]) && (mat[1][2]==mat[2][2])) || ((mat[1][2]==mat[1][0]) && (mat[1][2]==mat[1][1])))
  436.                   win('o');
  437.             }
  438.             else if(x+60>=maxx/2-75 && x+60<=maxx/2+75 && y+60>=maxy/2-75 && y+60<=maxy/2+75 && mat[1][1]==' ')
  439.             {
  440.                putimage(maxx/2-60,maxy/2-60,buffo,XOR_PUT);
  441.                music(4);
  442.                delay(500);
  443.                mat[1][1]='o';
  444.                if(((mat[1][1]==mat[0][0]) && (mat[1][1]==mat[2][2])) || ((mat[1][1]==mat[0][1]) && (mat[1][1]==mat[2][1])) || ((mat[1][1]==mat[1][0]) && (mat[1][1]==mat[1][2])) || ((mat[1][1]==mat[2][0]) && (mat[1][1]==mat[0][2])))
  445.                   win('o');
  446.             }
  447.             else
  448.             {
  449.                putimage(x,y,buffo,XOR_PUT);
  450.                continue;
  451.             }
  452.             break;
  453.          }
  454.  
  455.  
  456.          //Checking the boundaries of the board for O
  457.          if(x<=maxx/2-225)
  458.             x+=5;
  459.          else if(x>=maxx/2+110)
  460.             x-=5;
  461.          else if(y<=maxy/2-220)
  462.             y+=5;
  463.          else if(y>=maxy/2+115)
  464.             y-=5;
  465.          putimage(x,y,buffo,XOR_PUT);
  466.          delay(0);
  467.       }
  468.    }
  469. }
  470.  
  471.  
  472. //Draws and allows movement of X
  473. void putx(void)
  474. {
  475.    x=10;
  476.    y=350;
  477.    music(1);
  478.    putimage(x,y,buffx,XOR_PUT);
  479.    while(1)
  480.    {
  481.       if(kbhit())
  482.       {
  483.  
  484.  
  485.          putimage(x,y,buffx,XOR_PUT);
  486.          delay(0);
  487.          ch=getch();
  488.          if(ch==77 || ch=='d' || ch=='D')
  489.             x+=5;
  490.          else if(ch==75 || ch=='a' || ch=='A')
  491.             x-=5;
  492.          else if(ch==72 || ch=='w' || ch=='W')
  493.             y-=5;
  494.          else if(ch==80 || ch=='s' || ch=='S')
  495.             y+=5;
  496.          else if(ch==27)
  497.          {
  498.             music(1);
  499.             mainscreen();
  500.          }
  501.          else if(ch=='\r' || ch==' ')
  502.          {
  503.             if(x+50<=maxx/2-75 && y+50<=maxy/2-75 && mat[0][0]==' ')
  504.             {
  505.                putimage(maxx/2-200,maxy/2-200,buffx,XOR_PUT);
  506.                music(4);
  507.                delay(500);
  508.                mat[0][0]='x';
  509.                if(((mat[0][0]==mat[1][1]) && (mat[0][0]==mat[2][2])) || ((mat[0][0]==mat[0][1]) && (mat[0][0]==mat[0][2])) || ((mat[0][0]==mat[1][0]) && (mat[0][0]==mat[2][0])))
  510.                   win('x');
  511.             }
  512.             else if(x+50>=maxx/2+75 && y+50<=maxy/2-75 && mat[0][2]==' ')
  513.             {
  514.                putimage(maxx/2+102,maxy/2-200,buffx,XOR_PUT);
  515.                music(4);
  516.                delay(500);
  517.                mat[0][2]='x';
  518.                if(((mat[0][2]==mat[0][0]) && (mat[0][2]==mat[0][1])) || ((mat[0][2]==mat[1][2]) && (mat[0][2]==mat[2][2])) || ((mat[0][2]==mat[1][1]) && (mat[0][2]==mat[2][0])))
  519.                   win('x');
  520.             }
  521.             else if(x+50>=maxx/2-75 && x+50<=maxx/2+75 && y+50<=maxy/2-75 && mat[0][1]==' ')
  522.             {
  523.                putimage(maxx/2-50,maxy/2-200,buffx,XOR_PUT);
  524.                music(4);
  525.                delay(500);
  526.                mat[0][1]='x';
  527.                if(((mat[0][1]==mat[0][0]) && (mat[0][1]==mat[0][2])) || ((mat[0][1]==mat[1][1]) && (mat[0][1]==mat[2][1])))
  528.                   win('x');
  529.             }
  530.             else if(x+50<=maxx/2-75 && y+50>=maxy/2+75 && mat[2][0]==' ')
  531.             {
  532.                putimage(maxx/2-200,maxy/2+100,buffx,XOR_PUT);
  533.                music(4);
  534.                delay(500);
  535.                mat[2][0]='x';
  536.                if(((mat[2][0]==mat[0][0]) && (mat[2][0]==mat[1][0])) || ((mat[2][0]==mat[1][1]) && (mat[2][0]==mat[0][2])) || ((mat[2][0]==mat[2][1]) && (mat[2][0]==mat[2][2])))
  537.                   win('x');
  538.             }
  539.             else if(x+50>=maxx/2+75 && y+50>=maxy/2+75 && mat[2][2]==' ')
  540.             {
  541.                putimage(maxx/2+102,maxy/2+100,buffx,XOR_PUT);
  542.                music(4);
  543.                delay(500);
  544.                mat[2][2]='x';
  545.                if(((mat[2][2]==mat[0][0]) && (mat[2][2]==mat[1][1])) || ((mat[2][2]==mat[2][0]) && (mat[2][2]==mat[2][1])) || ((mat[2][2]==mat[0][2]) && (mat[2][2]==mat[1][2])))
  546.                   win('x');
  547.             }
  548.             else if(x+50>=maxx/2-75 && x+50<=maxx/2+75 && y+50>=maxy/2+75 && mat[2][1]==' ')
  549.             {
  550.                putimage(maxx/2-50,maxy/2+100,buffx,XOR_PUT);
  551.                music(4);
  552.                delay(500);
  553.                mat[2][1]='x';
  554.                if(((mat[2][1]==mat[0][1]) && (mat[1][1]==mat[2][1])) || ((mat[2][1]==mat[2][0]) && (mat[2][1]==mat[2][2])))
  555.                   win('x');
  556.             }
  557.             else if(x+50<=maxx/2-75 && y+50>=maxy/2-75 && y+50<=maxy/2+75 && mat[1][0]==' ')
  558.             {
  559.                putimage(maxx/2-200,maxy/2-50,buffx,XOR_PUT);
  560.                music(4);
  561.                delay(500);
  562.                mat[1][0]='x';
  563.                if(((mat[1][0]==mat[1][1]) && (mat[1][0]==mat[1][2])) || ((mat[1][0]==mat[0][0]) && (mat[1][0]==mat[2][0])))
  564.                   win('x');
  565.             }
  566.             else if(x+50>=maxx/2+75 && y+50>=maxy/2-75 && y+50<=maxy/2+75 && mat[1][2]==' ')
  567.             {
  568.                putimage(maxx/2+102,maxy/2-50,buffx,XOR_PUT);
  569.                music(4);
  570.                delay(500);
  571.                mat[1][2]='x';
  572.                if(((mat[1][2]==mat[0][2]) && (mat[1][2]==mat[2][2])) || ((mat[1][2]==mat[1][0]) && (mat[1][2]==mat[1][1])))
  573.                   win('x');
  574.             }
  575.             else if(x+50>=maxx/2-75 && x+50<=maxx/2+75 && y+50>=maxy/2-75 && y+50<=maxy/2+75 && mat[1][1]==' ')
  576.             {
  577.                putimage(maxx/2-50,maxy/2-50,buffx,XOR_PUT);
  578.                music(4);
  579.                delay(500);
  580.                mat[1][1]='x';
  581.                if(((mat[1][1]==mat[0][0]) && (mat[1][1]==mat[2][2])) || ((mat[1][1]==mat[0][1]) && (mat[1][1]==mat[2][1])) || ((mat[1][1]==mat[1][0]) && (mat[1][1]==mat[1][2])) || ((mat[1][1]==mat[2][0]) && (mat[1][1]==mat[0][2])))
  582.                   win('x');
  583.             }
  584.             else
  585.             {
  586.                putimage(x,y,buffx,XOR_PUT);
  587.                continue;
  588.             }
  589.             break;
  590.          }
  591.  
  592.  
  593.          //Checking the boundaries of the board for X
  594.          if(x<=maxx/2-225)
  595.             x+=5;
  596.          else if(x>=maxx/2+125)
  597.             x-=5;
  598.          else if(y<=maxy/2-220)
  599.             y+=5;
  600.          else if(y>=maxy/2+125)
  601.             y-=5;
  602.          putimage(x,y,buffx,XOR_PUT);
  603.          delay(0);
  604.       }
  605.    }
  606. }
  607.  
  608.  
  609. //Shows 'matchdraw' message in case no player wins
  610. void matchdraw(void)
  611. {
  612.    setfillstyle(8,8);
  613.    bar(maxx/2-150,maxy/2-100,maxx/2+150,maxy/2+100);
  614.    setcolor(4);
  615.    rectangle(maxx/2-150,maxy/2-100,maxx/2+150,maxy/2+100);
  616.    outtextxy(maxx/2-textwidth("Game Drawn!!")/2,maxy/2-textheight("Game Drawn!!")+10,"Game Drawn!!");
  617.    music(3);
  618.    setfillstyle(1,15);
  619.    delay(500);
  620.    music(1);
  621.    mainscreen();
  622. }
  623.  
  624.  
  625. //Checks which player wins the game
  626. void win(char won)
  627. {
  628.    wong=won;
  629.    if(won=='o' && xoro=='o')
  630.       winp[0]='1';
  631.    else if(won=='o' && xoro=='x')
  632.       winp[0]='2';
  633.    else if(won=='x' && xoro=='o')
  634.       winp[0]='2';
  635.    else if(won=='x' && xoro=='x')
  636.       winp[0]='1';
  637.    winp[1]='\0';
  638.    result();
  639. }
  640.  
  641.  
  642. //Shows the result of the match
  643. void result(void)
  644. {
  645.    setfillstyle(8,8);
  646.    bar(maxx/2-150,maxy/2-100,maxx/2+150,maxy/2+100);
  647.    if(wong=='o')
  648.       setcolor(2);
  649.    else setcolor(9);
  650.    rectangle(maxx/2-150,maxy/2-100,maxx/2+150,maxy/2+100);
  651.    outtextxy(maxx/2-textwidth("Player ")/2,maxy/2-50,"Player ");
  652.    outtextxy(maxx/2+textwidth("Player ")/2,maxy/2-50,winp);
  653.    outtextxy(maxx/2-textwidth("won the game!!")/2,maxy/2,"won the game!!");
  654.    music(3);
  655.    setfillstyle(1,15);
  656.    delay(500);
  657.    music(1);
  658.    mainscreen();
  659. }
  660.  
  661.  
  662. //Plays various music used in the game
  663. void music ( int type )
  664. {
  665.    /* natural frequencies of 7 notes */
  666.    float octave[7] = { 130.81, 146.83, 164.81, 174.61, 196, 220, 246.94 } ;
  667.    int mn, mi ;
  668.  
  669.  
  670.    switch ( type )
  671.    {
  672.       case 1 :
  673.          for ( mi = 0 ; mi < 7 ; mi++ )
  674.          {
  675.             sound ( octave[mi] * 8 ) ;
  676.             delay ( 30 ) ;
  677.          }
  678.          nosound() ;
  679.          break ;
  680.  
  681.  
  682.       case 2 :
  683.          for ( mi = 0 ; mi < 15 ; mi++ )
  684.          {
  685.             mn = random ( 7 ) ;
  686.             sound ( octave[mn] * 4 ) ;
  687.             delay ( 100 ) ;
  688.          }
  689.          nosound() ;
  690.          break ;
  691.  
  692.  
  693.       case 3 :
  694.          while ( !kbhit() )
  695.          {
  696.             mn = random ( 7 ) ;
  697.             sound ( octave[mn] * 4 ) ;
  698.             delay ( 100 ) ;
  699.          }
  700.          nosound() ;
  701.  
  702.  
  703.          /* flush the keyboard buffer */
  704.          if ( getch() == 0 )
  705.             getch() ;
  706.  
  707.  
  708.          break ;
  709.  
  710.  
  711.       case 4 :
  712.          for ( mi = 4 ; mi >= 0 ; mi-- )
  713.          {
  714.             sound ( octave[mi] * 4 ) ;
  715.             delay ( 15 ) ;
  716.          }
  717.          nosound() ;
  718.          break ;
  719.  
  720.  
  721.       case 5 :
  722.          sound ( octave[6] * 2 ) ;
  723.          delay ( 50 ) ;
  724.          nosound() ;
  725.    }
  726. }
  727.  
  728.  
  729. /*
  730. >>>>>>>>>>>>>>>>>>>>>>>>>END OF PROGRAM<<<<<<<<<<<<<<<<<<<<<<<<<
  731. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement