Thread: Minesweeper

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

    Arrow Minesweeper

    Greetings, i've been assigned to write code for Minesweeper in C language.
    The thing is that i have to use Recursion on the function (int anoigma).I've got
    no idea how to use recursion >_< . Anyway here is the code it's not completely done yet(it can compile...) but the int anoigma is messed up. If anyone could help me with the Recursion part (int anoigma) somehow it would be really really helpfull!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void tamplo(int n, int m, int **pa);
    int anoigma(int a, int b, int **pa);
    
    int main(int argc, char *argv[])
    {
       int i,j,n,m,o,p,a,b,r1,r2,k;
       int **pa=NULL;
       printf("Dwste tis diastaseis tou paixnidiou n x m\n");
       printf("Prosoxh, to tamplo mporei na exei megethos to poly 24x30\n");
       scanf("%d%d", &n,&m);
       printf("Epilxte to epipedo duskolias tou paixnidiou\n");
       printf("Pathste 1 gia eukolo, 2 gia metro, 3 gia duskolo kai 4 gia akatorthoto\n");
       scanf("%d", &o);
       if(o==1)
       {
          p=m*n/8;
       }
       if(o==2)
       {
          p=m*n/7;
       }
       if(o==3)
       {
          p=m*n/6;
       }
       if(o==4)
       {
          p=m*n/5;
       }    
       pa=(int**)calloc(n, sizeof(int*)); //desmeush mnhmhs gia dimiourgia 2d pinaka
       for(i=0;i<n;i++)
       {
             pa[i]=(int*)calloc(m, sizeof(int));
       }
       tamplo(n,m,pa);
       printf("dwste tis syntetagmenes tou tetragwnou pou thelete na anoixete\n");
       scanf("%d%d", &a,&B);
       a=a-1;
       b=b-1;
       pa[a][b]=9;//9 antiprosopeyei ta anoixta tetragvna poy den periexoyn kati
       srand(time(NULL));
       for(i=0;i<p;i++)
       {
           do
           {                     
               r1=rand()%n;
               r2=rand()%m;
           }while(r1==a && r2==B); //wste na mhn yparxei narkh sto tetragwnw pou epelekse
           pa[r1][r2]=10;//to 10 antiprosopevei tis narkes
       }
       k=anoigma(a,b,pa);
       printf("%s%d\n", "ayto einai to k:", k);
       tamplo(n,m,pa);    
       for(i=0;i<n;i++)
       {
           free(pa[i]);
       }
       free(pa);
       system("PAUSE");	
       return 0;
    }
    
    int anoigma(int a, int b, int **pa)
    {
        int i,j,k=0,narkes=0;
        for(i=a-1;i<a+2;i++)
        {
             for(j=b-1;j<b+2;j++)
             {
                   if(pa[i][j]==1)
                   {
                        narkes=narkes+1;
                   }
             }
        }
        if(narkes==0)
        {
           pa[a][b]=9;
        }
        else
        {
            pa[a][b]=narkes;
        }
        k=k+1;
        return k;
    }  
        
    void tamplo(int n, int m, int **pa)
    {
       int i,j;
       if(m<10)
       {
            printf("   ");
            for(j=0;j<m;j++)
            {
                printf("%s%d", " ", j+1);
            }
            printf("\n");
            printf("-");
            for(j=0;j<m+1;j++)
            {
                printf("--");
            }
            printf("\n");
       }
       else
       {
           printf("   ");
           for(j=0;j<9;j++)
           {
                 printf("%s%d", "  ", j+1);
           }
           for(j=0;j<m-9;j++)
           {
                 printf("%s%d", " ", j+10);
           }
           printf("\n");
           printf("---");
           for(j=0;j<m;j++)
           {
                printf("---");
           }
           printf("\n");
       }       
       for(i=0;i<n;i++)
       {
             if(i<9)
             {
                  printf("%s%d%s", " ", i+1, "|"); 
             }
             else
             {
                 printf("%d%s", i+1, "|");
             }
             for(j=0;j<m;j++) 
             {
                  if(pa[i][j]==0)
                  {
                        if(m<10)
                        {
                            printf(" #");
                        }
                        else
                        {
                            printf("  #");
                        }
                  }           
                  if(pa[i][j]==9)
                  {
                        if(m<10)
                        {
                             printf(" .");
                        }
                        else
                        {
                            printf("  .");
                        }
                  }
                  if(pa[i][j]!=0 && pa[i][j]!=9)
                  {
                        if(m<10)
                        {
                             printf("%s%d", " ", pa[i][j]);
                        }
                        else
                        {
                             printf("%s%d", "  ", pa[i][j]);
                        }
                  }
                   
             }
             printf("\n");
       }
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Did you try looking around the forum before posting?
    There are at least 2 other current threads on this topic.
    It seems likely reading them will answer at least half your questions.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    *shrug"
    So much for suggesting readable names
    Problem with rand() (trying to create minesweeper)

    #include <std_doormat.h>
    void walk_all_over_me(void);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minesweeper
    By sameintheend01 in forum C++ Programming
    Replies: 1
    Last Post: 02-18-2003, 09:50 AM
  2. Minesweeper
    By PJYelton in forum Game Programming
    Replies: 2
    Last Post: 12-22-2002, 10:58 AM
  3. minesweeper...
    By jk81 in forum C Programming
    Replies: 1
    Last Post: 10-24-2002, 05:23 PM
  4. minesweeper problem
    By adamrobbie in forum C Programming
    Replies: 2
    Last Post: 10-14-2002, 08:03 PM
  5. minesweeper
    By denny in forum C Programming
    Replies: 5
    Last Post: 01-27-2002, 07:48 AM