From: Laurent Mazet Date: Sun, 23 Jun 2024 11:36:05 +0000 (+0200) Subject: correct copy X-Git-Tag: v1.0~20 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=84be3c24f8c495bc9413ed0fa3f12488ec706600;p=gameoflife.git correct copy --- diff --git a/function.c b/function.c index aa5a1e9..c24b319 100644 --- a/function.c +++ b/function.c @@ -33,9 +33,9 @@ board_t *initboard (int xsize, int ysize) return board; } -void copyboard (board_t *dst, board_t *src) +board_t *copyboard (board_t *dst, board_t *src) { - if (dst->xsize * dst->ysize != src->xsize * src->ysize) { + if ((dst->xsize != src->xsize) || (dst->ysize != src->ysize)) { free (dst->tab); dst->tab = (char *) calloc (1, src->xsize * src->ysize + 1); CHECKALLOC (dst->tab); @@ -43,6 +43,26 @@ void copyboard (board_t *dst, board_t *src) dst->ysize = src->ysize; } memcpy (dst->tab, src->tab, dst->xsize * dst->ysize); + + return dst; +} + +board_t *resizeboard (board_t *board, int xsize, int ysize) +{ + int i, j; + + board_t *newboard = initboard (xsize, ysize); + + for (i = 0; (i < board->xsize) && (i < xsize); i++) { + for (j = 0; (j < board->ysize) && (j < ysize); j++) { + *getcell (newboard, i, j) = *getcell (board, i, j); + } + } + + copyboard (board, newboard); + freeboard (newboard); + + return board; } void freeboard (board_t *board) @@ -53,22 +73,40 @@ void freeboard (board_t *board) free (board); } -void cleanboard (board_t *board) +board_t *cleanboard (board_t *board) { memset (board->tab, ' ', board->xsize * board->ysize); + return board; +} + +int _makecomments (char *buffer, board_t *board) +{ + int i, j, l = 0; + for (j = 0; j < board->ysize; j++) { + l += sprintf (buffer + l, "rem: \""); + for (i = 0; i < board->xsize; i++) { + l += sprintf (buffer + l, "%c", *getcell (board, i, j)); + } + l += sprintf (buffer + l, "\"\n"); + } + return l; } char *saveboard (board_t *board, char *name) { int size = 9 + strlen (name) + 2 * (8 + 3) + 8 + board->xsize * board->ysize + 1; + VERBOSE (INFO, size += board->ysize * (8 + board->xsize)); + char *buffer = (char *) calloc (size, 1); CHECKALLOC (buffer); int l = sprintf (buffer, "name: \"%s\"\n", name); l += sprintf (buffer + l, "xsize: %d\n", board->xsize); - l += sprintf (buffer + l, "xsize: %d\n", board->ysize); + l += sprintf (buffer + l, "ysize: %d\n", board->ysize); l += sprintf (buffer + l, "tab: \"%s\"\n", board->tab); + VERBOSE (INFO, _makecomments (buffer + l, board)); + return buffer; } @@ -154,6 +192,8 @@ board_t *loadboard (char *str) ysize = atoi (value); } else if (strcmp (keyword, "tab") == 0) { tab = atos (value); + } else if (strcmp (keyword, "rem") == 0) { + /* nothing to do with remark */ } else { VERBOSE (WARNING, printf ("unknown keyword: %s\n", name)); } @@ -196,7 +236,7 @@ int getnbneighbours (board_t *board, int x, int y) return nb; } -void computenextgen (board_t *board) +board_t *computenextgen (board_t *board) { int i, j; board_t *nextboard = initboard (board->xsize, board->ysize); @@ -230,9 +270,11 @@ void computenextgen (board_t *board) copyboard (board, nextboard); freeboard (nextboard); + + return board; } -void rotateelement (board_t *element, int rot) +board_t *rotateelement (board_t *element, int rot) { int i, j; @@ -268,12 +310,18 @@ void rotateelement (board_t *element, int rot) break; } - copyboard (element, newelement); + if (rot % 2 == 1) { + resizeboard (newelement, element->ysize, element->xsize); + } + copyboard (element, newelement); freeboard (newelement); + + return element; } -void mirrorelement (board_t *element, int mode) + +board_t *mirrorelement (board_t *element, int mode) { int i, j; @@ -299,9 +347,11 @@ void mirrorelement (board_t *element, int mode) } } } + + return element; } -void putelement (board_t *board, board_t *element, int x, int y, int mode) +board_t *putelement (board_t *board, board_t *element, int x, int y, int mode) { int i, j; @@ -312,6 +362,8 @@ void putelement (board_t *board, board_t *element, int x, int y, int mode) } } } + + return board; } /* vim: set ts=4 sw=4 et: */ diff --git a/function.h b/function.h index bc4a8cf..a402ab4 100644 --- a/function.h +++ b/function.h @@ -15,11 +15,13 @@ int strmaxlen (char *str, char ch); board_t *initboard (int xsize, int ysize); -void copyboard (board_t *dst, board_t *src); +board_t *copyboard (board_t *dst, board_t *src); + +board_t *resizeboard (board_t *board, int xsize, int ysize); void freeboard (board_t *board); -void cleanboard (board_t *board); +board_t *cleanboard (board_t *board); char *saveboard (board_t *board, char *name); @@ -31,13 +33,13 @@ board_t *loadboard (char *str); char *getcell (board_t *board, int x, int y); -void computenextgen (board_t *board); +board_t *computenextgen (board_t *board); -void rotateelement (board_t *element, int rot); +board_t *rotateelement (board_t *element, int rot); -void mirrorelement (board_t *element, int mode); +board_t *mirrorelement (board_t *element, int mode); -void putelement (board_t *board, board_t *element, int x, int y, int mode); +board_t *putelement (board_t *board, board_t *element, int x, int y, int mode); #endif /* __FUNCTION_H__ */