Skip to content

Instantly share code, notes, and snippets.

@saidinesh5
Created November 9, 2012 23:13
Show Gist options
  • Save saidinesh5/4048935 to your computer and use it in GitHub Desktop.
Save saidinesh5/4048935 to your computer and use it in GitHub Desktop.
One of the very first programs I have written... Here for purely its nostalgic value :D The clearmine(int rx,int ry) function is what taught me the basics of recurssion!
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
int main_array[10][10],disp_array[10][10],i,j,x,y,marked=0,correct=0;
//left over items :
//Game menu.
void new_random_board()
{
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
for(i=0;i<10;i++)
{
x=1+(int) (8.0*rand()/(RAND_MAX+1.0));//gets a random value for x;
y=1+(int) (8.0*rand()/(RAND_MAX+1.0));
if (main_array[x][y]==1)i--;
else main_array[x][y]=1;
}
}
void display()
{
system("cls");
printf("\n\n\t\t\t\t~~Minesweeper~~\n\n\n");
printf("\t\t \t |");
for(i=0;i<8;i++)printf(" %d |",i+1);
printf("\n");
for(i=1;i<9;i++)
{
printf("\t\t==================================================\n");
printf("\t\t| %d ||",i);
for(j=1;j<9;j++)
{
switch(disp_array[i][j])
{
case 10 :printf(" |");break;
case 11 :printf(" X |");break;
case 12 :printf(" ? |");break;
case 13 :printf(" - |");break;
default :printf(" %d |",disp_array[i][j]);break;
}
}
printf("| %d |\n",i);
}
printf("\t\t==================================================\n");
printf("\n\t \t\t |");
for(i=0;i<8;i++)printf(" %d |",i+1);
printf("\n");
printf("\n\n no.of mines marked :)%d\n\n",marked);
}
int validate(int x,int y){
int sum=0;
for(i=-1;i<2;i++)
for(j=-1;j<2;j++){
if(i==0&&j==0)continue;
else sum+=main_array[x+i][y+j];
}
return sum;
}
void clearmine(int rx,int ry)
{
int i,j;
for(i=-1;i<2;i++)
{
for(j=-1;j<2;j++)
{
if((i==0&&j==0)||(rx+i<1)||(ry+j<1)||(disp_array[rx+i][ry+j]==13)||(rx+i>8)||(ry+j>8))continue;
if ((validate(rx+i,ry+j))==0)
{
disp_array[rx+i][ry+j]=13;
clearmine(rx+i,ry+j);
}
else disp_array[rx+i][ry+j]=validate(rx+i,ry+j);
}
}
}
main()
{
int loop=1,action;
for(i=0;i<10;i++)for(j=0;j<10;j++)main_array[i][j]=0;
for(i=0;i<10;i++)for(j=0;j<10;j++)disp_array[i][j]=10;
system("");
new_random_board();
do{
display();
printf("\n Enter the value of the square to be selected in the format(x,y): ");
scanf("%d,%d",&x,&y);
printf(" Choose the action to be performed on (%d,%d):",x,y);
printf("\n 1:Select this square.\n");
printf(" 2:Mark this square.\n");
printf(" 3:Have a doubt upon this square.\n");
printf(" 4:Unmark this square.\n");
printf(" 5: Back.\n :");
action=getch();
action=action-48;
printf("%d\n",action);
switch(action)
{
case 1: if((main_array[x][y])==1){
system("cls");
printf("\n\n\n\n\n\n\t\t\t\tOOPS...Sorry!!!\n\t\t\t\tYou Lost!!\n\t\t\t\tGAME OVER!!! :(\n");
loop=0;}
else {
if (validate(x,y)==0)
{
disp_array[x][y]=13;
clearmine(x,y);
}
else disp_array[x][y]=validate(x,y);
}
break;
case 2:if(disp_array[x][y]==10||disp_array[x][y]==12){
disp_array[x][y]=11;
if(disp_array[x][y])correct++;
marked++;
}
break;
case 3: disp_array[x][y]=12;
break;
case 4: if(disp_array[x][y]==11)marked--;
disp_array[x][y]=10;
break;
}
if(correct==10)
{
system("cls");
printf("\n\n\n\n\n\n\t\t\t\tCongratulations!!!\n :)\n");
printf("\n\n\n\n\n\n\t\t\t\t YOU Won!!!\n\n\n\n\n\n\n :D\n");
loop=0;
}
}while (loop!=0);
system("pause");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment