Guest User

Untitled

a guest
Oct 18th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 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.  
  7. int check_free_space(collect_t *map, collect_t *new_elem, pos_t pos)
  8. {
  9. int i, j;
  10.  
  11. for(i = 0; i < new_elem->h; i++)
  12. for(j = 0; j < new_elem->w; j++)
  13. if(new_elem->array[i][j] == '*')
  14. {
  15. if(i+pos.y < map->h && j+pos.x < map->w && i+pos.y >= 0 && j+pos.x >= 0)
  16. {
  17. if(map->array[i+pos.y][j+pos.x] != '.')
  18. return (-1);
  19. }
  20. else
  21. return (-1);
  22. }
  23. return (0);
  24. }
  25.  
  26. int check_connection(collect_t *map, collect_t *new_elem, pos_t pos, char symbol)
  27. {
  28. int i, j;
  29.  
  30. for(i = 0; i < new_elem->h; i++)
  31. {
  32. for(j = 0; j < new_elem->w; j++)
  33. {
  34. if(new_elem->array[i][j] != '.')
  35. {
  36. if(i+pos.y+1 < map->h && map->array[i+pos.y+1][j+pos.x] == symbol)
  37. return (0);
  38. if (i+pos.y-1 >= 0 && map->array[i+pos.y-1][j+pos.x] == symbol)
  39. return (0);
  40. if (j+pos.x+1 < map->w && map->array[i+pos.y][j+pos.x+1] == symbol)
  41. return (0);
  42. if (j+pos.x-1 >= 0 && map->array[i+pos.y][j+pos.x-1] == symbol)
  43. return (0);
  44. }
  45. }
  46. }
  47. return (-1);
  48. }
  49.  
  50. pos_t play(req_t *core)
  51. {
  52. pos_t res;
  53. int i, j;
  54.  
  55. printlog("filler.log", "a","H = %d, W = %d", core->map.h, core->map.w);
  56. for (i = 0; i < core->map.h; i++)
  57. {
  58. for (j = 0; j < core->map.w; j++)
  59. {
  60. printlog("filler.log", "a", "play on\n");
  61. res.x = j, res.y = i;
  62. if (!check_free_space(&(core->map), &(core->elem), res) && !check_connection(&(core->map), &(core->elem), res, core->symbol))
  63. {
  64. printlog("filler.log", "a", "position = %d %d\n", res.x, res.y);
  65. return (res);
  66. }
  67. }
  68. }
  69. return (res);
  70. }
Add Comment
Please, Sign In to add comment