clean:
-rm -f $(BUILD)/* $(OBJDIR)/*
-debug: CFLAGS += -ggdb3 -Og
-debug: $(BIN)
+debug: $(SRC)
+ $(CC) $(SRC) $(FLAGS) -ggdb3 -Og -pipe -o $(BIN)-debug
$(MANPAGE): $(MAN)
$(ZIP) -c $(MAN) > $(MANPAGE)
#include "gol.h"
-extern int row,col,step,ascii,color,weight;
+extern int row,col,step,ascii,color,weight,file;
+extern char *filename;
void parseopts(int n, char **args){
char c;
- while ((c = getopt(n,args,"hVacy:x:t:w:")) != -1){
+ while ((c = getopt(n,args,"hVacy:x:t:w:f:")) != -1){
switch (c) {
case 'h':
help();
case 'a':
ascii = 1;
break;
+ case 'f':
+ file = 1;
+ strcpy(filename,optarg);
+ break;
case '?':
fprintf(stderr, "\t%s\n",optarg);
errorcheck(FLAG_ERR);
if ((err & STATE_ERR) != 0) { fprintf(stderr,"ERROR: Could not determine cell state\n"); }
+ if ((err & FILE_ERR) != 0) { fprintf(stderr,"ERROR: Could not open file\n"); }
+
exit(err);
}
#include "gol.h"
-int row,col,step,ascii,weight;
+int row,col,step,ascii,weight,file;
+char *filename;
int main(int argc, char *argv[]){
srand(time(NULL));
step = 75000;
ascii = 0;
weight = 5;
+ file = 0;
+ filename = malloc(sizeof(char) * MAXLINE);
parseopts(argc,argv);
-
struct map map = newmap();
- genmap(map);
+
+ if (file) {
+ mapfile(map,filename);
+ } else {
+ genmap(map);
+ }
while (stop > 0){
stop = updatemap(map);
}
freemap(map);
-
+ free(filename);
return 0;
}
enum {DIE = -1, DEAD = 0, ALIVE = 1, SPAWN = 1 << 1};
/* error codes */
-enum {NO_ERR = 0, FLAG_ERR = 1, STATE_ERR = 1 << 1, COLOR_ERR = 1 << 2};
+enum {NO_ERR = 0, FLAG_ERR = 1, STATE_ERR = 1 << 1, COLOR_ERR = 1 << 2, FILE_ERR = 1 << 3};
struct cell {
char pixel[4];
/* map.c */
void genmap(struct map);
+void mapfile(struct map, char *);
int updatemap(struct map);
int numneighbor(struct map,int,int);
int checkstate(struct map,int,int);
return;
}
+void mapfile(struct map map, char *file){
+ FILE *fp = fopen(file, "r");
+ if (!fp) errorcheck(FILE_ERR);
+
+ int r = 0;
+ int c = 0;
+ char x = ' ';
+
+ while ((x = getc(fp)) != EOF){
+ /* exit if more rows than in map */
+ if (r >= row) { break; }
+
+ /* spaces are dead, anything else alive */
+ if (x == ' ') {
+ map.line[r].cell[c].state = DEAD;
+ } else if (c < col) {
+ map.line[r].cell[c].state = ALIVE;
+ }
+
+ /* move down a row and reset columns at newlines and column limits */
+ if (x == '\n' || c >= col ) {
+ map.line[r].cell[c].state = DEAD;
+ r++,c=0;
+ }
+
+ c++;
+ }
+
+ fclose(fp);
+ return;
+}
+
int updatemap(struct map map){
int n = 0;
struct map tmp = newmap();
+ /* non edges */
for (int r = 0; r < row-1; r++){
for(int c = 0; c < col-1; c++){
int state = checkstate(map,r,c);
}
}
+ /* left/right side */
for (int r = 0; r < row -1; r++){
int state = checkstate(map,r,0);
tmp.line[r].cell[col-1].state = state;
if (state > DEAD){ n++; }
}
+ /* top/bottom */
for (int c = 0; c < col -1; c++){
int state = checkstate(map,0,c);
tmp.line[row-1].cell[c].state = state;
if (state > DEAD){ n++; }
}
+ /* copy to map */
for(int r = 0; r < row; r++){
for (int c = 0; c < col; c++){
map.line[r].cell[c] = tmp.line[r].cell[c];