Guest User

Untitled

a guest
Oct 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/stat.h>
  4. #include "filler.h"
  5. #include "my_string.h"
  6. #include <math.h>
  7.  
  8. unsigned int decission(collect_t *map, pos_t pos)
  9. {
  10. unsigned int des;
  11.  
  12. if (pos.x > (map->h/2))
  13. des = 0;
  14. else
  15. des = 1;
  16. return (des);
  17. }
  18.  
  19. int is_enemy(collect_t *map)
  20. {
  21. int i, j;
  22.  
  23. for (i = 0; i < map->h; i++)
  24. {
  25. for (j = 0; j < map->w; j++)
  26. {
  27. if (map->array[i][j] == '#')
  28. {
  29. return 0;
  30. }
  31. else
  32. {
  33. return 1;
  34. }
  35. }
  36. }
  37. }
  38.  
  39. pos_t check_enemy(collect_t *map)
  40. {
  41. int i, j;
  42. pos_t enemy;
  43.  
  44. for (i = 0; i < map->h; i++)
  45. {
  46. for (j = 0; j < map->w; j++)
  47. {
  48. if (map->array[i][j] == '#')
  49. {
  50. enemy.x = i, enemy.y = j;
  51. }
  52. }
  53. }
  54. return (enemy);
  55. }
  56.  
  57.  
  58. int check_free_space(collect_t *map, collect_t *new_elem, pos_t pos)
  59. {
  60. int i, j;
  61.  
  62. for (i = 0; i < new_elem->h; i++)
  63. for (j = 0; j < new_elem->w; j++)
  64. if (new_elem->array[i][j] == '*')
  65. {
  66. if (i+pos.y < map->h && j+pos.x < map->w && i+pos.y >= 0 && j+pos.x >= 0)
  67. {
  68. if (map->array[i+pos.y][j+pos.x] != '.')
  69. return (-1);
  70. }
  71. else
  72. return (-1);
  73. }
  74. return (0);
  75. }
  76.  
  77. int check_connection(collect_t *map, collect_t *new_elem, pos_t pos, char symbol)
  78. {
  79. int i, j;
  80.  
  81. for (i = 0; i < new_elem->h; i++)
  82. {
  83. for (j = 0; j < new_elem->w; j++)
  84. {
  85. if (new_elem->array[i][j] != '.')
  86. {
  87. if (i+pos.y+1 < map->h && map->array[i+pos.y+1][j+pos.x] == symbol)
  88. return (0);
  89. if (i+pos.y-1 >= 0 && map->array[i+pos.y-1][j+pos.x] == symbol)
  90. return (0);
  91. if (j+pos.x+1 < map->w && map->array[i+pos.y][j+pos.x+1] == symbol)
  92. return (0);
  93. if (j+pos.x-1 >= 0 && map->array[i+pos.y][j+pos.x-1] == symbol)
  94. return (0);
  95. }
  96. }
  97. }
  98. return (-1);
  99. }
  100.  
  101. pos_t play(req_t *core)
  102. {
  103. unsigned int des;
  104. pos_t res, enemy;
  105. int i, j;
  106.  
  107. enemy = check_enemy(&(core->map));
  108. printlog("filler.log", "a","H = %d, W = %d", core->map.h, core->map.w);
  109. switch (decission(&(core->map), res))
  110. {
  111. case 1:
  112. for (i = 0; i < core->map.h; i++)
  113. {
  114. for (j = 0; j < core->map.w; j++)
  115. {
  116. if (is_enemy(&(core->map)))
  117. {
  118. printlog("enemy.log", "w", "position = %d %d\n", enemy.x, enemy.y);
  119. res.x = abs(enemy.x - j);
  120. res.y = abs(enemy.y - i);
  121. }
  122. else
  123. {
  124. res.x = j;
  125. res.y = i;
  126. }
  127. if (!check_free_space(&(core->map), &(core->elem), res) && !check_connection( &(core->map), &(core->elem), res, core->symbol))
  128. {
  129. printlog("filler.log", "a", "position = %d %d\n", res.x, res.y);
  130. return res;
  131. }
  132. }
  133. }
  134. case 0:
  135. for (i = core->map.h; i > 0; i--)
  136. {
  137. for (j = 0; j < core->map.w; j++)
  138. {
  139. if (is_enemy(&(core->map)))
  140. {
  141. printlog("enemy.log", "w", "position = %d %d\n", enemy.x, enemy.y);
  142. res.x = abs(enemy.x - j);
  143. res.y = abs(enemy.y - i);
  144. }
  145. else
  146. {
  147. res.x = j;
  148. res.y = i;
  149. }
  150. if (!check_free_space(&(core->map), &(core->elem), res) && !check_connection(&(core->map), &(core->elem), res, core->symbol))
  151. {
  152. printlog("filler.log", "a", "position = %d %d\n", res.x, res.y);
  153. return res;
  154. }
  155. }
  156. }
  157. }
  158. }
Add Comment
Please, Sign In to add comment