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 25
- #define ROW_SIZE ((int) (sizeof(board) / sizeof(board[0])))
- int main(void)
- {
- char board[SIZE][SIZE] = {};
- // 0 = Up, 1 = Down, 2 = Left, 3 = Right
- unsigned short i = 0, x, y;
- // Generate a random number
- srand((unsigned) time(NULL));
- int dir = rand() % 4;
- // Set all positions of board to '.'
- for (x = 0; x < ROW_SIZE; x++) {
- for (y = 0; y < ROW_SIZE; y++)
- board[x][y] = '.';
- }
- x = 0;
- y = 0;
- board[0][0] = 'A';
- // Generate the path
- while (i != PATH_SIZE) {
- for (i = 0; i < PATH_SIZE;) {
- // Check the direction and replace that char
- switch (dir) {
- case 0: if ((y - 1) >= 0 &&
- board[x][y - 1] == '.') {
- board[x][--y] = i + 'B';
- ++i;
- } break;
- case 1: if ((y + 1) >= 0 &&
- board[x][y + 1] == '.') {
- board[x][++y] = i + 'B';
- ++i;
- } break;
- case 2: if ((x - 1) >= 0 &&
- board[x - 1][y] == '.') {
- board[--x][y] = i + 'B';
- ++i;
- } break;
- case 3: if ((x + 1) >= 0 &&
- board[x + 1][y] == '.') {
- board[++x][y] = i + 'B';
- ++i;
- } break;
- }
- // Reset the random direction
- dir = rand() % 4;
- }
- }
- // Print the board
- 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