Advertisement
Roxar9-0

randomwalk

Nov 23rd, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.66 KB | None | 0 0
  1. /*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*/
  2.  
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<time.h>
  6. #define n 10
  7.  
  8. void randomwalk(char[][n]);
  9. void stampa(char[][n]);
  10. void riempi(char[][n]);
  11.  
  12. int main()
  13. {
  14. srand((unsigned int) time(0));
  15. char matrice[n][n];
  16. riempi(matrice);
  17. randomwalk(matrice);
  18. stampa(matrice);
  19. }
  20.  
  21. void randomwalk(char matrice[][n])
  22. {
  23. int direction1=0;
  24. int tentativi=0;
  25. int i=rand()%n, j=rand()%n, k=0;
  26. char alfabeto[26]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  27. matrice[i][j]=alfabeto[k];
  28. k++;
  29. direction1=rand()%4;
  30. while(1)
  31. {
  32.  
  33.  switch(direction1)
  34.  {
  35.  case 0: i++;//vai sotto
  36.          if(i>=n || matrice[i][j]!='.')
  37.            {tentativi++;
  38.             i--;
  39.             direction1++;
  40.         break;
  41.            }
  42.     else {
  43.              matrice[i][j]=alfabeto[k];
  44.              k++;
  45.              tentativi=0;
  46.          direction1=rand()%4;
  47.              break;
  48.              }
  49.            
  50.  case 1: i--;//vai sopra
  51.          if(i<0 || matrice[i][j]!='.')
  52.           {
  53.            tentativi++;
  54.            i++;
  55.        direction1++;
  56.            break;
  57.           }
  58.         else {
  59.              matrice[i][j]=alfabeto[k];
  60.              k++; tentativi=0;
  61.              direction1=rand()%4;
  62.              break;
  63.              }
  64.  case 2: j++;//vai a destra
  65.          if(j>=n || matrice[i][j]!='.')
  66.            {
  67.             tentativi++;
  68.             j--;
  69.        direction1++;
  70.            break;
  71.            }
  72.            else {
  73.                  matrice[i][j]=alfabeto[k];
  74.                  k++;
  75.                  tentativi=0;
  76.                  direction1=rand()%4;
  77.                  break;
  78.                 }
  79.  case 3: j--;//vai a sinistra
  80.           if(j<0 || matrice[i][j]!='.')
  81.             {
  82.              tentativi++;
  83.              j++;
  84.              direction1=0;
  85.          break;
  86.             }
  87.           else {
  88.                matrice[i][j]=alfabeto[k];
  89.                k++;
  90.                tentativi=0;
  91.                direction1=rand()%4;
  92.                break;
  93.                 }
  94.  }
  95. if(k==26)
  96.    break;
  97. if(tentativi==4)
  98.    break;
  99. }
  100. }
  101.  
  102. void stampa(char matrice[][n])
  103. {
  104.  int i, j;
  105.  for(i=0;i<n;i++)
  106.      {
  107.      for(j=0;j<n;j++)
  108.         {
  109.          printf("%c\t", matrice[i][j]);
  110.         }
  111.      printf("\n\n");
  112.      }
  113. }
  114.  
  115. void riempi(char matrice[][n])
  116. {
  117. int i, j;
  118. for(i=0;i<n;i++)
  119.    for(j=0;j<n;j++)
  120.       matrice[i][j]='.';
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement