Advertisement
Guest User

Untitled

a guest
Oct 7th, 2011
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5.  
  6. #define ARRAY_COLUMNS   10
  7. #define ARRAY_ROWS      10
  8. #define CHAR_ARRAY      ((int) (sizeof(alphabet) / sizeof(alphabet[0])))
  9.  
  10. int main(void)
  11. {
  12.     int walking_array[ARRAY_COLUMNS][ARRAY_ROWS], i, j, counter = 0,
  13.         counter2 = 0, new_direction;
  14.     const char alphabet[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  15.                             'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  16.                             'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y',
  17.                             'W', 'Z'};
  18.  
  19.     // initialize random number generator
  20.     srand( (unsigned) time(NULL) );
  21.    
  22.     // fill walking_array with dots
  23.     for (i = 0; i < ARRAY_COLUMNS; i++)
  24.     {
  25.         for (j = 0; j < ARRAY_ROWS; j++)
  26.         {
  27.             walking_array[i][j] = '.';
  28.         }
  29.     }
  30.    
  31.     while (counter < CHAR_ARRAY)
  32.     {
  33.         // to pick random move, take modulus of rand() and 4
  34.         new_direction = rand() % 4;
  35.        
  36.         if (new_direction == 0)
  37.         {
  38.             if (counter == 1)
  39.             {
  40.                 continue;
  41.             }
  42.             else if (walking_array[(counter - 1)][counter2] == '.')
  43.             {
  44.                 counter++;
  45.                 counter2--;
  46.                 walking_array[counter][counter2] = alphabet[counter];
  47.             }
  48.         }
  49.        
  50.         if (new_direction == 1)
  51.         {
  52.             if (counter > 8)
  53.             {
  54.                 continue;
  55.             }
  56.             else
  57.             {
  58.                 counter++;
  59.                 counter2++;
  60.                 walking_array[counter][counter2] = alphabet[counter];
  61.             }
  62.         }
  63.        
  64.         if (new_direction == 2)
  65.         {
  66.             if (counter2 == 0)
  67.             {
  68.                 continue;
  69.             }
  70.             else if (walking_array[counter][(counter2 + 1)] == '.')
  71.             {
  72.                 counter++;
  73.                 counter2--;
  74.                 walking_array[counter][counter2] = alphabet[counter];
  75.             }
  76.         }
  77.        
  78.         if (new_direction == 3)
  79.         {
  80.             if (counter2 > 8)
  81.             {
  82.                 continue;
  83.             }
  84.             if (walking_array[counter][(counter2 + 1)] == '.')
  85.             {
  86.                 counter++;
  87.                 counter2++;
  88.                 walking_array[counter][counter2] = alphabet[counter];
  89.             }
  90.         }
  91.     }
  92.    
  93.     for (i = 0; i < ARRAY_COLUMNS; i++)
  94.     {
  95.         for (j = 0; j < ARRAY_ROWS; j++)
  96.         {
  97.             printf(" %c", walking_array[i][j]);
  98.         }
  99.         printf("\n");
  100.     }
  101.    
  102.     return 0;
  103.  }
  104.  
  105.  
  106.  
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement