Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <time.h>
- #include <stdlib.h>
- #define ARRAY_COLUMNS 10
- #define ARRAY_ROWS 10
- #define CHAR_ARRAY ((int) (sizeof(alphabet) / sizeof(alphabet[0])))
- int main(void)
- {
- int walking_array[ARRAY_COLUMNS][ARRAY_ROWS], i, j, counter = 0,
- counter2 = 0, new_direction;
- const char alphabet[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
- 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
- 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y',
- 'W', 'Z'};
- // initialize random number generator
- srand( (unsigned) time(NULL) );
- // fill walking_array with dots
- for (i = 0; i < ARRAY_COLUMNS; i++)
- {
- for (j = 0; j < ARRAY_ROWS; j++)
- {
- walking_array[i][j] = '.';
- }
- }
- while (counter < CHAR_ARRAY)
- {
- // to pick random move, take modulus of rand() and 4
- new_direction = rand() % 4;
- if (new_direction == 0)
- {
- if (counter == 1)
- {
- continue;
- }
- else if (walking_array[(counter - 1)][counter2] == '.')
- {
- counter++;
- counter2--;
- walking_array[counter][counter2] = alphabet[counter];
- }
- }
- if (new_direction == 1)
- {
- if (counter > 8)
- {
- continue;
- }
- else
- {
- counter++;
- counter2++;
- walking_array[counter][counter2] = alphabet[counter];
- }
- }
- if (new_direction == 2)
- {
- if (counter2 == 0)
- {
- continue;
- }
- else if (walking_array[counter][(counter2 + 1)] == '.')
- {
- counter++;
- counter2--;
- walking_array[counter][counter2] = alphabet[counter];
- }
- }
- if (new_direction == 3)
- {
- if (counter2 > 8)
- {
- continue;
- }
- if (walking_array[counter][(counter2 + 1)] == '.')
- {
- counter++;
- counter2++;
- walking_array[counter][counter2] = alphabet[counter];
- }
- }
- }
- for (i = 0; i < ARRAY_COLUMNS; i++)
- {
- for (j = 0; j < ARRAY_ROWS; j++)
- {
- printf(" %c", walking_array[i][j]);
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement