Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.40 KB | None | 0 0
  1. Mine Sweeper Game
  2.  
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <graphics.h>
  6. #include <dos.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #define MROW 8
  10. #define MCOL 8
  11. #define MINES 5
  12. #define BSIZE 15
  13. union REGS i,o;
  14. //9 for mines
  15. main()
  16. {
  17. int bm=DETECT,bg=DETECT,maxx,maxy,button,x,y,i,j,checkx,checky,nclick;
  18. int arr[8][8],dot[MINES];
  19. int count1,count2;
  20. int row,col;
  21. time_t t;
  22. clrscr();
  23. initgraph(&bm,&bg,"e:\tcc\bgi");
  24. maxx=getmaxx();
  25. maxy=getmaxy();
  26. if(initmouse()==0)
  27.          {
  28.          closegraph();
  29.          restorecrtmode();
  30.          printf("MOUSE NOT FOUND");
  31.          exit(1);
  32.          }
  33. while(!kbhit())
  34.          {
  35.          char loop='t';
  36.          setcolor(WHITE);
  37.          setviewport(0,0,getmaxx(),getmaxy(),1);
  38.          clearviewport();
  39.          showmouse();
  40.          //It Blanks the Array
  41.          for(i=0;i<MROW;i++)
  42.                  for(j=0;j<MCOL;j++)
  43.                          arr[i][j]=0;
  44.          //put mines in random blocks
  45.          srand((unsigned) time(&t));
  46.          for(i=0;i<MINES;i++)
  47.                  {
  48.                  dot[i]=rand()%64;
  49.                  for(j=0;j<i;j++)
  50.                          if(dot[i]==dot[j])
  51.                                  break;
  52.                  if(i!=j)
  53.                          {
  54.                          i--;
  55.                          continue;
  56.                          }
  57.                  else
  58.                          {
  59.                          row=dot[i]/MROW;
  60.                          col=dot[i]%MCOL;
  61.                          arr[row][col]=9;
  62.                          }
  63.                  }
  64.          //put mines value in other blocks
  65.          for(row=0;row<MROW;row++)
  66.                  {
  67.                  for(col=0;col<MCOL;col++)
  68.                          {
  69.                          if(arr[row][col]!=9)        //No Mine
  70.                                  {
  71.                                  int i,num=0,temp,ecol,erow,startrow;
  72.                                  temp=col==0?col:col-1;
  73.                                  ecol=col!=MCOL-1?col+1:col;
  74.                                  startrow=row==0?row:row-1;
  75.                                  erow=row==MROW-1?row:row+1;
  76.                                  for(i=startrow;i<=erow;i++)
  77.                                          for(j=temp;j<=ecol;j++)
  78.                                                  if(arr[i][j]==9)
  79.                                                          num++;
  80.                                  arr[row][col]=num;
  81.                                  }
  82.                          }
  83.                  }
  84.  
  85.          rectangle(0,0,maxx,maxy);
  86.          //sets view port to the play area only
  87.          setviewport(200,190,385,370,1);
  88.          nclick=0;
  89.          while(!kbhit()&&loop=='t')
  90.                  {
  91.                  int row=10,col=5;
  92.                  setlinestyle(0,0,3);
  93.                  rectangle(row-5,col-3,row*17,col*33); //Draws outer block
  94.                  setlinestyle(0,0,2);
  95.                  for(i=0;i<MROW;i++)
  96.                          {
  97.                          for(j=0;j<MCOL;j++)
  98.                                  {
  99.                                  rectangle(row,col,row+BSIZE,col+BSIZE);
  100.                                  if(arr[i][j]!=9)
  101.                                          {
  102.                                          int temp[3];
  103.                                          if(arr[i][j]>=80)
  104.                                                  outtextxy(row+4,col+4,"F");
  105.                                          else
  106.                                                  if(arr[i][j]>=50)
  107.                                                  {
  108.                                                  itoa((arr[i][j]-50),temp,10);
  109.                                                  outtextxy(row+BSIZE/2,col+BSIZE/2,temp);
  110.                                                  }
  111.                                          }
  112.                                  row+=BSIZE+5;
  113.                                  }
  114.                          row=10;
  115.                          col+=BSIZE+5;
  116.                          }
  117.                  if(nclick==MROW*MCOL-MINES)
  118.                          {
  119.                          setviewport(0,0,getmaxx(),getmaxy(),1);
  120.                          outtextxy(200,100,"CONGRATS! YOU WON THE GAME");
  121.                          getch();
  122.                          loop='f';
  123.                          }
  124.                  getpos(&button,&x,&y);
  125.  
  126.                  //puts F tag in Array
  127.                  if(button&2)
  128.                          {
  129.                          int val,prex=x,prey=y;
  130.                          x=210+(x-210)/20*20;
  131.                          y=195+(y-195)/20*20;
  132.                          val=arr[(y-195)/20][(x-210)/20];
  133.                          if(val<10 || val>=80)
  134.                                  {
  135.                                  val=val>=80?val-80:80+val;
  136.                                  arr[(y-195)/20][(x-210)/20]=val;
  137.  
  138.                                  while(button&2)
  139.                                          getpos(&button,&x,&y);
  140.  
  141.                                  hidemouse();
  142.                                  setpos(30,50);
  143.                                  clearviewport();
  144.                                  showmouse();
  145.                                  setpos(prex,prey);
  146.                                  }
  147.                          }
  148.  
  149.                  if(button&1 && x>=210 && x<=365 && y>=195 && y<=350)
  150.                          {
  151.                          char temp[4];
  152.                          int trow,tcol;
  153.                          //determine current box
  154.                          x=210+(x-210)/20*20;
  155.                          y=195+(y-195)/20*20;
  156.                          trow=(y-195)/20;
  157.                          tcol=(x-210)/20;
  158.                          if(arr[trow][tcol]==9)
  159.                                  {
  160.                                  int row=10,col=5;
  161.                                  setviewport(0,0,getmaxx(),getmaxy(),1);
  162.                                  hidemouse();
  163.                                  outtextxy(200,100,"SORRY,YOU LOOSE THE GAME");
  164.                                  setviewport(200,190,385,370,1);
  165.                                  //prints X marks in END on screen
  166.                                  for(i=0;i<MROW;i++)
  167.                                          {
  168.                                          for(j=0;j<MCOL;j++)
  169.                                                  {
  170.                                                  if(arr[i][j]==9 || arr[i][j]==89)
  171.                                                          {
  172.                                                          setfillstyle(1,RED);
  173.                                                          bar(row,col,row+BSIZE,col+BSIZE);
  174.                                                          outtextxy(row+5,col+5,"x");
  175.                                                          }
  176.                                                  row+=20;
  177.                                                  }
  178.                                          row=10;
  179.                                          col+=20;
  180.                                          }
  181.                                  showmouse();
  182.                                  setviewport(0,0,getmaxx(),getmaxy(),1);
  183.                                  setfillstyle(1,YELLOW);
  184.                                  bar(170,400,270,425);
  185.                                  bar(330,400,430,425);
  186.                                  while(loop=='t')
  187.                                          {
  188.                                          setcolor(BLUE);
  189.                                          outtextxy(180,410,"PLAY AGAIN");
  190.                                          outtextxy(360,410,"QUIT");
  191.                                          getpos(&button,&x,&y);
  192.                                          if(!button&1)
  193.                                                  {
  194.                                                  if(x>=170 && x<=270 && y>=400 && y<=425)
  195.                                                          {
  196.                                                          setfillstyle(1,CYAN);
  197.                                                          bar(170,400,270,425);
  198.                                                          setfillstyle(1,YELLOW);
  199.                                                          bar(330,400,430,425);
  200.                                                          outtextxy(180,410,"PLAY AGAIN");
  201.                                                          outtextxy(360,410,"QUIT");
  202.                                                          while(x>=170 && x<=270 && y>=400 && y<=425 && !button&1)
  203.                                                                  getpos(&button,&x,&y);
  204.                                                          }
  205.                                                  if(x>=330 && x<=430 && y>=400 && y<=425)
  206.                                                          {
  207.                                                          setfillstyle(1,CYAN);
  208.                                                          bar(330,400,430,425);
  209.                                                          setfillstyle(1,YELLOW);
  210.                                                          bar(170,400,270,425);
  211.                                                          }
  212.                                                          outtextxy(180,410,"PLAY AGAIN");
  213.                                                          outtextxy(360,410,"QUIT");
  214.                                                          while(x>=330 && x<=430 && y>=400 && y<=425 && !button&1)
  215.                                                                  getpos(&button,&x,&y);
  216.                                                  }
  217.                                          else
  218.                                                  {
  219.                                                  if(x>=170 && x<=270 && y>=400 && y<=425)
  220.                                                          {
  221.                                                          setviewport(200,190,385,370,1);
  222.                                                          loop='f';
  223.                                                          hidemouse();
  224.                                                          break;
  225.                                                          }
  226.                                                  if(x>=330 && x<=430 && y>=400 && y<=425)
  227.                                                          {
  228.                                                          restorecrtmode();
  229.                                                          closegraph();
  230.                                                          exit(0);
  231.                                                          }
  232.                                                  }
  233.                                          }
  234.                                  }
  235.                          else
  236.                                  { //NOT ON DANGER AREA
  237.                                  if(arr[trow][tcol]<50)
  238.                                          {
  239.                                          nclick++;
  240.                                          arr[trow][tcol]+=50;
  241.                                          }
  242.                                  }
  243.                          }
  244.                  }
  245.          if(loop=='t')
  246.                  {
  247.                  restorecrtmode();
  248.                  closegraph();
  249.                  exit(0);
  250.                  }
  251.          }
  252. }
  253.  
  254. initmouse()
  255. {
  256. i.x.ax=0;
  257. int86(0x0033,&i,&o);
  258. return(o.x.ax);
  259. }
  260.  
  261. showmouse()
  262. {
  263. i.x.ax=1;
  264. int86(0x0033,&i,&o);
  265. }
  266.  
  267. hidemouse()
  268. {
  269. i.x.ax=2;
  270. int86(0x0033,&i,&o);
  271. }
  272.  
  273. restarea(int x1,int x2,int y1,int y2)
  274. {
  275. i.x.ax=7;
  276. i.x.cx=x1;
  277. i.x.dx=x2;
  278. int86(0x0033,&i,&o);
  279. i.x.ax=8;
  280. i.x.cx=y1;
  281. i.x.dx=y2;
  282. int86(0x0033,&i,&o);
  283. }
  284.  
  285. setpos(int x,int y)
  286. {
  287. i.x.ax=4;
  288. i.x.cx=x;
  289. i.x.dx=y;
  290. int86(0x0033,&i,&o);
  291. }
  292.  
  293. getpos(int *button,int *x,int *y)
  294. {
  295. i.x.ax=3;
  296. int86(0x0033,&i,&o);
  297. *button=o.x.bx;
  298. *x=o.x.cx;
  299. *y=o.x.dx;
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement