Help needed with minesweeper recursion!

This is a discussion on Help needed with minesweeper recursion! within the C Programming forums, part of the General Programming Boards category; Hello to everybody. I am new to this forum and i think i ll like it very much. I study ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    7

    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)));
                        
                     
    }
    Last edited by hellraiser; 04-20-2011 at 05:19 AM. Reason: put just the code of the function....

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Thumbs up: CODE tags
    Thumbs down: inconsistent indentation and too much code for people to read with no idea where you are having trouble.
    Can you narrow it down?

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    Quote Originally Posted by mike65535 View Post
    Thumbs up: CODE tags
    Thumbs down: inconsistent indentation and too much code for people to read with no idea where you are having trouble.
    Can you narrow it down?


    Hello my friend,
    and realy thank you for your comment...
    now...as i say in the code comments at the start of the code the problem is on how to make a recursive function that does what i asked for...my functions recursion and recursion2 was the 2 tries i made but nothing came up...so the problem is the function at the top of the code....

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    Oh and one last thing i forgot to mention...

    for the ones that may say that i just want to cheat etc etc....

    i made this thread asking for help after reading the thread about homework....and the only reason i did made this thread is because i want to understand how recursion works....if i just wanted to complete my code...i could just make a function with 2 for loops that checks the whole array and opens just the nearby cells ofthe cell chosen...

    Thanks anyway

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    help anyone???

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    I guess you are unwilling to heed my advice:
    too much code for people to read
    You can learn how recursion works without ~500 lines of code.

    As I suggested yesterday - provide a snippet of code that demonstrates the issue and you'll get all kinds of help.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    Quote Originally Posted by mike65535 View Post
    I guess you are unwilling to heed my advice:

    You can learn how recursion works without ~500 lines of code.

    As I suggested yesterday - provide a snippet of code that demonstrates the issue and you'll get all kinds of help.


    OK then ill delete the whole code thing and just leave the recursive function....the only reason i left the whole code is for someone to understand what i want because if i just give you the code that i tried to make twice for the recursive fuction you ll probably and every body here will just try to fix it.....the problem is that it cannot be fixed....i need a whole new recursive function....and the problem still is that i ve found a lot of algorithms over the internet about recursivly clear adjacent cells to an emty cell but none of them helped cause it could not be transposed to fit in my case.....

    so i ll do as you said but i think this way the problem will grow larger...

    i am fixiing it right now....

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    every body here will just try to fix it.....the problem is that it cannot be fixed....i need a whole new recursive function
    huh?

    <crickets...>

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    in simple words.....my code for the recursive function i need to build is just messed up and wont work.....so i ll need someone to tell me what is the right code and why.......so if i dont give him the whole code i wrote till now how is he going to be able to built what i need and help me without knowing the rest of the code????


    thats all i meant....

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    You need to learn how to isolate the problem. Not just to satisfy us here on the boards, but to allow you to progress in the face of the unknown. Without the ability to narrow down your focus, you can never move forward when presented with errors.

    Write a little code, test it, write more code. The more fluent you get with a programming language, and programming in general, the more code you can confidently write before you test.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    Quote Originally Posted by mike65535 View Post
    You need to learn how to isolate the problem. Not just to satisfy us here on the boards, but to allow you to progress in the face of the unknown. Without the ability to narrow down your focus, you can never move forward when presented with errors.

    Write a little code, test it, write more code. The more fluent you get with a programming language, and programming in general, the more code you can confidently write before you test.
    My dear friend mike.

    Really thank you for the advice...
    but just to inform you...in order to get to that huge piece of code i first made smaller pieces of each function and tested them and after that i built it all together...

    in conclusion...
    really thanks for all your advices and everything.But the problem still remains...and believe me i tried so hard even to build it alone....but nothing came up....

    WHAT CAN I DO? I AM DESPERATE :P::P:P:P

  12. #12
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    There's a nice discussion of recursion in the minesweeper game at http://www.cs.ucf.edu/~reinhard/clas...ecursion01.pdf. Perhaps it would be of help to you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion help needed
    By lewiso in forum C Programming
    Replies: 6
    Last Post: 03-02-2008, 11:18 AM
  2. Minesweeper Recursion
    By sql.scripter in forum C Programming
    Replies: 2
    Last Post: 03-21-2006, 07:02 AM
  3. Minesweeper
    By PJYelton in forum Game Programming
    Replies: 2
    Last Post: 12-22-2002, 09:58 AM
  4. minesweeper...
    By jk81 in forum C Programming
    Replies: 1
    Last Post: 10-24-2002, 05:23 PM
  5. stack and recursion help needed!
    By LouB in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2002, 02:19 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21