From 3b99dd82a1cadda7e130e38973fd34bfb842f1e5 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Thu, 17 Oct 2024 23:21:53 +0200 Subject: [PATCH] add board definitions --- constant.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++ constant.h | 23 +++++++++++ function.c | 7 ++++ function.h | 2 + pacman.c | 46 +++++++++++++-------- 5 files changed, 179 insertions(+), 17 deletions(-) create mode 100644 constant.c create mode 100644 constant.h diff --git a/constant.c b/constant.c new file mode 100644 index 0000000..b96873e --- /dev/null +++ b/constant.c @@ -0,0 +1,118 @@ +#include +#include + +#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 index 0000000..5a790ad --- /dev/null +++ b/constant.h @@ -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: */ diff --git a/function.c b/function.c index 0d5ef3b..6ddcac2 100644 --- a/function.c +++ b/function.c @@ -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; diff --git a/function.h b/function.h index f666f4d..f7c0f08 100644 --- a/function.h +++ b/function.h @@ -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); diff --git a/pacman.c b/pacman.c index c37fd30..5ded060 100644 --- 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 #include #include #include +#include "constant.h" #include "debug.h" #include "display.h" #include "function.h" @@ -18,17 +19,9 @@ 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 = " 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: */ -- 2.30.2