Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. void genera(int **map,int size);
  6. int main()
  7. {
  8.   srand(time(NULL));
  9.   int size=rand() % 20 + 3;
  10.   int **map,i;
  11.  
  12.   map = calloc(size,sizeof(int*));
  13.   for(i=0;i<size;i++)
  14.   {
  15.     map[i]=calloc(size,sizeof(int));
  16.   }
  17.   genera(map,size);
  18.  
  19.   /*
  20.   for(int i=0;i<size;i++)
  21.   {
  22.     for(int j=0;j<size;j++)
  23.     {
  24.       printf("%d",*(*map+i*size+j));
  25.     }
  26.   }
  27.   */
  28. }
  29.  
  30. void genera(int **map,int size)
  31. {
  32.   int righe=size;
  33.   int colonne=size;
  34.   int pos=0;
  35.   int count=0;
  36.   int continua;
  37.   int pos_cur[20]={0};
  38.   int rand_continua=0;
  39.  
  40.   int x_pos_uscita= rand() %size + 1;
  41.   int y_pos_uscita= rand() %size + 1;
  42.  
  43. //this creates a matrix made of 0s and 1s.
  44.   for(int i=0;i<righe;i++)
  45.   {
  46.     for(int j=0;j<colonne;j++)
  47.     {
  48.       *(*map+i*colonne+j)= rand() % 2;
  49.     }
  50.   }
  51.  
  52.  
  53.   for(int i=1;i<righe;i++)
  54.   {
  55.     for(int j=0;j<colonne;j++)
  56.     {
  57.       if (*(*map+(i-1)*colonne+j)==0)
  58.       {
  59.         /*in this for loop, im checking if theres a 0 in the previous row of the
  60.         previous row of the matrix, if yes, save the position of the current column
  61.         inside a array called pos_cur, and keep a counter of the number of 0s found
  62.         inside a variable count.
  63.         example:
  64.         00114
  65.         23450
  66.         this stores the positions 0 and 1 inside pos_cur and sets count to 2*/
  67.  
  68.         pos_cur[pos]=j;
  69.         pos++;
  70.         count++;
  71.       }
  72.     }
  73.  
  74.     //this variable creates a random number between 0 and the number of 0s found previously
  75.     continua=rand() %count;
  76.  
  77.     for(int k=0;k<continua;k++)
  78.     {
  79.       /*this for loop creates a random number between 0s and the number of 0s found,
  80.       if it hasnt been used yet, it uses the position stored inside the array pos_cur
  81.       to change the number of the original matrix to 0;
  82.       example:
  83.       00104
  84.       23450  ->count=3, pos_cur={0,1,3}
  85.       continua=random number between 0 and 2, lets say 1;
  86.       for a "continua" number of times(1), finds a random number betwen 0 and 2 that
  87.       has not been used yet, lets say 2;
  88.       as pos_cur[2]=3, the column 3 (+1 because it starts from 0) at line i in the original matrix is changed to 0:
  89.       00104
  90.       234(0)0
  91.       */
  92.       rand_continua=rand() %count;
  93.       if(pos_cur[rand_continua]!=-1)
  94.       {
  95.         *(*map+i*colonne+pos_cur[rand_continua])= 0;
  96.         pos_cur[rand_continua]=-1;
  97.       }
  98.       else
  99.       {
  100.         k--;
  101.       }
  102.     }
  103.     count=0;
  104.   }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement