1

我正在构建一个与空间相关的游戏,但不知道如何遍历我的百分比“生成”机会数组,最终得到一个具有一定大小的生成星。

它必须如何生成的示例:

$star_chance = rand(1,100);
$star_size_chance = rand(1,100);
//loop through the list of stars here
//selected star - orange
    //loop through the size chances of star orange
    //selected star size = medium
  1. 红色、橙色和黄色星星各有一个生成机会,一次只能生成一个
  2. 对于每个生成的恒星,它都有可能变成什么大小
  3. 该方程需要适应可能更多的恒星或尺寸

    $star_array = array(
    "red" => array(
        "spawn_chance" => "25",
        "size_immense_chance" => "50",
        "size_giant_chance" => "25",
        "size_large_chance" => "15",
        "size_medium_chance" => "5",
        "size_small_chance" => "4",
        "size_dwarf_chance" => "1"
        ),
    "orange" => array(
        "spawn_chance" => "50",
        "size_immense_chance" => "4",
        "size_giant_chance" => "15",
        "size_large_chance" => "50",
        "size_medium_chance" => "25",
        "size_small_chance" => "6",
        "size_dwarf_chance" => "0"
        ),
    "yellow" => array(
        "spawn_chance" => "25",
        "size_immense_chance" => "0",
        "size_giant_chance" => "6",
        "size_large_chance" => "25",
        "size_medium_chance" => "50",
        "size_small_chance" => "15",
        "size_dwarf_chance" => "4",
        )
    );
    
4

1 回答 1

1

不应该是这样的问题:

$star_array;

foreach($star_array as $chances_array) {
  foreach($chances_array as $chance) {
    // do sth
  }
}

如果您需要密钥,您可以使用:

$star_array;

foreach($star_array as $star => $chances_array) {
  foreach($chances_array as $chance_name => $chance_percentage) {
    // do sth
  }
}
于 2013-02-15T04:20:40.877 回答