Guest User

Untitled

a guest
Feb 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. /*
  2. _ _ _
  3. __| (_) ___| | _____
  4. / _` | |/ __| |/ / __|
  5. | (_| | | (__| <\__ \
  6. \__,_|_|\___|_|\_\___/
  7.  
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <ncurses.h>
  12. #include <string.h>
  13.  
  14. #define ROWS 42
  15. #define COLS 36
  16. #define PLAYER_STRING "**"
  17. #define DEFAULT_STRING " "
  18.  
  19. void mapopen(char* map[ROWS][COLS]);
  20. void printmap(char* map[ROWS][COLS]);
  21. void play(char* map[ROWS][COLS]);
  22. void walk(char* map[ROWS][COLS], char* row, char* col, char dir);
  23. char checkcollisions(char* map[ROWS][COLS], char* row, char* col, char dir);
  24.  
  25. int main() {
  26. char row, col;
  27. char* map[ROWS][COLS];
  28.  
  29. /* ncurses initialization, receives single keypresses */
  30. initscr();
  31. raw();
  32. keypad(stdscr, TRUE);
  33. noecho();
  34.  
  35. /* Start with the actual game content */
  36. mapopen(map);
  37. play(map);
  38.  
  39. /* ncurses footer */
  40. refresh();
  41. getch();
  42. endwin();
  43.  
  44. return 0;
  45. }
  46.  
  47. /* Read map from file */
  48. void mapopen(char* map[ROWS][COLS]) {
  49. FILE *mapfile = fopen("map", "r");
  50. char charbuffer[3], row, col;
  51. /* - Read 2 characters from file, assign to each map grid - */
  52. row = 0, col = 0;
  53. while (fgets(charbuffer, 3, mapfile) && row < ROWS) {
  54. if (*charbuffer != '\n') {map[row][col] = strdup(charbuffer); col++;}
  55. else {row++; col = 0;}
  56. }
  57. fclose(mapfile):
  58. }
  59.  
  60. /* Printf map */
  61. void printmap(char* map[ROWS][COLS]) {
  62. char row, col;
  63. for (row = 0; row < ROWS; row++) {
  64. for (col = 0; col < COLS; col++) {
  65. printw("%s", map[row][col]);
  66. }
  67. printw("\n");
  68. }
  69. }
  70.  
  71. /* Start game */
  72. void play(char* map[ROWS][COLS]) {
  73. char row = 0, col = 0, input = ' ';
  74. map[row][col] = PLAYER_STRING;
  75. while (true) {
  76. printmap(map);
  77. input = getch();
  78. walk(map, &row, &col, input);
  79. clear();
  80. }
  81. }
  82.  
  83. /* Move player */
  84. void walk(char* map[ROWS][COLS], char *row, char *col, char dir) {
  85. if (!checkcollisions(map, row, col, dir)) {
  86. switch (dir) {
  87. case 'w':
  88. map[*row][*col] = DEFAULT_STRING;
  89. map[--(*row)][*col] = PLAYER_STRING;
  90. break;
  91. case 'a':
  92. map[*row][*col] = DEFAULT_STRING;
  93. map[*row][--(*col)] = PLAYER_STRING;
  94. break;
  95. case 's':
  96. map[*row][*col] = DEFAULT_STRING;
  97. map[++(*row)][*col] = PLAYER_STRING;
  98. break;
  99. case 'd':
  100. map[*row][*col] = DEFAULT_STRING;
  101. map[*row][++(*col)] = PLAYER_STRING;
  102. break;
  103. }
  104. }
  105. }
  106.  
  107. /* Check map boundaries and validate movements */
  108. char checkcollisions(char* map[ROWS][COLS], char *row, char *col, char dir) {
  109. char nextrow = 0, nextcol = 0;
  110. switch (dir) {
  111. case 'w':
  112. nextrow = *row - 1;
  113. nextcol = *col;
  114. break;
  115. case 'a':
  116. nextrow = *row;
  117. nextcol = *col - 1;
  118. break;
  119. case 's':
  120. nextrow = *row + 1;
  121. nextcol = *col;
  122. break;
  123. case 'd':
  124. nextrow = *row;
  125. nextcol = *col + 1;
  126. break;
  127. }
  128. if (strcmp(map[nextrow][nextcol], DEFAULT_STRING) != 0 || nextrow > (ROWS - 1) || nextcol > (COLS - 1) || nextrow < 0 || nextcol < 0) {return 1;}
  129. return 0;
  130. }
Add Comment
Please, Sign In to add comment