randomize chars in multi dimen array

Discussion in 'C' started by musicmancanora4, Mar 29, 2006.

  1. musicmancanora4

    musicmancanora4 New Member

    Joined:
    Mar 9, 2006
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    Hi guys im making a minesweeper game. "Result" variable is equal to the NoMines, basically
    i need to randomize the NoMines in the mineField multi dimensional array and then print the
    'M' in the random positions in the array some in columns some in rows but randomly. Iv done something like this but im not sure how correct it is?


    size is the whole size of the mineField if user types 3 the grid is the square root of it
    printf grid mine size prints '?' depending on how large the size of the grid is
    ? ? ?
    ? ? ?

    this is how i need to random the character in the array
    ? ? M
    ? M ?

    i need to be able to print them in squares not sure how too?

    Code:
    
    define MINE 'M'
    
    void printGridMineSize(char displayGrid[MAX_GRID][MAX_GRID],unsigned size,int getMineSize)
    {
        
        /* declaration of for loop variables*/
        int i=0;
        int k=0;
        int j=0;
        int a=0;
        int b=0;
      
        printf("************************** Mine Sweeper**************************\n");
        printf("\n");
       
        /* outter loop for array*/
        
        for(i=0; i<size; i++)
        {
             /* loop to keep printing inside seperator lines*/
    	 for(b=2; b<=size; b++)
    	 {
    	   printf("+---");
    	   
    	 }
    	 printf("+---\n");
    	 
    	 printf("|");
    	 
    	 /* printing columns and also the array*/
    	 for(a=0; a<size; a++)
    	 {
    	        
    	        printf(" %c |",displayGrid[i][k]); 
    		
    	        
    	 }
    	 printf("\n");
    	 
        } 
    	 /* print the outter seperators*/
    	 for(j=0;j<size; j++)
    	 {
    	 
    	        printf("+---"); 
    	 
    	 
    	 
    	 }
    	 printf("\n");
    	
    	
    	
        
        
    }
    
    
    
    
    void placeMines(char minefield[MAX_GRID][MAX_GRID], unsigned size)
    {
    
      double result;
      double sizetotal;
      double roundUp;
      int seed;
      int range;
      int NoMines;
      int TotalMine;
      int i=0;
      int j=0;
      char mineChar = MINE;
      
      rand()%range;
      /* square root the grid number*/
      
      sizetotal = size * size; 
      
      /* result is the sizetotal * 0.6*/
      result = (float)(float)sizetotal * MINE_DENSITY;
      
      /*rouding the number up 5.7 up to 6*/
      roundUp = ceil(result);
      
      /* cast it to an int to get no decimal places*/
      printf("%d",(int) roundUp);
      
      roundUp = NoMines;
      
      
      /* for the size thats entered randomize the mines in the array and print them in the squares*/
      for(i=1; i<size; i++)
      {
         printf("%c\n",mineChar);
         minefield[i][j]=rand();
         
         
         printf("\n");
      }
      
    
    }
    
    
    
    
    
     
  2. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    In your [thread=665]other thread[/thread] I replied how to print the output. Now you can use that to randomize the Mines and no mines. Just dont print them but put them in size into size array. then display the array at the end. If you are unable to get it drop here a line and will let you have that.
     
  3. musicmancanora4

    musicmancanora4 New Member

    Joined:
    Mar 9, 2006
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    thnxs buddy got it workin
     
  4. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    My pleasure.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice