Help needed with minesweeper recursion!
Hello to everybody.
I am new to this forum and i think i ll like it very much.
I study at a computer science university and we learn c language...
So now i am up to make a minesweeper game for the university....
and here comes the problem...:
I ve almost done with the whole game, but i miss one basic thing.They want me to put a recursive function in the source code that is going to check all the neighbour cells of the chosen one by the user in order to build the opened field that the chosen cell opens...(like in the windows minesweeper when you try a cell and its emty it opens all the nearby emty cells until it finds walls made of numbers 1-8 that indicate that their is mines nearby).
The problem is that i tried to make the recursive function in multiple ways but none of them was ok....so here i put my whole source code thing with its comments...
please if you are able to help me post the right code for the function below...
or pm me with any question...
Thanks in advance
to all of you and to this great community that helps us share our knowledge...
THE GAME IS IN C LANGUAGE.
here is the link to download the source code----> minesweeper.zip
virus scan(even though it is just a source code file....)----> Multi-Engine Antivirus Scanner - Services - NoVirusThanks.org
Really many thanks in advance!
here are 2 functions....the reason is that this 2 are 2 diferrent tries to make tha same thing....(didnt manage to built that recursive function either way......)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//********************************************MINESWEEPER************************************************
//************PLEASE JUST TRY TO HELP ME WITH THE RECURSION.....CAUSE THE REST OF THE GAME IS JUST WHAT I NEED.....THANKS IN ADVANCE*********************
//HINTS
//MINES ARE INDICATED BY 9
//CLOSED CELLS BY 0-8 AND SPECIFICLY 0 INDICATES THE STILL CLOSED BUT EMTY CELL AND 1-8 THE STILL CLOSED BUT FILLED WITH THIS NUMBER CELL
//10-18 CELLS INDICATE THA OPENED CELLS...AND AS ABOVE...10 INCDICATES OPENED BUT EMTY CELL AND 11-18 OPENED BUT FILLED WITH NUMBER CELL...
//first try to make the recursive function that is going to check all the neighbour cells if they have a number from 1 to 8.
//if they have 0 then check their neighbours too!
//in order to differ the "opened" cells from the "closed" ones i need the function to add 10 to the cell
//so that if the cell contains 0 then it will become a 10(so that i am going to be able to show to the user that plays the game
//a "." that indicates the opened and emty cell)
//also if it contains 1-8 it will add 10 so that i am going to know that in my int array all the 1-8 are closed cells(numbered) and all the 11-18 cells
//are going to be the opened cells cointaining a number from 1-8(in order to show the right array to the user )
int **recursion(int **array,int x,int y,int n ,int m){
int i,j;
if((array[x][y]>=1)&&(array[x][y]<=8)){
array[x][y]=array[x][y]+10;
return array;
}
if(array[x][y]==0){
array[x][y]=array[x][y]+10;
for(i=-1;i<=1;i++){
for(j=-1;j<=1;j++){
if ((i==0)&&(j==0)){
continue;
}
if(x+i>n) x=x-1;
if(y+j>m) y=y-1;
if(x+i<0) x=x+1;
if(y+j<0) y=y+1;
return recursion(array,x+i,y+i,n,m);
}
}
}
}
//second try to make the recursive function...with the same characteristics.....
int **recursion2(int **array,int x,int y,int n ,int m){
if ((array[x][y]>=1)&&(array[x][y]<=8)){
array[x][y]=array[x][y]+10;
return array;
}
do{
if(x+1<=n){
array[x][y]=array[x][y]+10;
return recursion2(array,x+1,y,n,m);
}
if(y+1<=m){
array[x][y]=array[x][y]+10;
return recursion2(array,x,y+1,n,m);
}
if(x-1>0){
array[x][y]=array[x][y]+10;
return recursion2(array,x-1,y,n,m);
}
if(y-1>0){
array[x][y]=array[x][y]+10;
return recursion2(array,x,y-1,n,m);
}
if((x+1<=n)&&(y+1<=m)){
array[x][y]=array[x][y]+10;
return recursion2(array,x+1,y+1,n,m);
}
if((x-1>0)&&(y+1<=m)){
array[x][y]=array[x][y]+10;
return recursion2(array,x-1,y+1,n,m);
}
if((x-1>0)&&(y-1>0)){
array[x][y]=array[x][y]+10;
return recursion2(array,x-1,y-1,n,m);
}
if((x+1<=n)&&(y-1>0)){
array[x][y]=array[x][y]+10;
return recursion2(array,x+1,y-1,n,m);
}
} while((x+1<=n)||(y+1<=m)||(x-1>0)||(y-1>0)||((x+1<=n)&&(y+1<=m))||((x-1>0)&&(y-1>0))||((x-1>0)&&(y+1<=m))||((x+1<=n)&&(y-1>0)));
}