add pacman moves
authorLaurent Mazet <mazet@softndesign.org>
Sun, 13 Oct 2024 07:36:45 +0000 (09:36 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 13 Oct 2024 07:36:45 +0000 (09:36 +0200)
display.c
display.h
function.c
function.h
pacman.c

index eaddbf30f9b99566833c420640168344e9a434b5..59b23bccb1e321c324badbb6d1af4ece3ea41645 100644 (file)
--- 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);
         }
     }
 }
index b800d356204b0b95e510c7ade1e443029f110471..5cb101c6e120e107d2b749c0f2f7a6866602f34e 100644 (file)
--- 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);
index 7b3fd885bfa138a1130e2e4bd9ee953db1500712..d095eb2a30192726f917702bf0313bbbfdc1d885 100644 (file)
@@ -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: */
index 0483576b94d172714feb75465b45eacd3558671a..d5926d4706687b85c12d49415a3f012e2dd9d21f 100644 (file)
@@ -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: */
index 2655257ee2d43a64eb53ade71b3ce39cdc1d5cdc..d642d007d78642a73f82e56fb340ae67d54306d0 100644 (file)
--- 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 <curses.h>
 #include <stdio.h>
@@ -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 */