Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 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.   printf("size is %d\n",size);
  11.   int **map,i;
  12.  
  13.   map = calloc(size,sizeof(int*));
  14.   for(i=0;i<size;i++)
  15.   {
  16.     map[i]=calloc(size,sizeof(int));
  17.   }
  18.   genera(map,size);
  19.  
  20.   /*
  21.   for(int i=0;i<size;i++)
  22.   {
  23.     for(int j=0;j<size;j++)
  24.     {
  25.       printf("%d",*(*map+i*size+j));
  26.     }
  27.   }
  28.   */
  29. }
  30.  
  31. void genera(int **map,int size)
  32. {
  33.   int righe=size;
  34.   int colonne=size;
  35.   int pos=0;
  36.   int count=0;
  37.   int continua;
  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.       printf("%d", *(*map+i*colonne+j));
  50.     }
  51.     printf("\n");
  52.   }
  53.  
  54.  
  55.   for(int i=1;i<righe;i++)
  56.   {
  57.     int pos_cur[20]={0};
  58.     for(int j=0;j<colonne;j++)
  59.     {
  60.       if (*(*map+(i-1)*colonne+j)==0)
  61.       {
  62.         /*in this for loop, im checking if theres a 0 in the previous row of the
  63.         previous row of the matrix, if yes, save the position of the current column
  64.         inside a array called pos_cur, and keep a counter of the number of 0s found
  65.         inside a variable count.
  66.         example:
  67.         00114
  68.         23450
  69.         this stores the positions 0 and 1 inside pos_cur and sets count to 2*/
  70.  
  71.         pos_cur[pos]=j;
  72.         printf("Theres a 0 at column %d\n",j);
  73.  
  74.         pos++;
  75.         count++;
  76.       }
  77.     }
  78.  
  79.     //this variable creates a random number between 0 and the number of 0s found previously
  80.     printf("count è %d\n", count);
  81.     continua=rand() %count;
  82.     printf("continua è %d\n", continua);
  83.     printf("L'array di pos:\n");
  84.     for(int p=0;p<count;p++)
  85.     {
  86.       printf("%d  ",pos_cur[p]);
  87.     }
  88.  
  89.     while(continua>0)
  90.     {
  91.       /*this for loop creates a random number between 0s and the number of 0s found,
  92.       if it hasnt been used yet, it uses the position stored inside the array pos_cur
  93.       to change the number of the original matrix to 0;
  94.       example:
  95.       00104
  96.       23450  ->count=3, pos_cur={0,1,3}
  97.       continua=random number between 0 and 2, lets say 1;
  98.       for a "continua" number of times(1), finds a random number betwen 0 and 2 that
  99.       has not been used yet, lets say 2;
  100.       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:
  101.       00104
  102.       234(0)0
  103.       */
  104.       rand_continua=rand() %count;
  105.       if(pos_cur[rand_continua]!=-1)
  106.       {
  107.         printf("rand_continua è %d\n", rand_continua);
  108.         *(*map+i*colonne+pos_cur[rand_continua])= 0;
  109.         pos_cur[rand_continua]=-1;
  110.  
  111.         printf("L'array di pos:\n");
  112.         for(int p=0;p<count;p++)
  113.         {
  114.           printf("%d  ",pos_cur[p]);
  115.         }
  116.         printf("\n");
  117.         continua--;
  118.       }
  119.     }
  120.     count=0;
  121.   }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement