Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*data una matrice nxn, il programma si sposta in maniera casuale all'interno della matrice verso l'alto, il basso, verso destra e sinistra. Scrive al suo passaggio le lettere dell'alfabeto. Termina quando ha terminato le lettere da scrivere opuure quando non può più spostarsi all'interno della matrice. La posizione iniziale viene stabilita in maniera random*/
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- #define n 10
- void randomwalk(char[][n]);
- void stampa(char[][n]);
- void riempi(char[][n]);
- int main()
- {
- srand((unsigned int) time(0));
- char matrice[n][n];
- riempi(matrice);
- randomwalk(matrice);
- stampa(matrice);
- }
- void randomwalk(char matrice[][n])
- {
- int direction1=0;
- int tentativi=0;
- int i=rand()%n, j=rand()%n, k=0;
- char alfabeto[26]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- matrice[i][j]=alfabeto[k];
- k++;
- direction1=rand()%4;
- while(1)
- {
- switch(direction1)
- {
- case 0: i++;//vai sotto
- if(i>=n || matrice[i][j]!='.')
- {tentativi++;
- i--;
- direction1++;
- break;
- }
- else {
- matrice[i][j]=alfabeto[k];
- k++;
- tentativi=0;
- direction1=rand()%4;
- break;
- }
- case 1: i--;//vai sopra
- if(i<0 || matrice[i][j]!='.')
- {
- tentativi++;
- i++;
- direction1++;
- break;
- }
- else {
- matrice[i][j]=alfabeto[k];
- k++; tentativi=0;
- direction1=rand()%4;
- break;
- }
- case 2: j++;//vai a destra
- if(j>=n || matrice[i][j]!='.')
- {
- tentativi++;
- j--;
- direction1++;
- break;
- }
- else {
- matrice[i][j]=alfabeto[k];
- k++;
- tentativi=0;
- direction1=rand()%4;
- break;
- }
- case 3: j--;//vai a sinistra
- if(j<0 || matrice[i][j]!='.')
- {
- tentativi++;
- j++;
- direction1=0;
- break;
- }
- else {
- matrice[i][j]=alfabeto[k];
- k++;
- tentativi=0;
- direction1=rand()%4;
- break;
- }
- }
- if(k==26)
- break;
- if(tentativi==4)
- break;
- }
- }
- void stampa(char matrice[][n])
- {
- int i, j;
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- {
- printf("%c\t", matrice[i][j]);
- }
- printf("\n\n");
- }
- }
- void riempi(char matrice[][n])
- {
- int i, j;
- for(i=0;i<n;i++)
- for(j=0;j<n;j++)
- matrice[i][j]='.';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement