code skeleton (not working)
authorLaurent Mazet <mazet@softndesign.org>
Wed, 19 Jun 2024 20:59:41 +0000 (22:59 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Wed, 19 Jun 2024 20:59:41 +0000 (22:59 +0200)
debug.c [new file with mode: 0644]
debug.h [new file with mode: 0644]
display.c [new file with mode: 0644]
display.h [new file with mode: 0644]
gameoflife.c [new file with mode: 0644]

diff --git a/debug.c b/debug.c
new file mode 100644 (file)
index 0000000..b9eba44
--- /dev/null
+++ b/debug.c
@@ -0,0 +1,5 @@
+#include "debug.h"
+
+int verbose = 0;
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/debug.h b/debug.h
new file mode 100644 (file)
index 0000000..32dc0ca
--- /dev/null
+++ b/debug.h
@@ -0,0 +1,21 @@
+#ifndef __DEBUG_H__
+#define __DEBUG_H__
+
+/* constants */
+
+#define DEBUG 3
+#define INFO 2
+#define WARNING 1
+#define ERROR 0
+
+/* macros */
+
+#define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0)
+
+/* gobal variables */
+
+extern int verbose;
+
+#endif /* __DEBUG_H__ */
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/display.c b/display.c
new file mode 100644 (file)
index 0000000..055aa97
--- /dev/null
+++ b/display.c
@@ -0,0 +1,202 @@
+#include <curses.h>
+#include <stdlib.h>
+
+#include "debug.h"
+
+#include "display.h"
+
+typedef enum {
+    white = 1,
+    red,
+    green,
+    blue,
+    cyan,
+    magenta,
+    yellow,
+    black,
+    wred,
+    wgreen,
+    wblue,
+    wcyan,
+    wmagenta,
+    wyellow,
+} color_t;
+
+void set_color (color_t color)
+{
+    static int init = 1;
+
+    if (init) {
+        init_pair (white, COLOR_WHITE, COLOR_BLACK);
+        init_pair (red, COLOR_BLACK, COLOR_RED);
+        init_pair (green, COLOR_BLACK, COLOR_GREEN);
+        init_pair (blue, COLOR_BLACK, COLOR_BLUE);
+        init_pair (magenta, COLOR_BLACK, COLOR_MAGENTA);
+        init_pair (yellow, COLOR_BLACK, COLOR_YELLOW);
+        init_pair (cyan, COLOR_BLACK, COLOR_CYAN);
+        init_pair (black, COLOR_BLACK, COLOR_WHITE);
+        init_pair (wred, COLOR_WHITE, COLOR_RED);
+        init_pair (wgreen, COLOR_WHITE, COLOR_GREEN);
+        init_pair (wblue, COLOR_WHITE, COLOR_BLUE);
+        init_pair (wcyan, COLOR_WHITE, COLOR_CYAN);
+        init_pair (wmagenta, COLOR_WHITE, COLOR_MAGENTA);
+        init_pair (wyellow, COLOR_WHITE, COLOR_YELLOW);
+        init = 0;
+    }
+
+    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;
+    int j = 0;
+    while ((msg) && (*msg != '\0')) {
+        if ((*msg == '\n') || (i  == length)) {
+            i = 0;
+            j++;
+        }
+        if (*msg != '\n') {
+            mvaddch (yoffset + j, xoffset + i, *msg);
+            i++;
+        }
+        msg++;
+    }
+    return j;
+}
+
+void _displaytitle (char *title, int xoffset, int yoffset)
+{
+    int i;
+    for (i = 0; title[i] != '\0'; i++) {
+        mvaddch (yoffset, xoffset + i, title[i]);
+        mvaddch (yoffset + 1, xoffset + i, ACS_HLINE);
+    }
+}
+
+int helpwindow (char *msg, char *msg2, 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)
+{
+    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);
+
+    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]);
+        }
+    }
+    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]);
+                set_color (white);
+            } else {
+                int j;
+                for (j = 0; words->tab[i][j] != '\0'; j++) {
+                    mvaddch (yoffset + 2 + n, xoffset + j, ' ');
+                }
+            }
+            n++;
+        }
+    }
+}
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/display.h b/display.h
new file mode 100644 (file)
index 0000000..2d08abf
--- /dev/null
+++ b/display.h
@@ -0,0 +1,8 @@
+#ifndef __DISPLAY_H__
+#define __DISPLAY_H__
+
+void showboard (int xoffset, int yoffset);
+
+#endif /* __DISPLAY_H__ */
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/gameoflife.c b/gameoflife.c
new file mode 100644 (file)
index 0000000..e43784b
--- /dev/null
@@ -0,0 +1,166 @@
+/* depend: */
+/* cflags: */
+/* linker: debug.o display.o -lcurses */
+/* doslnk: debug.o display.o -lpdc~1 */
+/* winlnk: debug.o display.o -lpdcurses */
+
+#include <curses.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#include "debug.h"
+
+#define KEY_ESC 0x1b
+#define KEY_DELETE 0x014a
+
+#define max(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a > _b ? _a : _b; })
+
+/* static variables */
+char *progname = NULL;
+char *version = "0.1";
+
+char *file = NULL;
+char mode = '\0';
+
+char *help =
+    "<i> Move up tile\n"
+    "<j> Move left tile\n"
+    "<k> Move down tile\n"
+    "<l> Move right tile\n"
+    "<q> Quit\n"
+    "<s> Save file\n"
+    "<v> Put tile\n"
+    "<x> Retrieve tile\n"
+    ;
+
+int usage (int ret)
+{
+    FILE *fd = ret ? stderr : stdout;
+    fprintf (fd, "usage: %s [-e file] [-h] [-p file] [-v level]\n", progname);
+    fprintf (fd, " -e: edit file (%s)\n", (file) ? file : "none");
+    fprintf (fd, " -h: help message\n");
+    fprintf (fd, " -p: play file (%s)\n", (file) ? file : "none");
+    fprintf (fd, " -v: verbose level (%d)\n", verbose);
+    fprintf (fd, "%s version %s\n", progname, version);
+
+    return ret;
+}
+
+/* main function */
+int main (int argc, char *argv[])
+{
+
+    /* get basename */
+    char *pt = progname = argv[0];
+    while (*pt) {
+        if ((*pt == '/') || (*pt == '\\')) {
+           progname = pt + 1;
+        }
+        pt++;
+    }
+
+    /* process argument */
+     while (argc-- > 1) {
+        char *arg = *(++argv);
+        if (arg[0] != '-') {
+            VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- %s\n", progname, arg));
+            return usage (1);
+        }
+        char c = arg[1];
+        switch (c) {
+        case 'e':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: no board specified\n", progname));
+                return usage (1);
+            }
+            mode = 'e';
+            file = arg;
+            break;
+        case 'p':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: no dictionary specified\n", progname));
+                return usage (1);
+            }
+            mode = 'p';
+            file = arg;
+            break;
+        case 'v':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname));
+                return usage (1);
+            }
+            verbose = atoi (arg);
+            break;
+        case 'h':
+        default:
+            return usage (c != 'h');
+        }
+    }
+
+    /* init curses window */
+    initscr ();
+    noecho ();
+    cbreak ();
+    nonl ();
+    keypad (stdscr, TRUE);
+    curs_set (0);
+
+    start_color ();
+
+    /* event loop */
+    int stop = 0;
+    while (!stop) {
+        switch (getch ()) {
+        case '\n':
+        case '\r':
+            break;
+        case KEY_UP:
+        case 'i':
+            break;
+        case KEY_LEFT:
+        case 'j':
+            break;
+        case KEY_DOWN:
+        case 'k':
+            break;
+        case KEY_RIGHT:
+        case 'l':
+            break;
+        case KEY_ESC:
+        case 'q':
+            stop = 1;
+            break;
+        case 's':
+            break;
+        case ' ':
+        case 'v':
+            break;
+        case KEY_BACKSPACE:
+        case KEY_DELETE:
+        case 127:
+        case '\b':
+        case 'x':
+            break;
+        //case ERR:
+        //default:
+        }
+    }
+
+    endwin ();
+
+    return 0;
+}
+
+/* test: gameoflife.exe -e 2>&1 | grep 'no dictionary' */
+/* 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 -v 2>&1 | grep missing */
+/* test: gameoflife.exe _ 2>&1 | grep invalid */
+
+/* vim: set ts=4 sw=4 et: */