Guest User

Untitled

a guest
Oct 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ncurses.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. void draw_game_area ();
  8. void generate_game_area ();
  9.  
  10. struct coordinate
  11. {
  12. int x;
  13. int y;
  14. };
  15.  
  16. const struct coordinate UP = { 0, -1 };
  17. const struct coordinate DOWN = { 0, 1 };
  18. const struct coordinate RIGHT = { 1, 0 };
  19. const struct coordinate LEFT = { -1, 0 };
  20.  
  21. struct coordinate LOOK_AROUND[4] = {
  22. {1, 0},
  23. {0, -1},
  24. {-1, 0},
  25. {0, 1}
  26. };
  27.  
  28. struct coordinate player_pos;
  29. struct coordinate goal_pos;
  30.  
  31. #define GAME_AREA_WIDTH 2*49+1
  32. #define GAME_AREA_HEIGHT 2*18+1
  33.  
  34. #define OFFSET_X 3
  35. #define OFFSET_Y 3
  36.  
  37. enum
  38. { WALL, NOTHING } game_area[GAME_AREA_WIDTH][GAME_AREA_HEIGHT];
  39. bool visited[GAME_AREA_WIDTH][GAME_AREA_HEIGHT];
  40.  
  41. void
  42. screen_init (void)
  43. {
  44. initscr ();
  45. noecho ();
  46. cbreak ();
  47. keypad (stdscr, TRUE);
  48. refresh ();
  49. }
  50.  
  51. void
  52. DFS (int x, int y)
  53. { //maze generation using depth-first search
  54. if (visited[x][y])
  55. return;
  56. draw_game_area ();
  57. refresh ();
  58. usleep (20000);
  59. visited[x][y] = TRUE;
  60. game_area[x][y] = NOTHING;
  61. char unvisited_neighbour_counter;
  62. do
  63. {
  64. unvisited_neighbour_counter = 0;
  65. int i;
  66. for (i = 0; i < 4; i++)
  67. {
  68. int _x = x + (2 * LOOK_AROUND[i].x);
  69. int _y = y + (2 * LOOK_AROUND[i].y);
  70. if (GAME_AREA_HEIGHT > _y && _y > 0 && GAME_AREA_WIDTH > _x
  71. && _x > 0 && visited[_x][_y] == FALSE)
  72. {
  73. unvisited_neighbour_counter++;
  74. }
  75. }
  76. if (unvisited_neighbour_counter > 0)
  77. {
  78. int internal_counter = -1;
  79. int j = -1;
  80. char choice = rand () % (unvisited_neighbour_counter + 1);
  81. do
  82. {
  83. int _x = x + (2 * LOOK_AROUND[j].x);
  84. int _y = y + (2 * LOOK_AROUND[j].y);
  85. if (GAME_AREA_HEIGHT > _y && _y > 0 && GAME_AREA_WIDTH > _x
  86. && _x > 0 && visited[_x][_y] == FALSE)
  87. internal_counter++;
  88. j++;
  89.  
  90. }
  91. while (internal_counter != choice - 1);
  92. j--;
  93. game_area[x + LOOK_AROUND[j].x][y + LOOK_AROUND[j].y] = NOTHING;
  94. DFS (x + (2 * LOOK_AROUND[j].x), y + (2 * LOOK_AROUND[j].y));
  95. }
  96. }
  97. while (unvisited_neighbour_counter > 0);
  98. }
  99.  
  100. void
  101. generate_game_area ()
  102. {
  103. int i, j;
  104. for (i = 0; i < GAME_AREA_WIDTH; i++)
  105. {
  106. for (j = 0; j < GAME_AREA_HEIGHT; j++)
  107. {
  108. game_area[i][j] = WALL;
  109. visited[i][j] = FALSE;
  110. }
  111. }
  112. srand (time (NULL));
  113.  
  114. player_pos.x = GAME_AREA_WIDTH - 2;
  115. player_pos.y = GAME_AREA_HEIGHT - 2;
  116. goal_pos.x = 1;
  117. goal_pos.y = 1;
  118. draw_game_area ();
  119. usleep (1000);
  120. DFS (1, 1);
  121. }
  122.  
  123. void
  124. draw_game_area ()
  125. {
  126. int i, j;
  127. for (i = 0; i < GAME_AREA_WIDTH; i++)
  128. {
  129. for (j = 0; j < GAME_AREA_HEIGHT; j++)
  130. {
  131. move (j + OFFSET_X, i + OFFSET_Y);
  132. if (game_area[i][j] == WALL)
  133. addch (' ' | A_REVERSE);
  134. else
  135. addch (' ');
  136. }
  137. }
  138. move (OFFSET_X + 1, OFFSET_Y + 1);
  139. addch (ACS_BOARD | A_BLINK);
  140. }
  141.  
  142. void
  143. draw_player ()
  144. {
  145. move (player_pos.y + OFFSET_Y, player_pos.x + OFFSET_X);
  146. addch (ACS_DIAMOND);
  147. }
  148.  
  149. void
  150. game_move (struct coordinate movement)
  151. {
  152. if (game_area[player_pos.x + movement.x][player_pos.y + movement.y] ==
  153. NOTHING)
  154. {
  155. mvprintw (player_pos.y + OFFSET_Y, player_pos.x + OFFSET_X, " ");
  156. player_pos.x += movement.x;
  157. player_pos.y += movement.y;
  158. draw_player ();
  159. //mvprintw(1, 0, "x: %d, y: %d ", player_pos.x, player_pos.y);
  160. }
  161. move (0, 0);
  162. }
  163.  
  164. int
  165. main ()
  166. {
  167. screen_init ();
  168. curs_set (0);
  169. mvprintw (0, 0, "Labirintus");
  170. while (1)
  171. {
  172. while (1)
  173. {
  174. generate_game_area ();
  175. sleep (1);
  176. }
  177. draw_game_area ();
  178. draw_player ();
  179. refresh ();
  180. do
  181. {
  182. int c = wgetch (stdscr);
  183. switch (c)
  184. {
  185. case KEY_UP:
  186. game_move (UP);
  187. break;
  188. case KEY_DOWN:
  189. game_move (DOWN);
  190. break;
  191. case KEY_RIGHT:
  192. game_move (RIGHT);
  193. break;
  194. case KEY_LEFT:
  195. game_move (LEFT);
  196. break;
  197. }
  198.  
  199. }
  200. while ((goal_pos.x != player_pos.x) || (goal_pos.y != player_pos.y));
  201. }
  202. clrtoeol ();
  203. refresh ();
  204. endwin ();
  205. return 0;
  206. }
Add Comment
Please, Sign In to add comment