Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/stat.h>
- #include "filler.h"
- #include "my_string.h"
- #include <math.h>
- unsigned int decission(collect_t *map, pos_t pos)
- {
- unsigned int des;
- if (pos.x > (map->h/2))
- des = 0;
- else
- des = 1;
- return (des);
- }
- int is_enemy(collect_t *map)
- {
- int i, j;
- for (i = 0; i < map->h; i++)
- {
- for (j = 0; j < map->w; j++)
- {
- if (map->array[i][j] == '#')
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- }
- }
- pos_t check_enemy(collect_t *map)
- {
- int i, j;
- pos_t enemy;
- for (i = 0; i < map->h; i++)
- {
- for (j = 0; j < map->w; j++)
- {
- if (map->array[i][j] == '#')
- {
- enemy.x = i, enemy.y = j;
- }
- }
- }
- return (enemy);
- }
- int check_free_space(collect_t *map, collect_t *new_elem, pos_t pos)
- {
- int i, j;
- for (i = 0; i < new_elem->h; i++)
- for (j = 0; j < new_elem->w; j++)
- if (new_elem->array[i][j] == '*')
- {
- if (i+pos.y < map->h && j+pos.x < map->w && i+pos.y >= 0 && j+pos.x >= 0)
- {
- if (map->array[i+pos.y][j+pos.x] != '.')
- return (-1);
- }
- else
- return (-1);
- }
- return (0);
- }
- int check_connection(collect_t *map, collect_t *new_elem, pos_t pos, char symbol)
- {
- int i, j;
- for (i = 0; i < new_elem->h; i++)
- {
- for (j = 0; j < new_elem->w; j++)
- {
- if (new_elem->array[i][j] != '.')
- {
- if (i+pos.y+1 < map->h && map->array[i+pos.y+1][j+pos.x] == symbol)
- return (0);
- if (i+pos.y-1 >= 0 && map->array[i+pos.y-1][j+pos.x] == symbol)
- return (0);
- if (j+pos.x+1 < map->w && map->array[i+pos.y][j+pos.x+1] == symbol)
- return (0);
- if (j+pos.x-1 >= 0 && map->array[i+pos.y][j+pos.x-1] == symbol)
- return (0);
- }
- }
- }
- return (-1);
- }
- pos_t play(req_t *core)
- {
- unsigned int des;
- pos_t res, enemy;
- int i, j;
- enemy = check_enemy(&(core->map));
- printlog("filler.log", "a","H = %d, W = %d", core->map.h, core->map.w);
- switch (decission(&(core->map), res))
- {
- case 1:
- for (i = 0; i < core->map.h; i++)
- {
- for (j = 0; j < core->map.w; j++)
- {
- if (is_enemy(&(core->map)))
- {
- printlog("enemy.log", "w", "position = %d %d\n", enemy.x, enemy.y);
- res.x = abs(enemy.x - j);
- res.y = abs(enemy.y - i);
- }
- else
- {
- res.x = j;
- res.y = i;
- }
- if (!check_free_space(&(core->map), &(core->elem), res) && !check_connection( &(core->map), &(core->elem), res, core->symbol))
- {
- printlog("filler.log", "a", "position = %d %d\n", res.x, res.y);
- return res;
- }
- }
- }
- case 0:
- for (i = core->map.h; i > 0; i--)
- {
- for (j = 0; j < core->map.w; j++)
- {
- if (is_enemy(&(core->map)))
- {
- printlog("enemy.log", "w", "position = %d %d\n", enemy.x, enemy.y);
- res.x = abs(enemy.x - j);
- res.y = abs(enemy.y - i);
- }
- else
- {
- res.x = j;
- res.y = i;
- }
- if (!check_free_space(&(core->map), &(core->elem), res) && !check_connection(&(core->map), &(core->elem), res, core->symbol))
- {
- printlog("filler.log", "a", "position = %d %d\n", res.x, res.y);
- return res;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment