-
Notifications
You must be signed in to change notification settings - Fork 0
/
life.c
178 lines (163 loc) · 4.73 KB
/
life.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
//#define printhelp
//#define debug
//#define time
#define TAM 20
typedef struct node{
struct node *location[3];
char name;
bool final;
}automata;
void readFile(char startcase[TAM][TAM]){
int fd;
struct stat file;
if((fd=open("./startcase.txt", O_RDONLY, 00777))<0){
perror("failed to open file\n");
}
if(fstat(fd,&file)==-1){
perror("failed getting size\n");
}
void *rawDat=mmap(NULL, file.st_size, PROT_READ, MAP_SHARED, fd, 0);
char *raw=(char*)rawDat;
printf("%p\n",rawDat);
for(int iterX=0; iterX<TAM; iterX++){
for(int iterY=0; iterY<TAM; iterY++){
startcase[iterX][iterY]=raw[21*iterX+iterY];
}
}
munmap(raw, file.st_size);
close(fd);
}
bool generateAutomata(automata **value){
//intial node
bool states[]={0,1,0,0,1,1,0,0,0,0,1};
automata *q[11];
for(int iter=0; iter<11; iter++){
q[iter]=(automata*)malloc(sizeof(automata));
q[iter]->final=states[iter];
q[iter]->name=iter;
}
*value=q[0];
//q0;
q[0]->location[0]=q[0];
q[0]->location[1]=q[1];
q[0]->location[2]=q[7];
//q1
q[1]->location[0]=q[1];
q[1]->location[1]=q[1];
q[1]->location[2]=q[2];
//q2
q[2]->location[0]=q[2];
q[2]->location[1]=q[3];
q[2]->location[2]=q[7];
//q3
q[3]->location[0]=q[3];
q[3]->location[1]=q[4];
q[3]->location[2]=q[7];
//q4
q[4]->location[0]=q[4];
q[4]->location[1]=q[5];
q[4]->location[2]=q[2];
//q5
q[5]->location[0]=q[5];
q[5]->location[1]=q[6];
q[5]->location[2]=q[2];
//q6
q[6]->location[0]=q[6];
q[6]->location[1]=q[6];
q[6]->location[2]=q[7];
//q7
q[7]->location[0]=q[7];
q[7]->location[1]=q[8];
q[7]->location[2]=q[7];
//q8
q[8]->location[0]=q[8];
q[8]->location[1]=q[9];
q[8]->location[2]=q[7];
//q9
q[9]->location[0]=q[9];
q[9]->location[1]=q[10];
q[9]->location[2]=q[7];
//q10
q[10]->location[0]=q[10];
q[10]->location[1]=q[6];
q[10]->location[2]=q[2];
}
void action(automata **value, char element){
(*value)=(*value)->location[element];
}
void debug(automata *tabletop[TAM][TAM]){
automata *help=tabletop[0][0];
for(int iterX=0; iterX<TAM; iterX++){
for(int iterY=0; iterY<TAM; iterY++){
printf("%d ",tabletop[iterX][iterY]==help);
}
printf("\n");
}
}
void life(automata *tabletop[2][TAM][TAM], automata *sentinel, char state){
int deltaY[]={1,1,1,0,-1,-1,-1,0};
int deltaX[]={-1,0,1,1,1,0,-1,-1};
debug(tabletop[state]);
for(int iterX=0; iterX<TAM; iterX++){
for(int iterY=0; iterY<TAM; iterY++){
tabletop[state][iterX][iterY]=tabletop[state^1][iterX][iterY];
action(&tabletop[state][iterX][iterY], 2);
// printf("%d %d ", iterX, iterY);
for(int delta=0; delta<8; delta++){
int y=((iterY+deltaY[delta])<0)?TAM-1:(iterY+deltaY[delta]>=TAM)?0:iterY+deltaY[delta];
int x=((iterX+deltaX[delta])<0)?TAM-1:(iterX+deltaX[delta]>=TAM)?0:iterX+deltaX[delta];
// printf("X:%d Y: %d ", x, y);
bool estado=tabletop[state^1][x][y]->final;
#ifdef debug
printf("%d", estado);
#endif
action(&tabletop[state][iterX][iterY], estado);
}
printf("\n");
}
}
}
void initialize(automata *tabletop[2][TAM][TAM], char matrix[TAM][TAM], automata *sentinel){
for(int iterX=0; iterX<TAM; iterX++){
for(int iterY=0; iterY<TAM; iterY++){
tabletop[0][iterX][iterY]=sentinel;
action(&tabletop[0][iterX][iterY], matrix[iterX][iterY]-'0');
tabletop[1][iterX][iterY]=tabletop[0][iterX][iterY];
// printf("%d %p %p\n", matrix[iterX][iterY]-'0', tabletop[0][iterX][iterY], tabletop[1][iterX][iterY]);
}
}
}
int printTabletop(automata *tabletop[TAM][TAM]){
for(int iterX=0; iterX<TAM; iterX++){
printf("\t\t\t\t");
for(int iterY=0; iterY<TAM; iterY++){
printf("%d ",tabletop[iterX][iterY]->final);
}
printf("\n");
}
return 0;
}
int main(){
char inicial[TAM][TAM]={};
automata *tabletop[2][TAM][TAM];
automata *base;
generateAutomata(&base);
initialize(tabletop, inicial, base);
char state=1;
char value;
printTabletop(tabletop[state]);
while((value=fgetc(stdin))!='q'){
life(tabletop, base, state);
printTabletop(tabletop[state]);
state^=1;
}
printf("hola %ld\n",sizeof(automata));
}