-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc5.c
69 lines (64 loc) · 1.79 KB
/
c5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define X_LIMIT 10
#define Y_LIMIT 10
int main(void) {
int direction;
char walk[X_LIMIT][Y_LIMIT];
int i = 0, j = 0;
char letter = 'A';
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
walk[i][j] = '.';
}
}
walk[i][j] = letter;
while (letter < 'Z') {
direction = rand() % 4;
if (direction == 0) {
if (i > 0 && walk[i - 1][j] == '.') {
i--;
walk[i][j] = ++letter;
}
} else if (direction == 1) {
if (j < Y_LIMIT - 1 && walk[i][j + 1] == '.') {
j++;
walk[i][j] = ++letter;
}
} else if (direction == 2) {
if (i < X_LIMIT - 1 && walk[i + 1][j] == '.') {
i++;
walk[i][j] = ++letter;
}
} else if (direction == 3) {
if (j > 0 && walk[i][j - 1] == '.') {
j--;
walk[i][j] = ++letter;
}
}
if (((i == 0 || walk[i - 1][j] != '.')
&& (i == X_LIMIT - 1 || walk[i + 1][j] != '.')
&& (j == 0 || walk[i][j - 1] != '.')
&& (j == Y_LIMIT - 1 || walk[i][j + 1] != '.')))
break;
// Debug
// for (int i = 0; i < X_LIMIT; i++) {
// for (int j = 0; j < Y_LIMIT; j++) {
// printf("%letter ", walk[i][j]);
// }
// putchar('\n');
// }
// printf("*****%d*****", letter - 'A');
// getchar();
}
// Print matrix
for (int i = 0; i < X_LIMIT; i++) {
for (int j = 0; j < Y_LIMIT; j++) {
printf("%c ", walk[i][j]);
}
putchar('\n');
}
return 0;
}