Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- void genera(int **map,int size);
- int main()
- {
- srand(time(NULL));
- int size=rand() % 20 + 3;
- int **map,i;
- map = calloc(size,sizeof(int*));
- for(i=0;i<size;i++)
- {
- map[i]=calloc(size,sizeof(int));
- }
- genera(map,size);
- /*
- for(int i=0;i<size;i++)
- {
- for(int j=0;j<size;j++)
- {
- printf("%d",*(*map+i*size+j));
- }
- }
- */
- }
- void genera(int **map,int size)
- {
- int righe=size;
- int colonne=size;
- int pos=0;
- int count=0;
- int continua;
- int pos_cur[20]={0};
- int rand_continua=0;
- int x_pos_uscita= rand() %size + 1;
- int y_pos_uscita= rand() %size + 1;
- //this creates a matrix made of 0s and 1s.
- for(int i=0;i<righe;i++)
- {
- for(int j=0;j<colonne;j++)
- {
- *(*map+i*colonne+j)= rand() % 2;
- }
- }
- for(int i=1;i<righe;i++)
- {
- for(int j=0;j<colonne;j++)
- {
- if (*(*map+(i-1)*colonne+j)==0)
- {
- /*in this for loop, im checking if theres a 0 in the previous row of the
- previous row of the matrix, if yes, save the position of the current column
- inside a array called pos_cur, and keep a counter of the number of 0s found
- inside a variable count.
- example:
- 00114
- 23450
- this stores the positions 0 and 1 inside pos_cur and sets count to 2*/
- pos_cur[pos]=j;
- pos++;
- count++;
- }
- }
- //this variable creates a random number between 0 and the number of 0s found previously
- continua=rand() %count;
- for(int k=0;k<continua;k++)
- {
- /*this for loop creates a random number between 0s and the number of 0s found,
- if it hasnt been used yet, it uses the position stored inside the array pos_cur
- to change the number of the original matrix to 0;
- example:
- 00104
- 23450 ->count=3, pos_cur={0,1,3}
- continua=random number between 0 and 2, lets say 1;
- for a "continua" number of times(1), finds a random number betwen 0 and 2 that
- has not been used yet, lets say 2;
- 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:
- 00104
- 234(0)0
- */
- rand_continua=rand() %count;
- if(pos_cur[rand_continua]!=-1)
- {
- *(*map+i*colonne+pos_cur[rand_continua])= 0;
- pos_cur[rand_continua]=-1;
- }
- else
- {
- k--;
- }
- }
- count=0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement