Advertisement
John_Conner

Random Path/Multidimensional Array

Mar 10th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.37 KB | None | 0 0
  1. // Chapter 8 Programming Project #9
  2.  
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7.  
  8. #define SIZE 10
  9. #define PATH_SIZE ((int) (sizeof(brd_path)))
  10. #define ROW_SIZE ((int) (sizeof(board) / sizeof(board[0])))
  11. #define UP y - 1
  12. #define DOWN y + 1
  13. #define LEFT x - 1
  14. #define RIGHT x + 1
  15.  
  16. int main(void)
  17. {
  18.     char board[SIZE][SIZE] = {};
  19.     char brd_path[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G',
  20.                        'H', 'I', 'J', 'K', 'L', 'M', 'N',
  21.                        'O', 'P', 'Q', 'R', 'S', 'T', 'U',
  22.                        'V', 'W', 'X', 'Y', 'Z'};
  23.     // 0 = Up, 1 = Down, 2 = Left, 3 = Right
  24.     bool path_dir_chk[4] = {false};
  25.     bool blocked = false;
  26.     unsigned short i, j, x = 0, y = 0;
  27.  
  28.     srand((unsigned) time(NULL));
  29.     int test, dir = rand() % 4;
  30.  
  31.     for (x = 0; x < ROW_SIZE; x++) {
  32.         for (y = 0; y < ROW_SIZE; y++)
  33.             board[x][y] = '.';
  34.     }
  35.  
  36.     while (blocked != true) {
  37.         for (i = 0; i < 26;) {
  38.             // Reset path_dir_chk values if empty
  39.             for (j = 0; j < 4; j++) {
  40.                 if (board[x][UP] == '.')
  41.                     path_dir_chk[0] = false;
  42.                 if (board[x][DOWN] == '.')
  43.                     path_dir_chk[1] = false;
  44.                 if (board[LEFT][y] == '.')
  45.                     path_dir_chk[2] = false;
  46.                 if (board[RIGHT][y] == '.')
  47.                     path_dir_chk[3] = false;
  48.             }
  49.             // Check the direction and replace that char
  50.             if (dir == 0) {
  51.                 if (board[x][UP] == '.') {
  52.                     board[x][UP] = brd_path[i];
  53.                     y = UP;
  54.                     printf("I is now: %d\n", ++i);
  55.                 }
  56.             }
  57.             if (dir == 1) {
  58.                 if (board[x][DOWN] == '.') {
  59.                     board[x][DOWN] = brd_path[i];
  60.                     y = DOWN;
  61.                     printf("I is now: %d\n", ++i);
  62.                 }
  63.             }
  64.             if (dir == 2) {
  65.                 if (board[LEFT][y] == '.') {
  66.                     board[LEFT][y] = brd_path[i];
  67.                     y = LEFT;
  68.                     printf("I is now: %d\n", ++i);
  69.                 }
  70.             }
  71.             if (dir == 3) {
  72.                 if (board[RIGHT][y] == '.') {
  73.                     board[RIGHT][y] = brd_path[i];
  74.                     y = RIGHT;
  75.                     printf("I is now: %d\n", ++i);
  76.                 }
  77.             }
  78.             // Set path_dir_chk values if not empty
  79.             for (int j = 0; j < 4; j++) {
  80.                 if (board[x][UP] != '.')
  81.                     path_dir_chk[0] = true;
  82.                 if (board[x][DOWN] != '.')
  83.                     path_dir_chk[1] = true;
  84.                 if (board[LEFT][y] != '.')
  85.                     path_dir_chk[2] = true;
  86.                 if (board[RIGHT][y] != '.')
  87.                     path_dir_chk[3] = true;
  88.         }
  89.         // if all path's are true exit
  90.         if (path_dir_chk[0] == true &&
  91.             path_dir_chk[1] == true &&
  92.             path_dir_chk[2] == true &&
  93.             path_dir_chk[3] == true)
  94.             blocked = true;
  95.         // Reset the random direction
  96.         dir = rand() % 4;
  97.         }
  98.     }
  99.  
  100.     for (x = 0; x < ROW_SIZE; x++) {
  101.         for (y = 0; y < ROW_SIZE; y++)
  102.             printf("%c ", board[x][y]);
  103.         printf("\n");
  104.     }
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement