Wednesday 14 February 2018

Mine Sweeper Game in C

Mine Sweeper Game in c

Minesweeper is a single-player puzzle video game. The objective of the game is to clear a rectangular board containing hidden "mines" or bombs without detonating any of them, with help from clues about the number of neighboring mines in each field. The game originates from the 1960s, and has been written for many computing platforms in use today. It has many variations and offshoots.

SOURCE CODE

//Program password =Bhavesh

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>



void welcome();
void rand_mines(char msweep[12][12]);
void printmatrix(char msweep[12][12],int r,char user_chart[12][12]);
int process(char msweep[12][12],int r,int c,char user_chart[12][12]);
void load();
void pass();


int main()

{                    // size of array is 10,array starts from 1 to 11

char msweep[12][12] = {{'0'}};
int i,r,c;
char user_chart[12][12] = {{'0'}};
pass();
load();
// welcome();

 rand_mines(msweep); //calling of random function
     printf("Enter your location(ONLY 1 - 11) on the minefield x,y\n");
     scanf("%d%d",&r,&c);
     printmatrix(msweep,12,user_chart);
     i = process(msweep,r,c,user_chart); //returns 1 or 0,1 is notmine 0 = mine

  while(i == 1)
   {
     printf("Lucky , live on for another step\n");
     printf(" %c Surrounding MINEs\n\n",msweep[r][c]);
     printmatrix(msweep,12,user_chart);
     printf("enter next move...(ONLY 1 - 11) ");
     scanf("%d%d",&r,&c);
     i=0;
     i = process(msweep,r,c,user_chart);
   }
  if(i==0)
   printf("Game OVER, ta ta. you stepped on a MINE !!\n");
   getch(); // to hold the output screen
return 0;

}

void welcome()
{
  char op; // opereation
  clrscr();
  printf("Welcome to MINESWEEPER in C >>.....\n");

 printf("Enter <<\n");
 printf("         i for instructions\n");
 printf("         any other key to enter game\n");
 scanf("%c",&op);

 if(op == 'i')
  {
   printf("OH DEAR, what a shock you are unfortunatly in the midst of a "); printf("mine field.\n");
   printf("Enter the coordinates of the x and y plane between 1 to 11\n");
   printf("Are you destined to DIE or live ?\n");
   printf("HA ha ha hah, GOOD LUCK\n\n");

  }
else
 return;
}

void rand_mines(char msweep[12][12])  // function to plant mines (random function)

{

int r,c,m;

 //srand(12);

 for(m=0;m<20;m++) // plant 20 rand mines(m

  {
   r = rand() % 13; // this is mine planting
    // so can be at the edges aswell
   c = rand() % 13; // so 0 to 13 is APPROPRIATE.


   msweep[r][c] = '9';
  printf("%d %d \n",r,c);

  }
clrscr();
return;

}

void printmatrix(char msweep[][12],int r,char user_chart[12][12])
{

int i,j;

printf("   .-.-.-.-.-.-.-.-.-.-.-.\n");

 for(i=1;i<r;i++)
  {
   printf("./.");

   for(j=1;j<12;j++) //printing 1 to 11
    {
     printf("%c ",user_chart[i][j]);//to refer to mines use msweep[i][j]
    }

   printf(".\\.");

   printf("\n");

  }

printf(".-.-.-.-.-.-.-.-.-.-.-.-.\n\n");

return;
}

int process(char msweep[12][12],int r,int c,char user_chart[12][12])
{

 int i=r,j=c,b=0,k;
 char C;
  clrscr();
 if(msweep[r][c] == '9')
 {  k=0;
   return k;
 }
 else
  {
   if(msweep[i-1][j-1] == '9')
    b++;
   if(msweep[i-1][j] == '9')
    b++;
   if(msweep[i-1][j+1] == '9')
    b++;
   if(msweep[i][j-1] == '9')
    b++;
   if(msweep[i][j+1] == '9')
    b++;
   if(msweep[i+1][j-1] == '9')
    b++;
   if(msweep[i+1][j] == '9')
    b++;
   if(msweep[i+1][j+1] == '9')
    b++;

  C = (char)(((int)'0')+b); // to covert int to char;

   msweep[r][c] = C;
   user_chart[r][c] = C;

  }

 return 1;

}

void load() 
{
int i;
clrscr();
textcolor(11);
gotoxy(20,10);
printf("Loading.....\n");
       // cout<<"Loading\n";
for(i=1;i<=80;i++)
{
textcolor(RED);
cprintf("|");
}
for(i=1;i<=80;i++)
{
gotoxy(i,11);
textcolor(WHITE);
delay(50);
cprintf("|");
}
textcolor(10);
cprintf("Loading Completed....");
textcolor(7);
getch();
}
void pass()
{
char pwd[]="Bhavesh",verify[20],ch;
int i=0;
clrscr();
textcolor(6);
cprintf("\n\nEnter The Password To Verify:");
textcolor(7);
do
{
ch=getch();
if(ch!=13)
{ verify[i++]=ch;
  printf("*");
}
else
{ verify[i++]='\0';
}
}while(ch!=13);
if(strcmp(pwd,verify)==0)
{ textcolor(9);
printf("\n\nPassword Matched....");
  textcolor(7);
}
else
     { printf("\nWrong Password ...");
getch();
  pass();
}

}




OUTPUT


Password screen

Loading Screen 


Main Game Screen


Game Over




No comments:

Post a Comment