-
Notifications
You must be signed in to change notification settings - Fork 0
/
bird.c
86 lines (86 loc) · 1.69 KB
/
bird.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
Strand mutate(Strand parentStrand){
Strand strand = malloc(MAX_TEXT_SPACE);
memcpy(strand,parentStrand,MAX_TEXT_SPACE);
int i,j;
for(i=0;i<MAX_TEXT_SPACE;i++){
if((rand()%100)<1){//1 percent chance of mutation
j=rand()%8;
switch(j){
case 0:
strand[i]='>';
break;
case 1:
strand[i]='<';
break;
case 2:
strand[i]='+';
break;
case 3:
strand[i]='-';
break;
case 4:
strand[i]=',';
break;
case 5:
strand[i]='.';
break;
case 6:
strand[i]='[';
break;
case 7:
strand[i]=']';
break;
}
}
}
return strand;
}
Bird createBird(Strand strand){
Bird bird;
bird.fat = STARTING_FAT + rand()%50;
bird.strand = mutate(strand);
bird.vm = createVirtualMachine(bird.strand);
return bird;
}
BirdArray generateBirds(){
int i;
BirdArray birdArray;
birdArray.size=MAX_BIRDS;
birdArray.birds=calloc(sizeof(Bird),MAX_BIRDS+1);
Strand def = malloc(MAX_TEXT_SPACE);
for(i=0;i<MAX_TEXT_SPACE;i++){
def[i]='<';
}
for(i=0;i<MAX_BIRDS;i++){
birdArray.birds[i]=createBird(def);
}
return birdArray;
}
Bird * fattest(BirdArray ba){
//return the most fat bird in ba
int fattest=0;//index of the fattest
int i;
for(i=0;i<(ba.size);i++)
if((ba.birds[i].fat) > (ba.birds[fattest].fat))
fattest=i;
return &(ba.birds[fattest]);
}
int updateBird(Bird * bird){
int j;
if((bird->fat)>0){
bird->fat--;
j=step(&(bird->vm),bird->x);
if(j!=-1)
return j;
}else
bird->fat=0;
return -1;
}
void freeBird(Bird * bird){
//obligatory AND THIS BIRD YOU CANNOT CHAAAAAAAAAAAAAAAAAAAAAAAAAAANGE
freeVirtualMachine(bird->vm);
}
void replaceBird(Bird * to,Bird * from){
freeBird(to);
*to = createBird(from->strand);
}