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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#define ITERATIONS 16
//nsew
char *a[16];
char field[256];
char nfield[256];
void print_field() {
int i;
fprintf(stderr,"\x1b[H");
for(i=0;i<(16*strlen(a[field[0]]))+1;i++) fprintf(stderr,"#");
for(i=0;i<256;i++) {
if(i%16 == 0) fprintf(stderr,"#\n#");
fprintf(stderr,"%s",a[field[i]]);
}
fprintf(stderr,"#\n");
for(i=0;i<(16*strlen(a[field[0]]))+2;i++) fprintf(stderr,"#");
fprintf(stderr,"\n");
}
void prune() {//remove paths into wall
int i;
memcpy(nfield,field,256);//make a copy just for comparison
for(i=0;i<256;i++) {
if(i%16 == 0) field[i] &= ~0x1;//remove all left exits at left of map
else if((field[i]&0x1) && !(nfield[i-1]&0x2)) field[i] &= ~0x1;
if(i%16 == 15) field[i] &= ~0x2;//remove all right exits at right of map.
else if((field[i]&0x2) && !(nfield[i+1]&0x1)) field[i] &= ~0x2;
if(i > 240) field[i] &= ~0x4;
else if((field[i]&0x4) && !(nfield[i+16]&0x8)) field[i] &= ~0x4;
if(i < 16) field[i] &= ~0x8;
else if((field[i]&0x8) && !(nfield[i-16]&0x4)) field[i] &= ~0x8;
}
}
void grow() {
int i=0;
memcpy(nfield,field,256);
for(i=0;i<256;i++) {
if(random() % 2 == 0) {
if((field[i]&0x1) && !nfield[i-1] && i%16 != 0) nfield[i-1]=random()%16 | 0x2;
if((field[i]&0x2) && !nfield[i+1] && i%16 != 15) nfield[i+1]=random()%16 | 0x1;
if((field[i]&0x4) && !nfield[i+16] && i<240) nfield[i+16]=random()%16 | 0x8;
if((field[i]&0x8) && !nfield[i-16] && i>16) nfield[i-16]=random()%16 | 0x4;
}
}
memcpy(field,nfield,256);
}
void dump() {
int i;
for(i=0;i<256;i++) {
printf("%c",field[i]);
}
}
int main(int argc,char *argv[]) {
int i;
int seed=time(0) * getpid();
if(argc > 1) {
seed=atoi(argv[1]);
}
fprintf(stderr,"\x1b[H\x1b[2J");
srandom(seed);
a[0x0]=" ";
a[0x1]="- ";
a[0x2]=" -";
a[0x3]="---";
a[0x4]=" . ";
a[0x5]="-. ";
a[0x6]=" .-";
a[0x7]="-.-";
a[0x8]=" ' ";
a[0x9]="-' ";
a[0xa]=" '-";
a[0xb]="-'-";
a[0xc]=" | ";
a[0xd]="-| ";
a[0xe]=" |-";
a[0xf]="-|-";
field[7+16] = 0x4;//center top
for(i=0;i<ITERATIONS;i++) {
print_field();
// sleep(1);
grow();
}
prune();
print_field();
dump();
fprintf(stderr,"seed: %d\n",seed);
}
|