Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Chapter 8 Programming Project #9
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <time.h>
- #define SIZE 10
- #define PATH_SIZE ((int) (sizeof(brd_path)))
- #define ROW_SIZE ((int) (sizeof(board) / sizeof(board[0])))
- #define UP y - 1
- #define DOWN y + 1
- #define LEFT x - 1
- #define RIGHT x + 1
- int main(void)
- {
- char board[SIZE][SIZE] = {};
- char brd_path[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G',
- 'H', 'I', 'J', 'K', 'L', 'M', 'N',
- 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
- 'V', 'W', 'X', 'Y', 'Z'};
- // 0 = Up, 1 = Down, 2 = Left, 3 = Right
- bool path_dir_chk[4] = {false};
- bool blocked = false;
- unsigned short i, j, x = 0, y = 0;
- srand((unsigned) time(NULL));
- int test, dir = rand() % 4;
- for (x = 0; x < ROW_SIZE; x++) {
- for (y = 0; y < ROW_SIZE; y++)
- board[x][y] = '.';
- }
- while (blocked != true) {
- for (i = 0; i < 26;) {
- // Reset path_dir_chk values if empty
- for (j = 0; j < 4; j++) {
- if (board[x][UP] == '.')
- path_dir_chk[0] = false;
- if (board[x][DOWN] == '.')
- path_dir_chk[1] = false;
- if (board[LEFT][y] == '.')
- path_dir_chk[2] = false;
- if (board[RIGHT][y] == '.')
- path_dir_chk[3] = false;
- }
- // Check the direction and replace that char
- if (dir == 0) {
- if (board[x][UP] == '.') {
- board[x][UP] = brd_path[i];
- y = UP;
- printf("I is now: %d\n", ++i);
- }
- }
- if (dir == 1) {
- if (board[x][DOWN] == '.') {
- board[x][DOWN] = brd_path[i];
- y = DOWN;
- printf("I is now: %d\n", ++i);
- }
- }
- if (dir == 2) {
- if (board[LEFT][y] == '.') {
- board[LEFT][y] = brd_path[i];
- y = LEFT;
- printf("I is now: %d\n", ++i);
- }
- }
- if (dir == 3) {
- if (board[RIGHT][y] == '.') {
- board[RIGHT][y] = brd_path[i];
- y = RIGHT;
- printf("I is now: %d\n", ++i);
- }
- }
- // Set path_dir_chk values if not empty
- for (int j = 0; j < 4; j++) {
- if (board[x][UP] != '.')
- path_dir_chk[0] = true;
- if (board[x][DOWN] != '.')
- path_dir_chk[1] = true;
- if (board[LEFT][y] != '.')
- path_dir_chk[2] = true;
- if (board[RIGHT][y] != '.')
- path_dir_chk[3] = true;
- }
- // if all path's are true exit
- if (path_dir_chk[0] == true &&
- path_dir_chk[1] == true &&
- path_dir_chk[2] == true &&
- path_dir_chk[3] == true)
- blocked = true;
- // Reset the random direction
- dir = rand() % 4;
- }
- }
- for (x = 0; x < ROW_SIZE; x++) {
- for (y = 0; y < ROW_SIZE; y++)
- printf("%c ", board[x][y]);
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement