pepper2.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include <getopt.h>
  2. #include <curses.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <malloc_np.h>
  6. #include <string.h>
  7. #include <time.h>
  8. /* cc pepper2.c -lncurses -o p2 -O2 */
  9. typedef struct {
  10. size_t len;
  11. char *items[10];
  12. } truc_array;
  13. enum dirs {
  14. R, /* right */
  15. L, /* left */
  16. U, /* up */
  17. D /* down */
  18. };
  19. static size_t delay = 150;
  20. /* result window */
  21. static WINDOW * winR;
  22. /* sprite */
  23. static void worm(WINDOW *win, int x, int y, char c0, char repl)
  24. {
  25. static int ax, ay;
  26. static char ac = '*';
  27. wattron(win, A_REVERSE);
  28. mvwaddch(win, y + 1, x + 1, c0);
  29. wattroff(win, A_REVERSE);
  30. if (ac != '*') {
  31. int colnum = 4;
  32. if (ac != '.')
  33. colnum = 1;
  34. wattron(win, COLOR_PAIR(colnum));
  35. mvwaddch(win, ay, ax, ac);
  36. wattroff(win, COLOR_PAIR(colnum));
  37. }
  38. wrefresh(win);
  39. ax = x + 1;
  40. ay = y + 1;
  41. ac = repl;
  42. usleep(delay * 1000);
  43. }
  44. /* path lookup */
  45. static __attribute((malloc)) __attribute__((nonnull)) char *get_it(truc_array const *src)
  46. {
  47. char *r = mallocx(src->len, MALLOCX_ZERO);
  48. size_t n = 0;
  49. enum dirs dir = R;
  50. int x = 0, y = 0;
  51. char c0 = '<';
  52. WINDOW * win = newwin(12, 17, 1, 49);
  53. curs_set(0);
  54. box(win, 0, 0);
  55. while (src->len && (n < src->len)) {
  56. char c;
  57. while((c = src->items[y][x]) != ' ') {
  58. /* store */
  59. if (c != '*') {
  60. worm(win, x, y, c0, c);
  61. if (n > 0)
  62. mvwaddch(winR, 1, n , r[n - 1]);
  63. r[n++] = c;
  64. wattron (winR, COLOR_PAIR(2));
  65. mvwaddch(winR, 1, n, c);
  66. wattroff(winR, COLOR_PAIR(2));
  67. wrefresh(winR);
  68. } else
  69. worm(win, x, y, c0, '.');
  70. if (n == src->len) {
  71. break;
  72. }
  73. /* test next one */
  74. int tx = x;
  75. int ty = y;
  76. switch (dir) {
  77. case R:
  78. tx++;
  79. break;
  80. case L:
  81. tx--;
  82. break;
  83. case U:
  84. ty--;
  85. break;
  86. case D:
  87. ty++;
  88. break;
  89. default:
  90. fprintf(stderr, "Bug!\n");
  91. break;
  92. }
  93. /* ouch */
  94. if (tx > 14 || tx < 0 || ty <0 || ty > 9)
  95. break;
  96. if ((c = src->items[ty][tx]) == ' ') {
  97. wattron(win,COLOR_PAIR(3));
  98. mvwaddch(win, ty + 1, tx + 1, c);
  99. wattroff(win, COLOR_PAIR(3));
  100. break;
  101. }
  102. /* valid next */
  103. x = tx;
  104. y = ty;
  105. }
  106. switch (dir) {
  107. char c_up, c_down, c_left, c_right;
  108. case R:
  109. case L:
  110. /* cover up and down */
  111. c_up = ' ';
  112. c_down = ' ';
  113. if (y > 1)
  114. c_up = src->items[y - 1][x];
  115. if (y < 9)
  116. c_down = src->items[y + 1][x];
  117. if (c_up != ' ') {
  118. y = y - 1;
  119. dir = U;
  120. c0 = '^';
  121. }
  122. else if (c_down != ' ') {
  123. y = y + 1;
  124. dir = D;
  125. c0 = ';';
  126. }
  127. else
  128. goto end;
  129. break;
  130. case U:
  131. case D:
  132. /* cover left and right */
  133. c_left = ' ';
  134. c_right = ' ';
  135. if (x > 1)
  136. c_left = src->items[y][x - 1];
  137. if (y < 14)
  138. c_right = src->items[y][x + 1];
  139. if (c_left != ' ') {
  140. x = x - 1;
  141. dir = L;
  142. c0 = '>';
  143. }
  144. else if (c_right != ' ') {
  145. x = x + 1;
  146. dir = R;
  147. c0 = '<';
  148. } else
  149. goto end;
  150. break;
  151. }
  152. }
  153. end:
  154. worm(win, x , y , ' ', ' ');
  155. if (n > 1) {
  156. mvwaddch(winR, 1, n, r[n -1]);
  157. wrefresh(winR);
  158. }
  159. delwin(win);
  160. return r;
  161. }
  162. /* puzzle display */
  163. static void print_it(truc_array src)
  164. {
  165. WINDOW * win = newwin(12, 17, 1, 1);
  166. box(win, 0, 0);
  167. for (size_t j= 0; j < 10; ++j) {
  168. mvwprintw(win, j + 1, 1, "%s", src.items[j]);
  169. }
  170. wrefresh(win);
  171. delwin(win);
  172. }
  173. int main(int argc, char *argv[])
  174. {
  175. truc_array a= {60, };
  176. char *r = NULL;
  177. static struct option longopts[] = {
  178. { "delay", required_argument, NULL, 'd' },
  179. { NULL, 0, NULL, 0 }
  180. };
  181. int ch;
  182. while ((ch = getopt_long(argc, argv, "d:", longopts, NULL)) != -1) {
  183. switch (ch) {
  184. case 'd':
  185. delay = strtoul(optarg, NULL, 10);
  186. break;
  187. case 0:
  188. /* long option */
  189. break;
  190. default:
  191. break;
  192. }
  193. }
  194. /* 012345789ABCDEF */
  195. a.items[0] = "*01** $";
  196. a.items[1] = "edcBA ";
  197. a.items[2] = "* * X ";
  198. a.items[3] = "* ! * ";
  199. a.items[4] = "****F** ";
  200. a.items[5] = " * *H* ";
  201. a.items[6] = " *G** * ";
  202. a.items[7] = " X I ";
  203. a.items[8] = " X **J**** ";
  204. a.items[9] = "X Z $";
  205. initscr();
  206. nonl();
  207. noecho();
  208. winR = newwin(3, 65, 13, 1);
  209. box(winR, 0, 0);
  210. wrefresh(winR);
  211. start_color();
  212. init_pair(1, COLOR_RED, COLOR_GREEN);
  213. init_pair(2, COLOR_BLACK, COLOR_MAGENTA);
  214. init_pair(3, COLOR_BLUE, COLOR_BLUE);
  215. init_pair(4, COLOR_YELLOW, COLOR_GREEN);
  216. print_it(a);
  217. r = get_it(&a);
  218. dallocx(r, 0);
  219. delwin(winR);
  220. endwin();
  221. }