웹을 통해 검색 한 후 도움이되는 답변을 찾지 못했습니다. 내 프로그램이 C 언어입니다.무작위로 항목을지도에 배치하는 방법은 무엇입니까?
나는 목록 (예 : 37a, 22b, 29c, 13d, 19e, 2f 및 0g)이 있으며, 항목을지도에 표시합니다 (필자의 경우 int [height] [lenght] [7]). 나는 항목을 배치하고 전체 항목을 배치 할 때까지 루프를 사용한다고 생각했지만 너무 많은 시간과 자원을 사용합니다.
쉽고 올바르게 배치 할 수있는 방법이 있습니까?
/* allocates the tab in order to place the ressources/items */
void create_map(t_world *world)
{
unsigned int x;
unsigned int y;
x = 0;
world->map = xmalloc(world->height * sizeof(int**));
while (x < world->height)
{
y = 0;
world->map[x] = xmalloc(world->lenght * sizeof(int*));
while (y < world->lenght)
{
world->map[x][y] = xmalloc(7 * sizeof(int));
bzero(world->map[x][y], 7);
++y;
}
++x;
}
}
/* base defining the number of required item */
t_elevation elevation_tab[] =
{
{1, {0, 1, 0, 0, 0, 0, 0} },
{2, {0, 1, 1, 1, 0, 0, 0} },
{2, {0, 2, 0, 1, 0, 2, 0} },
{4, {0, 1, 1, 2, 0, 1, 0} },
{4, {0, 1, 2, 1, 3, 0, 0} },
{6, {0, 1, 2, 3, 0, 1, 0} },
{6, {0, 2, 2, 2, 2, 2, 1} }
};
/* calculates the number of item required */
unsigned int *calc_elevation(t_world *world)
{
unsigned int i;
unsigned int pos;
unsigned int *tab;
i = 0;
tab = xmalloc(7 * sizeof(int));
bzero(tab, 7);
while (i < 7)
{
pos = 0;
while (pos < 7)
{
tab[pos] += (world->population/
elevation_tab[i].required_players +
world->population %
elevation_tab[i].required_players) *
(elevation_tab[i].required_ressources[pos]);
++pos;
}
++i;
}
return (tab);
}
void place_ressources(t_world *world, unsigned int *ressources)
{
//here is my missing code
}
/*First called function*/
void create_world(t_param *params, t_world *world)
{
unsigned int *ressources_needed;
world->lenght = params->lenght;
world->height = params->height;
world->population = params->team_size * 2;
create_map(world);
ressources_needed = calc_elevation(world);
place_ressources(world, ressources_needed);
show_map(world);
world->players = NULL;
free(ressources_needed);
}
세계는 heigth와 아이폰에 사용하여 사용자에 의해 결정 특정 크기의 격자이다
여기 내 코드입니다. 그리드의 각 경우에는 각 항목의 양을 전달하는 int [7]가 있습니다. 그래서 같은 케이스에 여러 항목을 배치 할 수 있습니다.
당신은 지금까지 어떤 코드를 가지고 있니? – Eregrith
높이와 길이는 무엇입니까? 왜이 시간이 오래 걸리나요? 이미 점령 된 사각형에 물건을 두는 것을 피해야합니까? 그렇다면, 어떻게 할 것입니까? –
소식이 수정되었습니다. 감사합니다 – ss814