Thread: simplex minesweeper need help

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    simplex minesweeper need help

    i need a program which will generate a board(15x30) with randomly placed mines, and it will print the result on screen.

    Example run:
    >Enter N (number of mines): 20
    >Here’s the random minefield:
    > o o o o o o o o o o o o o o o o * * o o o o o o o o o o o o
    > o o o o o o o o o o o * o o o o o o o o o o o o * o o o o o
    > o o o * o o o o o o o o o o o o o o o o o o o o o o o o o o
    > o o o * o o o o o o o o o o o o o o o o o o o o o o o o o o
    > o o o o o o o o o o o o o o o * o o o o o o o o o o o o o o
    > o o o o o o o o o o o o o o o o o o o o o o o o o o o * o o
    > o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
    > o o o o o o o o o o o * o o o o o o o o o o o o o o o o o o
    > o o o o o o o o o o o * o o o o o o o o o o o o o o o o o o
    > o o o o o o o o o o o o o o o o o o o o o o o o o o o * o o
    > o o o o o o o o o o o o o o o o o o * o o o o o o o o o o o
    > o o o o o o * o o o o o o o o o o o o * o o o o o o o o o o
    > o o o o o o o o o o o o o * o o o o o o o o o o * o o o o o
    > o o o o o o o o o o o o o o o o * o o o o o o o * o o o o o
    > o o o o o o o o o o o o o o o o o o o o o * o o o * o o o o
    -------------------------------------------------------------------------------
    here is my code but it isn't perfect.it prints more than N value or less. can you help me?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
    int sayi,mayin,i,x,y,a,b;
    int tarla[15][30];
    
    printf ("Enter N (number of mines): ");
    scanf ("%d", &mayin);
    
    
    
    	tarla[a][b]=0;
    for (sayi=0;sayi<mayin;sayi++)
    {
    srand(time(0));
    do
    {
    x=1+rand()%16;
    y=1+rand()%31;
    }
    while(tarla[x][y]==i);
    tarla[x][y]=i;
    }
    for (x=0;x<15;x++)
    {
    for (y=0;y<30;y++)
    {
    if (tarla[x][y]==i) printf ("X");
    else printf ("o");
    printf("");
    }
    printf("\n");
    }
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First, indent, so your code is readable.
    Code:
    #include <stdlib.h>
    
    int main(void)
    {
        int sayi, mayin, i, x, y, a, b;
        int tarla[15][30];
    
        printf("Enter N (number of mines): ");
        scanf("%d", &mayin);
    
    
    
        tarla[a][b] = 0;
        for (sayi = 0; sayi < mayin; sayi++) {
            srand(time(0));
            do {
                x = 1 + rand() % 16;
                y = 1 + rand() % 31;
            }
            while (tarla[x][y] == i);
            tarla[x][y] = i;
        }
        for (x = 0; x < 15; x++) {
            for (y = 0; y < 30; y++) {
                if (tarla[x][y] == i)
                    printf("X");
                else
                    printf("o");
                printf("");
            }
            printf("\n");
        }
        return 0;
    }
    See SourceForge.net: Indentation - cpwiki


    Second, enable as many warnings as you can for your compiler,
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function ‘main’:
    foo.c:8: warning: implicit declaration of function ‘printf’
    foo.c:8: warning: incompatible implicit declaration of built-in function ‘printf’
    foo.c:9: warning: implicit declaration of function ‘scanf’
    foo.c:9: warning: incompatible implicit declaration of built-in function ‘scanf’
    foo.c:16: warning: implicit declaration of function ‘time’
    foo.c:31: warning: zero-length gnu_printf format string
    foo.c:13: warning: ‘a’ is used uninitialized in this function
    foo.c:13: warning: ‘b’ is used uninitialized in this function
    foo.c:22: warning: ‘i’ may be used uninitialized in this function
    All those "uninitialised" variables are bugs.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
       int sayi,mayin,i,x,y;
                    //int tarla[15][30];
       char tarla[15][30]; //ok
    
       printf ("Enter N (number of mines): ");
       scanf ("%d", &mayin);
    
       srand(time(0));  //ok
    
    
       for (sayi=0;sayi<mayin;sayi++)
       {
                               //srand(time(0));
                                 //do
           x=1+rand()%16;
           y=1+rand()%31;
           tarla[x][y]='*';
                               //while(tarla[x][y]==i);
                                //tarla[x][y]=i;
       }
       for (x=0;x<15;x++)
       {
          for (y=0;y<30;y++)
          {
             if(tarla[x][y]!='*')
                tarla[x][y]= 'O');
             printf("%c",tarla[x][y];
          }
          printf("\n");
       }
       return 0;
    }
    I'd use a char array for this, and logic like the above. Make sense?

    If you'll indent your code so subordinate lines of code are indented about 2-5 spaces, for each level of subordination, your code will be MUCH easier to study.
    Last edited by Adak; 11-30-2010 at 03:41 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Quote Originally Posted by Adak View Post
    I'd use a char array for this, and logic like the above. Make sense?

    If you'll indent your code so subordinate lines of code are indented about 2-5 spaces, for each level of subordination, your code will be MUCH easier to study.
    normally i study that way,i just erased them to not cover much place

    i rewrite code like this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
       int sayi,mayin,i,x,y;
                    
       char tarla[15][30]; 
    
       printf ("Enter N (number of mines): ");
       scanf ("%d", &mayin);
    
    
    
       for (sayi=0;sayi<mayin;sayi++)
       {
               srand(time(0))  ;             
        do{
           x=1+rand()%16;
           y=1+rand()%31;
           tarla[x][y]='*';
    }while(tarla[x][y]==i);
         
    
     tarla[x][y]=i;
    
       }
       for (x=0;x<15;x++)
       {
          for (y=0;y<30;y++)
          {
             if(tarla[x][y]!='*')
             {  
             tarla[x][y]='O';
             printf("%c",tarla[x][y]);
          }
      }
          printf("\n");
       }
       return 0;
    }
    but it didn't placed any mines .

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Look at this, and make your code very very similar:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
       int sayi,mayin,i,x,y;
       char tarla[15][30]; //ok
    
       printf ("Enter N (number of mines): ");
       scanf ("%d", &mayin);
       (void) getchar();   //removes left over newline from keyboard buffer
       srand(time(0));  //ok
    
       for(x=0;x<15;x++) {
         for(y=0;y<30;y++) {
           tarla[x][y]='O';
         }
       }
    
       for (sayi=0;sayi<mayin;sayi++)
       {
           x=rand()%15;
           y=rand()%30;
           tarla[x][y]='*';
       }
       for (x=0;x<15;x++)
       {
          for (y=0;y<30;y++)
          {
             printf("%c",tarla[x][y]);
          }
          printf("\n");
       }
       
       (void) getchar(); //just holds the console window open - you may not need it
       return 0;
    }
    This is slightly simpler and runs correctly *except* you can wind up with cases where the random x and random y (because their range of numbers is so small), repeat to a square that already has a mine.

    That will leave you one mine short, with every "collision" of mines.

    How might you solve that problem?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Quote Originally Posted by Adak View Post

    This is slightly simpler and runs correctly *except* you can wind up with cases where the random x and random y (because their range of numbers is so small), repeat to a square that already has a mine.

    That will leave you one mine short, with every "collision" of mines.

    How might you solve that problem?
    thank you very much it works fine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's the best way to draw a minesweeper field
    By Nazgulled in forum C Programming
    Replies: 1
    Last Post: 05-08-2007, 01:24 PM
  2. minesweeper
    By sameintheend01 in forum C++ Programming
    Replies: 1
    Last Post: 02-18-2003, 09:50 AM
  3. Recreating Minesweeper.
    By j0hnb in forum C Programming
    Replies: 15
    Last Post: 02-18-2003, 07:12 AM
  4. Minesweeper errors...
    By j0hnb in forum C Programming
    Replies: 0
    Last Post: 02-10-2003, 01:36 PM
  5. minesweeper problem
    By adamrobbie in forum C Programming
    Replies: 2
    Last Post: 10-14-2002, 08:03 PM