randomize chars in multi dimen array

musicmancanora4's Avatar
Go4Expert Member
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");
  }
  

}
coderzone's Avatar, Join Date: Jul 2004
Team Leader
In your other 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.
musicmancanora4's Avatar
Go4Expert Member
thnxs buddy got it workin
coderzone's Avatar, Join Date: Jul 2004
Team Leader
Quote:
Originally Posted by musicmancanora4
thnxs buddy got it workin
My pleasure.