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) {
}
//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);
}
}
}
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);
int width = 0;
int height = 0;
char *tab = NULL;
+ char *last = NULL;
char *saveptr1, *saveptr2;
char *line = strtok_r (str, "\n", &saveptr1);
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);
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: */
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: */
/* 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>
#include "debug.h"
#include "display.h"
#include "function.h"
+#include "time.h"
/* static variables */
char *progname = NULL;
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 ();
free (ptr);
free (savename);
}
+ draw = 1;
break;
}
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 */