From 04aaf04623641fcaf0381f0e2d451f41b49b4c30 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sun, 13 Oct 2024 09:36:45 +0200 Subject: [PATCH] add pacman moves --- display.c | 94 +++++++++++++++++++++++++++++++----------------------- display.h | 2 ++ function.c | 39 ++++++++++++++++++++-- function.h | 2 ++ pacman.c | 62 ++++++++++++++++++++++++----------- 5 files changed, 138 insertions(+), 61 deletions(-) diff --git a/display.c b/display.c index eaddbf3..59b23bc 100644 --- a/display.c +++ b/display.c @@ -66,9 +66,59 @@ int helpwindow (char *msg, int xoffset, int yoffset) return j; } +void displayelement (board_t *board, int x, int y) +{ + int element = ' '; + switch (*getcell (board, x, y)) { + case '-': + element = ACS_HLINE; + break; + case '|': + element = ACS_VLINE; + break; + case 'L': + element = ACS_LLCORNER; + break; + case 'J': + element = ACS_LRCORNER; + break; + case '7': + element = ACS_URCORNER; + break; + case 'F': + element = ACS_ULCORNER; + break; + case 'T': + element = ACS_TTEE; + break; + case 'E': + element = ACS_LTEE; + break; + case '#': + element = ACS_BTEE; + break; + case '3': + element = ACS_RTEE; + break; + case '+': + element = ACS_PLUS; + break; + case 'C': + setcolor (yellow_black); + element = 'C'; + break; + case 'O': + setcolor (yellow_black); + element = 'O'; + break; + } + mvaddch (board->yoffset + y, board->xoffset + x, element); + setcolor (gray_black); +} + void boardwindow (board_t *board, int mode) { - int i, j; + int x, y; //setcolor (mode ? gray_black : black_gray); if (mode) { @@ -76,45 +126,9 @@ void boardwindow (board_t *board, int mode) } //setcolor (gray_black); - for (i = 0; i < board->width; i++) { - for (j = 0; j < board->height; j++) { - int element = ' '; - switch (*getcell (board, i, j)) { - case '-': - element = ACS_HLINE; - break; - case '|': - element = ACS_VLINE; - break; - case 'L': - element = ACS_LLCORNER; - break; - case 'J': - element = ACS_LRCORNER; - break; - case '7': - element = ACS_URCORNER; - break; - case 'F': - element = ACS_ULCORNER; - break; - case 'T': - element = ACS_TTEE; - break; - case 'E': - element = ACS_LTEE; - break; - case '#': - element = ACS_BTEE; - break; - case '3': - element = ACS_RTEE; - break; - case '+': - element = ACS_PLUS; - break; - } - mvaddch (board->yoffset + j, board->xoffset + i, element); + for (x = 0; x < board->width; x++) { + for (y = 0; y < board->height; y++) { + displayelement (board, x, y); } } } diff --git a/display.h b/display.h index b800d35..5cb101c 100644 --- a/display.h +++ b/display.h @@ -8,6 +8,8 @@ int helpwindow (char *msg, int xoffset, int yoffset); +void displayelement (board_t *board, int x, int y); + void boardwindow (board_t *board, int mode); char *savewindow (int length, int xoffset, int yoffset); diff --git a/function.c b/function.c index 7b3fd88..d095eb2 100644 --- a/function.c +++ b/function.c @@ -146,6 +146,7 @@ board_t *loadboard (char *str) int width = 0; int height = 0; char *tab = NULL; + char *last = NULL; char *saveptr1, *saveptr2; char *line = strtok_r (str, "\n", &saveptr1); @@ -160,14 +161,30 @@ board_t *loadboard (char *str) if (strcmp (keyword, "width") == 0) { width = atoi (value); + last = NULL; } else if (strcmp (keyword, "height") == 0) { height = atoi (value); + last = NULL; } else if (strcmp (keyword, "tab") == 0) { - tab = atos (value); + tab = strdup (atos (value)); + last = tab; } else if (strcmp (keyword, "rem") == 0) { /* nothing to do with remark */ + last = NULL; } else { - VERBOSE (WARNING, printf ("unknown keyword: %s\n", keyword)); + while (*keyword == ' ') { + keyword++; + } + if (*keyword == '"') { + char *tmp = atos (keyword); + if ((last) && (last == tab)) { + last = tab = (char *) realloc (tab, strlen (tab) + strlen (tmp) + 1); + strcpy (tab + strlen (tab), tmp); + } + } else { + VERBOSE (WARNING, printf ("unknown keyword: %s\n", keyword)); + last = NULL; + } } line = strtok_r (NULL, "\n", &saveptr1); @@ -178,8 +195,26 @@ board_t *loadboard (char *str) board = initboard (width, height); memcpy (board->tab, tab, width * height); } + free (tab); return board; } +int findchar (board_t *board, char c, int *px, int *py) +{ + int i, j; + int ret = 0; + for (i = 0; i < board->width; i++) { + for (j = 0; j < board->height; j++) { + if (*getcell (board, i, j) == c) { + *px = i; + *py = j; + ret = 1; + break; + } + } + } + return ret; +} + /* vim: set ts=4 sw=4 et: */ diff --git a/function.h b/function.h index 0483576..d5926d4 100644 --- a/function.h +++ b/function.h @@ -39,6 +39,8 @@ char *atos (char *str); board_t *loadboard (char *str); +int findchar (board_t *board, char c, int *px, int *py); + #endif /* __FUNCTION_H__ */ /* vim: set ts=4 sw=4 et: */ diff --git a/pacman.c b/pacman.c index 2655257..d642d00 100644 --- a/pacman.c +++ b/pacman.c @@ -1,8 +1,8 @@ /* depend: */ /* cflags: */ -/* linker: color.o debug.o display.o function.o -lcurses */ -/* doslnk: color.o debug.o display.o function.o -lpdc~1 */ -/* winlnk: color.o debug.o display.o function.o -lpdcurses */ +/* 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 */ #include #include @@ -11,6 +11,7 @@ #include "debug.h" #include "display.h" #include "function.h" +#include "time.h" /* static variables */ char *progname = NULL; @@ -141,15 +142,41 @@ int main (int argc, char *argv[]) int ysave = yboard + (board->height - 1) / 2; char *savename = NULL; - /* cursor definition */ - int xcursor = board->width / 2; - int ycursor = board->height / 2; + /* pacman position */ + int x = 0; + int y = 0; + if (findchar (board, 'C', &x, &y)) { + VERBOSE (WARNING, printf ("can't find Pacman\n")); + } + int nx = x; + int ny = y; /* event loop */ int stop = 0; + int draw = 1; while (!stop) { - boardwindow (board, 0); + /* draw board */ + if (draw) { + boardwindow (board, 0); + draw = 0; + } + + /* move pacman */ + if ((nx != x) || (ny != y)) { + *getcell (board, x, y) = 'O'; + displayelement (board, x, y); + refresh (); + msleep (250); + *getcell (board, x, y) = ' '; + displayelement (board, x, y); + x = nx; + y = ny; + *getcell (board, x, y) = 'C'; + displayelement (board, x, y); + refresh (); + msleep (250); + } int ch = getch (); @@ -183,6 +210,7 @@ int main (int argc, char *argv[]) free (ptr); free (savename); } + draw = 1; break; } @@ -190,29 +218,25 @@ int main (int argc, char *argv[]) switch (ch) { case KEY_UP: case 'i': - if (ycursor > 0) { - ycursor--; - } + ny = ((y > 0) ? y : board->height) - 1; break; case KEY_LEFT: case 'j': - if (xcursor > 0) { - xcursor--; - } + nx = ((x > 0) ? x : board->width) - 1; break; case KEY_DOWN: case 'k': - if (ycursor < board->height) { - ycursor++; - } + ny = ((y < board->height - 1) ? y : -1) + 1; break; case KEY_RIGHT: case 'l': - if (xcursor < board->width) { - xcursor++; - } + nx = ((x < board->width - 1) ? x : -1) + 1; break; } + if (*getcell (board, nx, ny) != ' ') { + nx = x; + ny = y; + } } /* cleaning before quiting */ -- 2.30.2