add board definitions
authorLaurent Mazet <mazet@softndesign.org>
Thu, 17 Oct 2024 21:21:53 +0000 (23:21 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Thu, 17 Oct 2024 21:21:53 +0000 (23:21 +0200)
constant.c [new file with mode: 0644]
constant.h [new file with mode: 0644]
function.c
function.h
pacman.c

diff --git a/constant.c b/constant.c
new file mode 100644 (file)
index 0000000..b96873e
--- /dev/null
@@ -0,0 +1,118 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "function.h"
+#include "type.h"
+
+#include "constant.h"
+
+/* constants */
+
+int maxnbturns = 50;
+int turnspan = 400;
+int savelen = 12;
+int scorelen = 15;
+int xoffset = 1;
+int yoffset = 1;
+
+/* board */
+
+char _tab_39x20[39 * 20 + 1] =
+    "F----------7  |.L-----J.|  F----------7"
+    "|*.........|  |....c....|  |.........*|"
+    "|.F-7.F--7.|  |.F-----7.|  |.F--7.F-7.|"
+    "|.L-J.L--J.L--J.L-7 F-J.L--J.L--J.L-J.|"
+    "|................*| |*................|"
+    "|.F-7.F-----7.F---J L---7.F-----7.F-7.|"
+    "|.| |.L---7 |.L---------J.| F---J.| |.|"
+    "|.L-J....*L-J.............L-J*....L-J.|"
+    "|.....F-7.....F---3xE---7.....F-7.....|"
+    "L---7.| L---7.| P     Q |.F---J |.F---J"
+    "F---J.| F---J.|   R S   |.L---7 |.L---7"
+    "|.....L-J.....L---------J.....L-J.....|"
+    "|.F-7....*F-7.............F-7*....F-7.|"
+    "|.| |.F---J |.F---------7.| L---7.| |.|"
+    "|.L-J.L-----J.L---7 F---J.L-----J L-J.|"
+    "|................*| |*................|"
+    "|.F-7.F--7.F--7.F-J L-7.F--7.F--7.F-7.|"
+    "|.L-J.L--J.|  |.L-----J.|  |.L--J.L-J.|"
+    "|*.........|  |.........|  |.........*|"
+    "L----------J  |.F-----7.|  L----------J";
+board_t _board_39x20 = { 39, 20, _tab_39x20, 0, 0 };
+
+char _tab_39x22[39 * 22 + 1] =
+    "F----------7  |.L-----J.|  F----------7"
+    "|*.........|  |....c....|  |.........*|"
+    "|.F-7.F--7.|  |.F-----7.|  |.F--7.F-7.|"
+    "|.L-J.L--J.L--J.L-7 F-J.L--J.L--J.L-J.|"
+    "|................*| |*................|"
+    "|.F-7.F-----7.F---J L---7.F-----7.F-7.|"
+    "|.| |.L---7 |.L---------J.| F---J.| |.|"
+    "|.| |....*| |.............| |*....| |.|"
+    "|.L-J.F-7.L-J.F---3xE---7.L-J.F-7.L-J.|"
+    "|.....| |.....| P Q R S |.....| |.....|"
+    "L---7.| L---7.|F-7   F-7|.F---J |.F---J"
+    "F---J.| F---J.|| L---J ||.L---7 |.L---7"
+    "|.....| |.....|L-------J|.....| |.....|"
+    "|.F-7.L-J.F-7.L---------J.F-7.L-J.F-7.|"
+    "|.| |....*| |.............| |*....| |.|"
+    "|.| |.F---J |.F---------7.| L---7.| |.|"
+    "|.L-J.L-----J.L---7 F---J.L-----J L-J.|"
+    "|................*| |*................|"
+    "|.F-7.F--7.F--7.F-J L-7.F--7.F--7.F-7.|"
+    "|.L-J.L--J.|  |.L-----J.|  |.L--J.L-J.|"
+    "|*.........|  |.........|  |.........*|"
+    "L----------J  |.F-----7.|  L----------J";
+board_t _board_39x22 = { 39, 22, _tab_39x22, 0, 0 };
+
+char _tab_39x24[39 * 24 + 1] =
+    "F----------7  |.L-----J.|  F----------7"
+    "|*.........|  |....c....|  |.........*|"
+    "|.F-7.F--7.|  |.F-----7.|  |.F--7.F-7.|"
+    "|.L-J.L--J.L--J.L-7 F-J.L--J.L--J.L-J.|"
+    "|................*| |*................|"
+    "|.F-7.F-----7.F---J L---7.F-----7.F-7.|"
+    "|.| |.L---7 |.L---------J.| F---J.| |.|"
+    "|.| |....*| |.............| |*....| |.|"
+    "|.| |.F-7.| |.F---7xF---7.| |.F-7.| |.|"
+    "|.L-J.| |.L-J.|F--J L--7|.L-J.| |.L-J.|"
+    "|.....| |.....|| P Q R ||.....| |.....|"
+    "L---7.| L---7.||F7 S F7||.F---J |.F---J"
+    "F---J.| F---J.|||L---J|||.L---7 |.L---7"
+    "|.....| |.....||L-----J||.....| |.....|"
+    "|.F-7.| |.F-7.|L-------J|.F-7.| |.F-7.|"
+    "|.| |.L-J.| |.L---------J.| |.L-J.| |.|"
+    "|.| |....*| |.............| |*....| |.|"
+    "|.| |.F---J |.F---------7.| L---7.| |.|"
+    "|.L-J.L-----J.L---7 F---J.L-----J L-J.|"
+    "|................*| |*................|"
+    "|.F-7.F--7.F--7.F-J L-7.F--7.F--7.F-7.|"
+    "|.L-J.L--J.|  |.L-----J.|  |.L--J.L-J.|"
+    "|*.........|  |.........|  |.........*|"
+    "L----------J  |.F-----7.|  L----------J";
+board_t _board_39x24 = { 39, 24, _tab_39x24, 0, 0 };
+
+board_t *getboard (char *name)
+{
+    board_t *board = NULL;
+    board_t *pt = NULL;
+
+    if (strcmp (name, "39x20") == 0) {
+        pt = &_board_39x20;
+    } else if (strcmp (name, "39x22") == 0) {
+        pt = &_board_39x22;
+    } else if (strcmp (name, "39x24") == 0) {
+        pt = &_board_39x24;
+    } else if (strcmp (name, "list") == 0) {
+        printf ("board: 39x20 39x22 39x24\n");
+        board = (board_t *)(-1);
+    }
+
+    if (pt) {
+        board = copyboard (pt);
+    }
+
+    return board;
+}
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/constant.h b/constant.h
new file mode 100644 (file)
index 0000000..5a790ad
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef __CONSTANT_H__
+#define __CONSTANT_H__
+
+#include "type.h"
+
+/* global constants */
+
+#ifndef __CONSTANT_C__
+
+extern int maxnbturns;
+extern int turnspan;
+extern int savelen;
+extern int scorelen;
+extern int xoffset;
+extern int yoffset;
+
+#endif /* __CONSTANT_C__ */
+
+board_t *getboard (char *name);
+
+#endif /* __CONSTANT_H__ */
+
+/* vim: set ts=4 sw=4 et: */
index 0d5ef3b9c5658b20a31128e6ad8bb7b5eb34cd3f..6ddcac287d97bdcbaff5c451a1c3fa89ca11f6e5 100644 (file)
@@ -59,6 +59,13 @@ void freeboard (board_t *board)
     free (board);
 }
 
+board_t *copyboard (board_t *board)
+{
+    board_t *newboard = initboard (board->width, board->height);
+    memcpy (newboard->tab, board->tab, board->width * board->height + 1);
+    return newboard;
+}
+
 int _makecomments (char *buffer, board_t *board)
 {
     int i, j, l = 0;
index f666f4df3ca909d8f643fbfb4513c7fa31807bce..f7c0f08132669494a58e058f7b17f5cc58ba1e7e 100644 (file)
@@ -29,6 +29,8 @@ board_t *setscale (board_t *board, int scale);
 
 void freeboard (board_t *board);
 
+board_t *copyboard (board_t *board);
+
 int _makecomments (char *buffer, board_t *board);
 
 char *saveboard (board_t *board);
index c37fd3026a05354ac0e16767861567a9b1f9a654..5ded060164bbe7826ee52fa71bf464ba931a9e0c 100644 (file)
--- a/pacman.c
+++ b/pacman.c
@@ -1,14 +1,15 @@
 /* depend: */
 /* cflags: */
-/* linker: color.o debug.o display.o function.o time.c -lcurses */
-/* doslnk: color.o debug.o display.o function.o time.c -lpdc~1 */
-/* winlnk: color.o debug.o display.o function.o time.c -lpdcurses */
+/* linker: color.o constant.o debug.o display.o function.o time.c -lcurses */
+/* doslnk: color.o constant.o debug.o display.o function.o time.c -lpdc~1 */
+/* winlnk: color.o constant.o debug.o display.o function.o time.c -lpdcurses */
 
 #include <curses.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+#include "constant.h"
 #include "debug.h"
 #include "display.h"
 #include "function.h"
 char *progname = NULL;
 char *version = "0.1";
 
+char *boardname = "39x24";
 char *filename = NULL;
 int maxnbrecords = 0;
-int maxnbturns = 50;
-int savelen = 12;
-int turnspan = 400;
-
-int height = 20;
-int width = 40;
-int xoffset = 1;
-int yoffset = 1;
-int scorelen = 15;
 
 char *help =
     "<h> Help message\n"
@@ -44,7 +37,8 @@ char *help =
 int usage (int ret)
 {
     FILE *fd = ret ? stderr : stdout;
-    fprintf (fd, "usage: %s [-f file] [-h] [-r int] [-v int]\n", progname);
+    fprintf (fd, "usage: %s [-b name] [-f file] [-h] [-r int] [-v int]\n", progname);
+    fprintf (fd, " -b: board form  [39x24, 39x22, 39x20] (%s)\n", boardname);
     fprintf (fd, " -f: file name (%s)\n", (filename) ? filename : "none");
     fprintf (fd, " -h: help message\n");
     fprintf (fd, " -r: max nb records (%d)\n", maxnbrecords);
@@ -99,6 +93,14 @@ int main (int argc, char *argv[])
         }
         char c = arg[1];
         switch (c) {
+        case 'b':
+            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);
+            }
+            boardname = arg;
+            break;
         case 'f':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -144,7 +146,14 @@ int main (int argc, char *argv[])
             return 1;
         }
     } else {
-        board = initboard (width, height);
+        board = getboard (boardname);
+        if (board == (board_t *)(-1)) {
+            return 0;
+        }
+        if (board == NULL) {
+            VERBOSE (ERROR, fprintf (stderr, "unknown board (%s)\n", boardname));
+            return 1;
+        }
     }
 
     /* init recorder */
@@ -399,13 +408,16 @@ int main (int argc, char *argv[])
     return 0;
 }
 
+/* test: pacman.exe -b 2>&1 | grep 'no board' */
+/* test: pacman.exe -b unknown 2>&1 | grep 'unknown board' */
+/* test: pacman.exe -b list | grep '^board:' */
 /* test: pacman.exe -f 2>&1 | grep 'no file' */
-/* test: pacman.exe -f nofile.pen 2>&1 | grep "can't read file" */
-/* test: pacman.exe -f bogus.pen 2>&1 | grep 'incorrect file' */
+/* test: pacman.exe -f nofile.pac 2>&1 | grep "can't read file" */
+/* test-: pacman.exe -f bogus.pac 2>&1 | grep 'incorrect file' */
 /* test: pacman.exe -h | grep usage */
 /* test: pacman.exe -r 2>&1 | grep 'missing max' */
 /* test: pacman.exe -v 2>&1 | grep missing */
 /* test: pacman.exe _ 2>&1 | grep invalid */
-/* test: { sleep 1; echo -n ijklq; sleep 1; echo q; } | pacman.exe -s 3 -b 6x10 */
+/* test: { sleep 1; echo -n ijklq; sleep 1; echo q; } | pacman.exe -b 39x20 */
 
 /* vim: set ts=4 sw=4 et: */