功能开关递归,可能无限循环

时间:2012-07-13 18:48:42

标签: php class function loops recursion

我没有在stackoverflow和谷歌搜索中找到这种类型的场景。

我遇到了我编写的代码中的逻辑问题。问题出在下面的rollGiftsandLegacies函数的案例18中:

class char
{
    private function d4() { $RNG = mt_rand(1,4); return $RNG; }
    // etc.

    public function recursiveArrayShrink($array) {
        array_walk_recursive($array, function($v, $k, $res) { $res[] = $v; }, $res);
        return $res;
    }
    private function rollCulture($dice) {
        switch($dice) {
            case 1: // For this culture, we have a 1-3 different types of rewards
                for ($i = 1; $i <= $this->d3(); $i++) {
                    $GiftsAndLegacies[$i] = $this->rollGiftsandLegacies($this->d20());
                }
                $results = $this->recursiveArrayShrink($GiftsAndLegacies);
                $this->character['culture']['benefits'] = $results;
            break;
        }
    }
    private function rollGiftsandLegacies($dice) {
        switch($dice) {
            case 1: // A Weapon
                switch($this->d10()) {
                    case 1: $gifts[] = "Gemmed dagger."; break;
                    case 2: $gifts[] = "Gemmed sword."; break;
                    case 3: $gifts[] = "Plain sword."; break;
                    case 4: $gifts[] = "A mace."; break;
                    case 5: $gifts[] = "Gemmed spear."; break;
                    case 6: $gifts[] = "Well-made short or long bow."; break;
                    case 7: $gifts[] = "Gemmed battle-axe."; break;
                    case 8: $gifts[] = "Any type of crossbow."; break;
                    case 9: $gifts[] = "Exotic weapon. GM Only"; break;
                    // Simple re-roll example: works!
                    case 10: 
                        for($i = 1; $i <= $this->d4(); $i++) {
                            $gifts[] = $this->rollGiftsandLegacies($dice = 16);
                        }
                    break;
                }
            break;
            case 2: $gift = "A tapestry."; break;
            case 18: // SEALED TRUNK: 60% chance that it contains 2-4 additional items on this table.
                if($this->d100() <= 60) {
                    $gifts[] = "A sealed trunk with contents:";
                    $this->GiftsAndLegaciesSealedTrunk = $this->d3() + 1;
                    for($i = 1; $i <= $this->GiftsAndLegaciesSealedTrunk; $i++) {
                        $roll = $this->d20();
                        $item = $this->recursiveArrayShrink($this->rollGiftsandLegacies($roll));
                        $item[0] = "&nbsp;&nbsp;&nbsp;<i>". $item[0];
                        $item[0] .= "</i>";
                        $gifts[] = $item;
                    }
                } else {
                    $gifts[] = "A sealed trunk that is empty.";
                }
            break;
        }
        return $gifts[];
    }
}

我让它显示正确,但我觉得我可以做得更好......

数组,实际循环结果的foreach如下所示:

Array
(
    [0] => A sealed chest with contents:
    [1] =>     A musical instrument.
    [2] =>     The characters true (and colorful) family history.
    [3] =>     Gemmed dagger.
    [4] =>     A sealed trunk with contents:
    [5] =>     Gemmed sword.
    [6] =>     A map.
    [7] =>     A sealed trunk with contents:
    [8] =>     Plain sword.
)
if(isset($c['culture']['benefits'])) {
    foreach($c['culture']['benefits'] as $benefit) {
        print("".$benefit."<br>");
    }
}

如果我将两个密封的树干(或更多无限地)卷入其中,它看起来像这样:

带有内容物的密封行李箱:

  

乐器。
  人物真实(和丰富多彩)的家族历史。
   Gemmed dagger。
  密封的行李箱内容:
  宝石剑。
  地图。
  密封的行李箱内容:
  普通剑。

我希望它看起来像:

带有内容物的密封行李箱:

*乐器。*
*人物真实(和丰富多彩)的家族历史。*
*宝石匕首。*

带有内容物的密封行李箱:

*宝石剑。*
*地图。*

带有内容物的密封行李箱:

*普通剑。*

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我认为最简单的方法是使用$cultureRewards变量。

在开始时将其设置为正确的值,并有一个循环:

while ($cultureRewards > 0) {
    # roll dice
    $cultureRewards--;

    if ($dice == 1) {
        $cultureRewards += d3();
    }
}

编辑添加:

这并不像我初想的那么容易;你必须将$cultureRewards传递给rollRewards作为参数,因为你正在寻找你是否会得到额外的骰子,对吗?