From 3628b23b681d65ce8be25ebf5530d7fb103e7f09 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Tue, 24 Jan 2023 13:52:46 -0600 Subject: [PATCH] input file map generation --- Makefile | 4 ++-- src/function.c | 11 +++++++++-- src/gol.c | 15 +++++++++++---- src/gol.h | 3 ++- src/map.c | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 4ef4aaa..7d9852e 100644 --- a/Makefile +++ b/Makefile @@ -66,8 +66,8 @@ $(OBJDIR)/%.o: $(SRCDIR)/%.c $(HDR) 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) diff --git a/src/function.c b/src/function.c index 14b6580..363f253 100644 --- a/src/function.c +++ b/src/function.c @@ -1,11 +1,12 @@ #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(); @@ -29,6 +30,10 @@ void parseopts(int n, char **args){ case 'a': ascii = 1; break; + case 'f': + file = 1; + strcpy(filename,optarg); + break; case '?': fprintf(stderr, "\t%s\n",optarg); errorcheck(FLAG_ERR); @@ -54,6 +59,8 @@ void errorcheck(int 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); } diff --git a/src/gol.c b/src/gol.c index 093a368..5a010b8 100644 --- a/src/gol.c +++ b/src/gol.c @@ -1,6 +1,7 @@ #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)); @@ -11,11 +12,17 @@ int main(int argc, char *argv[]){ 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); @@ -24,7 +31,7 @@ int main(int argc, char *argv[]){ } freemap(map); - + free(filename); return 0; } diff --git a/src/gol.h b/src/gol.h index 517b808..f81ba77 100644 --- a/src/gol.h +++ b/src/gol.h @@ -15,7 +15,7 @@ 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]; @@ -39,6 +39,7 @@ void printdisplay(struct map); /* 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); diff --git a/src/map.c b/src/map.c index ae5ca41..95c3e20 100644 --- a/src/map.c +++ b/src/map.c @@ -17,10 +17,43 @@ void genmap(struct map map){ 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); @@ -31,6 +64,7 @@ int updatemap(struct map map){ } } + /* 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; @@ -39,6 +73,7 @@ int updatemap(struct map map){ 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; @@ -47,6 +82,7 @@ int updatemap(struct map map){ 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]; -- 2.44.2