1

ウェブを検索した後、私は私を助けるための答えを見つけられませんでした。私のプログラムはCです。

アイテムのリスト(例:37 a、22 b、29 c、13 d、19 e、2 f、0 g)があり、それらすべてのアイテムをマップ上にランダムに配置する必要があります(私の場合、int [高さ][長さ][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);
}

世界は、ユーザーが高さと長さで決める一定の大きさのグリッドです。グリッドの各ケースには、各アイテムの量を提供するint[7]があります。だから、同じケースに複数のアイテムを置くことができます。

4

1 に答える 1

0

この問題を解決するには2つの方法が考えられますが、処理する必要のあるアイテムの数によって異なります。

私が最初に考えたのは、コンピュータープログラムでトランプのデッキをシャッフルする方法に似た解決策でした。最初の要素を取り、x、y座標をランダムに生成します。次の要素を取り、x、y座標を生成します。そこにアイテムがない場合は、その位置にアイテムを配置します。そうでない場合は、新しいx、y座標を生成します。配置するすべての要素についてこれを続けます。

私が考えることができるもう1つのことは、ランダムに生成された座標が複製されないように、どのワールドタイルが使用されたかを何らかの方法で追跡できるかどうかです。

ワールドオブジェクトが3D配列であるという事実は、計算を指数関数的に増加させているものです。絶対に必要ですか?確かに、私はあなたのプログラムがどのようなコンテキストのために設計されているのか正確にはわかりません

于 2012-06-11T18:40:28.923 に答える