Advertisement
cd62131

Field map example

Jul 6th, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. // main.c
  2. #include <stdbool.h>
  3. #include "map.h"
  4.  
  5. int main(void) {
  6.   map_init(true);
  7.   map_display();
  8.   map_put(MAP_HORIZONTAL / 2, MAP_VERTICAL / 2, 'x');
  9.   map_display();
  10.   map_move(MAP_HORIZONTAL / 2, MAP_VERTICAL / 2, 0, 0);
  11.   map_display();
  12.   map_delete();
  13. }
  14.  
  15. // map.h
  16. #ifndef MAP_H_
  17. #define MAP_H_
  18. #include <stdbool.h>
  19. #include <stddef.h>
  20. #define MAP_HORIZONTAL 7
  21. #define MAP_VERTICAL 7
  22. void map_init(bool);
  23. void map_delete(void);
  24. void map_display(void);
  25. void map_put(size_t, size_t, char);
  26. char map_get(size_t, size_t);
  27. void map_move(size_t, size_t, size_t, size_t);
  28. #endif /* MAP_H_ */
  29.  
  30. // map_inner.h
  31. #ifndef MAP_INNER_H_
  32. #define MAP_INNER_H_
  33. #ifdef IN_MAP
  34. #include <stdbool.h>
  35. #include <stddef.h>
  36. static char *map;
  37. static bool map_invalid_coord(size_t, size_t);
  38. #endif /* IN_MAP */
  39. #endif /* MAP_INNER_H_ */
  40.  
  41. // map.c
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #define IN_MAP
  45. #include "map.h"
  46. #include "map_inner.h"
  47.  
  48. void map_init(bool force) {
  49.   if (!force) {
  50.     return;
  51.   }
  52.   map = malloc(MAP_HORIZONTAL * MAP_VERTICAL);
  53.   char c;
  54.   for (size_t j = 0; j < MAP_VERTICAL; ++j) {
  55.     for (size_t i = 0; i < MAP_HORIZONTAL; ++i) {
  56.       if (j == 0 || j == MAP_VERTICAL - 1) {
  57.         c = '*';
  58.       } else if (i == 0 || i == MAP_HORIZONTAL - 1) {
  59.         c = '*';
  60.       } else {
  61.         c = ' ';
  62.       }
  63.       map_put(i, j, c);
  64.     }
  65.   }
  66. }
  67.  
  68. void map_delete(void) {
  69.   free(map);
  70. }
  71.  
  72. void map_put(size_t x, size_t y, char c) {
  73.   if (map_invalid_coord(x, y)) {
  74.     return;
  75.   }
  76.   map[y * MAP_HORIZONTAL + x] = c;
  77. }
  78.  
  79. void map_display(void) {
  80.   for (size_t j = 0; j < MAP_VERTICAL; ++j) {
  81.     for (size_t i = 0; i < MAP_HORIZONTAL; ++i) {
  82.       printf("%c", map_get(i, j));
  83.     }
  84.     puts("");
  85.   }
  86.   puts("");
  87. }
  88.  
  89. char map_get(size_t x, size_t y) {
  90.   if (map_invalid_coord(x, y)) {
  91.     return '#'; // for error
  92.   }
  93.   return map[y * MAP_HORIZONTAL + x];
  94. }
  95.  
  96. void map_move(size_t x1, size_t y1, size_t x2, size_t y2) {
  97.   if (map_invalid_coord(x1, y1) || map_invalid_coord(x2, y2)) {
  98.     return;
  99.   }
  100.   map_put(x2, y2, map_get(x1, y1));
  101.   map_put(x1, y1, ' ');
  102. }
  103.  
  104. static bool map_invalid_coord(size_t x, size_t y) {
  105.   return x < 0 && MAP_HORIZONTAL <= x && y < 0 && MAP_VERTICAL <= y;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement