From: Laurent Mazet Date: Thu, 20 Jun 2024 21:44:29 +0000 (+0200) Subject: read file and init blank board X-Git-Tag: v1.0~29 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=8f0693978f3ce8cd444b6140c8447e6f117268fe;p=gameoflife.git read file and init blank board --- diff --git a/display.c b/display.c index 055aa97..ed898b6 100644 --- a/display.c +++ b/display.c @@ -1,7 +1,9 @@ #include -#include +#include #include "debug.h" +#include "function.h" +#include "type.h" #include "display.h" @@ -47,46 +49,6 @@ void set_color (color_t color) attrset (COLOR_PAIR(color)); } -void showboard (int xoffset, int yoffset) -{ - int x, y; - for (x = -1; x <= board->xsize; x++) { - for (y = -1; y <= board->ysize; y++) { - int c = ' '; - char pos[12] = {0}; - if ((x == -1) && (y == -1)) { - c = ACS_ULCORNER; - set_color (black); - } else if ((x == board->xsize) && (y== -1)) { - c = ACS_URCORNER; - set_color (black); - } else if ((x == -1) && (y == board->ysize)) { - c = ACS_LLCORNER; - set_color (black); - } else if ((x == board->xsize) && (y == board->ysize)) { - c = ACS_LRCORNER; - set_color (black); - } else if ((x == -1) || (x == board->xsize)) { - c = ACS_VLINE; - set_color (black); - } else if ((y == -1) || (y == board->ysize)) { - c = ACS_HLINE; - set_color (black); - } else { - c = play->tab[x + y * board->xsize]; - set_color (black); - } - } - VERBOSE (DEBUG, if ((c < 32) || (c > 255)) printf ("character: %c\n", c)); - if (c) { - mvaddch(yoffset + y, xoffset + x, c); - } - set_color (white); - - } - } -} - int _helpwindow (char *msg, int xoffset, int yoffset, int length) { int i = 0; @@ -114,87 +76,42 @@ void _displaytitle (char *title, int xoffset, int yoffset) } } -int helpwindow (char *msg, char *msg2, int xoffset, int yoffset) +int helpwindow (char *msg, int xoffset, int yoffset) { _displaytitle ("Help message", xoffset, yoffset); int length = strmaxlen (msg, '\n'); int j = 2; j += _helpwindow (msg, xoffset, yoffset + j, length); - j++; - j += _helpwindow (msg2, xoffset, yoffset + j, length); return length; } -void drawwindow (draw_t *game, int score, int xoffset, int yoffset, int mode) +void boardwindow (board_t *board, int xoffset, int yoffset, int bound) { - int i; - - for (i = 0; i < 2 * game->nbtiles - 1; i++) { - mvaddch (yoffset - 1, xoffset + i, ACS_HLINE); - mvaddch (yoffset, xoffset + i, ' '); - mvaddch (yoffset + 1, xoffset + i, ACS_HLINE); - } - mvaddch (yoffset - 1, xoffset - 1, ACS_ULCORNER); - mvaddch (yoffset, xoffset - 1, ACS_VLINE); - mvaddch (yoffset + 1, xoffset - 1, ACS_LLCORNER); - mvaddch (yoffset - 1, xoffset + 2 * game->nbtiles - 1, ACS_URCORNER); - mvaddch (yoffset, xoffset + 2 * game->nbtiles - 1, ACS_VLINE); - mvaddch (yoffset + 1, xoffset + 2 * game->nbtiles - 1, ACS_LRCORNER); + int i, j; - if (mode) { - mvaddch (yoffset - 1, xoffset + 2 * game->index , ACS_TTEE); - mvaddch (yoffset + 1, xoffset + 2 * game->index , ACS_BTEE); - } - - set_color (black); - for (i = 0; i < game->nbtiles; i++) { - if (game->tiles[i] != ' ') { - mvaddch (yoffset, xoffset + 2 * i, game->tiles[i]); + if (bound) { + for (i = 0; i < board->xsize; i++) { + mvaddch (yoffset - 1, xoffset + i, ACS_HLINE); + mvaddch (yoffset + board->ysize, xoffset + i, ACS_HLINE); + } + for (j = 0; j < board->ysize; j++) { + mvaddch (yoffset + j, xoffset - 1, ACS_VLINE); + mvaddch (yoffset + j, xoffset + board->xsize, ACS_VLINE); } + mvaddch (yoffset - 1, xoffset - 1, ACS_ULCORNER); + mvaddch (yoffset + board->ysize, xoffset - 1, ACS_LLCORNER); + mvaddch (yoffset - 1, xoffset + board->xsize, ACS_URCORNER); + mvaddch (yoffset + board->ysize, xoffset + board->xsize, ACS_LRCORNER); } - set_color (white); - char str[11] = {0}; - sprintf (str, "%3d", score); - mvaddstr (yoffset + 2, xoffset + (game->nbtiles * 2 - 1) / 2 - 2, str); -} - -void scorewindow (int score, int xoffset, int yoffset) -{ - _displaytitle ("Score", xoffset, yoffset); - char str[12] = {0}; - sprintf (str, " %3d", score); - mvaddstr (yoffset + 2, xoffset, str); -} - -void spellwindow (word_t *words, int xoffset, int yoffset, int mode) -{ - _displaytitle ("Spelling", xoffset, yoffset); - int i, n = 0; - for (i = 0; i < words->maxnbwords; i++) { - if (words->status[i] != none) { - if (mode) { - switch (words->status[i]) { - case correct: - set_color (wgreen); - break; - case incorrect: - set_color (wred); - break; - default: - set_color (white); - break; - } - mvaddstr (yoffset + 2 + n, xoffset, words->tab[i]); + for (i = 0; i < board->xsize; i++) { + for (j = 0; j < board->ysize; j++) { + if (board->tab[i + board->xsize * j] != ' ') { + set_color (black); + mvaddch (yoffset + j, xoffset + i, ' '); set_color (white); - } else { - int j; - for (j = 0; words->tab[i][j] != '\0'; j++) { - mvaddch (yoffset + 2 + n, xoffset + j, ' '); - } } - n++; } } } diff --git a/display.h b/display.h index 2d08abf..555fb19 100644 --- a/display.h +++ b/display.h @@ -1,7 +1,11 @@ #ifndef __DISPLAY_H__ #define __DISPLAY_H__ -void showboard (int xoffset, int yoffset); +#include "type.h" + +int helpwindow (char *msg, int xoffset, int yoffset); + +void boardwindow (board_t *board, int xboard, int yboard, int bound); #endif /* __DISPLAY_H__ */ diff --git a/function.c b/function.c new file mode 100644 index 0000000..0c9923b --- /dev/null +++ b/function.c @@ -0,0 +1,163 @@ +#include +#include +#include + +#include "debug.h" +#include "type.h" + +#include "function.h" + +int strmaxlen (char *str, char ch) +{ + int len = 0; + char *end = NULL; + while ((end = strchr (str, ch)) != NULL) { + int l = (int)(end - str); + if (l > len) { + len = l; + } + str = end + 1; + } + return len; +} + +board_t *initboard (int xsize, int ysize) +{ + board_t *board = (board_t *) malloc (sizeof (board_t)); + CHECKALLOC (board); + board->tab = (char *) calloc (1, xsize * ysize + 1); + CHECKALLOC (board->tab); + memset (board->tab, ' ', xsize * ysize); + board->xsize = xsize; + board->ysize = ysize; + return board; +} + +void freeboard (board_t *board) +{ + if (board) { + free (board->tab); + } + free (board); +} + +void cleanboard (board_t *board) +{ + memset (board->tab, ' ', board->xsize * board->ysize); +} + +char *saveboard (board_t *board, char *name) +{ + int size = 9 + strlen (name) + 2 * (8 + 3) + 8 + board->xsize * board->ysize + 1; + 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, "tab: \"%s\"\n", board->tab); + + return buffer; +} + +int writedata (char *name, char *data) +{ + int ret = 0; + + FILE *fd = fopen (name, "w"); + if (fd) { + size_t n = fwrite (data, strlen (data), 1, fd); + fclose (fd); + ret = (n == strlen (data)); + } else { + VERBOSE (WARNING, printf ("can't write file (%s)\n", name)); + } + + return ret; +} + +char *readdata (char *filename) +{ + char *buffer = NULL; + FILE *fd = fopen (filename, "r"); + if (fd) { + fseek (fd, 0, SEEK_END); + int size = ftell (fd); + buffer = (char *) calloc (size + 1, 1); + CHECKALLOC (buffer); + + fseek (fd, 0, SEEK_SET); + int nb = fread (buffer, 1, size, fd); + if (nb != size) { + VERBOSE (WARNING, printf ("can't fully read file (%s)\n", filename)); + } + fclose (fd); + } + + return buffer; +} + +char *atos (char *str) +{ + char *ret = NULL; + + if (*str == '"') { + char *ptr = ++str; + while ((*ptr != '"') && (*ptr != '\0')) { + ptr++; + } + if (*ptr == '"') { + *ptr = '\0'; + ret = str; + } + } + + return ret; +} + +board_t *loadboard (char *str) +{ + char *name = NULL; + int xsize = 0; + int ysize = 0; + char *tab = NULL; + + char *saveptr1, *saveptr2; + + char *line = strtok_r (str, "\n", &saveptr1); + while (line) { + + char *keyword = strtok_r (line, ":", &saveptr2); + + char *value = line + strlen (keyword) + 1; + while (*value == ' ') { + value++; + } + + if (strcmp (keyword, "name") == 0) { + name = atos (value); + } else if (strcmp (keyword, "xsize") == 0) { + xsize = atoi (value); + } else if (strcmp (keyword, "ysize") == 0) { + ysize = atoi (value); + } else if (strcmp (keyword, "tab") == 0) { + tab = atos (value); + } else { + VERBOSE (WARNING, printf ("unknown keyword: %s\n", name)); + } + + line = strtok_r (NULL, "\n", &saveptr1); + } + + board_t *board = NULL; + if ((tab) && (strlen (tab) == (size_t)(xsize * ysize))) { + board = initboard (xsize, ysize); + strcpy (board->tab, tab); + } else if (xsize * ysize > 0) { + board = initboard (xsize, ysize); + } + + return board; +} + +/* vim: set ts=4 sw=4 et: */ diff --git a/function.h b/function.h new file mode 100644 index 0000000..fc5101a --- /dev/null +++ b/function.h @@ -0,0 +1,32 @@ +#ifndef __FUNCTION_H__ +#define __FUNCTION_H__ + +#include "type.h" + +#define CHECKALLOC(ptr) \ + do { \ + if ((ptr) == NULL) { \ + VERBOSE (ERROR, fprintf (stderr, "can't get enough memory for '%s'\n", #ptr)); \ + exit (1); \ + } \ + } while (0) + +int strmaxlen (char *str, char ch); + +board_t *initboard (int xsize, int ysize); + +void freeboard (board_t *board); + +void cleanboard (board_t *board); + +char *saveboard (board_t *board, char *name); + +int writedata (char *name, char *data); + +char *readdata (char *filename); + +board_t *loadboard (char *str); + +#endif /* __FUNCTION_H__ */ + +/* vim: set ts=4 sw=4 et: */ diff --git a/gameoflife.c b/gameoflife.c index e43784b..4e56d35 100644 --- a/gameoflife.c +++ b/gameoflife.c @@ -1,8 +1,8 @@ /* depend: */ /* cflags: */ -/* linker: debug.o display.o -lcurses */ -/* doslnk: debug.o display.o -lpdc~1 */ -/* winlnk: debug.o display.o -lpdcurses */ +/* linker: debug.o display.o function.o -lcurses */ +/* doslnk: debug.o display.o function.o -lpdc~1 */ +/* winlnk: debug.o display.o function.o -lpdcurses */ #include #include @@ -10,6 +10,9 @@ #include #include "debug.h" +#include "display.h" +#include "function.h" +#include "type.h" #define KEY_ESC 0x1b #define KEY_DELETE 0x014a @@ -101,6 +104,31 @@ int main (int argc, char *argv[]) } } + if (mode == '\0') { + VERBOSE (ERROR, fprintf (stderr, "no mode defined\n")); + return 1; + } + + /* init board */ + char *buffer = readdata (file); + board_t *board = NULL; + if (buffer) { + board = loadboard (buffer); + free (buffer); + if (board == NULL) { + VERBOSE (ERROR, fprintf (stderr, "incorrect file (%s)\n", file)); + return 1; + } + } else { + board = initboard (60, 20); + } + + /* window positions */ + int xboard = 3; + int yboard = 3; + int xhelp = xboard + board->xsize + 3; + int yhelp = yboard; + /* init curses window */ initscr (); noecho (); @@ -111,9 +139,13 @@ int main (int argc, char *argv[]) start_color (); + helpwindow (help, xhelp, yhelp); + /* event loop */ int stop = 0; while (!stop) { + boardwindow (board, xboard, yboard, 1); + switch (getch ()) { case '\n': case '\r': @@ -152,15 +184,20 @@ int main (int argc, char *argv[]) endwin (); + freeboard (board); + return 0; } -/* test: gameoflife.exe -e 2>&1 | grep 'no dictionary' */ +/* test: gameoflife.exe 2>&1 | grep 'no mode' */ +/* test: gameoflife.exe -e 2>&1 | grep 'no board' */ /* test: gameoflife.exe -e gameoflife.c 2>&1 | grep 'incorrect file' */ /* test: gameoflife.exe -h | grep usage */ /* test: gameoflife.exe -p 2>&1 | grep 'no dictionary' */ -/* test: gameoflife.exe -p file.gol 2>&1 | grep 'not found' */ +/* test--: gameoflife.exe -p file.gol 2>&1 | grep 'not found' */ /* test: gameoflife.exe -v 2>&1 | grep missing */ /* test: gameoflife.exe _ 2>&1 | grep invalid */ +/* test: echo q | gameoflife.exe -e glider.gol */ +/* test: echo q | gameoflife.exe -e new.gol */ /* vim: set ts=4 sw=4 et: */ diff --git a/type.h b/type.h new file mode 100644 index 0000000..b32dd56 --- /dev/null +++ b/type.h @@ -0,0 +1,12 @@ +#ifndef __TYPE_H__ +#define __TYPE_H__ + +typedef struct { + int xsize; + int ysize; + char *tab; +} board_t; + +#endif /* __TYPE_H__ */ + +/* vim: set ts=4 sw=4 et: */