0

I did not find this type of scenario in stackoverflow and google searches.

I have run into a problem with the logic in my code I have written. The problem is on case 18 in the rollGiftsandLegacies function below:

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[];
    }
}

I get it to display correctly, but I feel I could do this so much better...

the array, and the foreach to actually loop through the results looks like so:

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>");
    }
}

If I roll two sealed trunks (or more infinitely) with things inside it, it looks like this:

A sealed trunk with contents:

A musical instrument.
The characters true (and colorful) family history.
Gemmed dagger.
A sealed trunk with contents:
Gemmed sword.
A map.
A sealed trunk with contents:
Plain sword.

I want it to look like:

A sealed trunk with contents:

*A musical instrument.*
*The characters true (and colorful) family history.*
*Gemmed dagger.*

A sealed trunk with contents:

*Gemmed sword.*
*A map.*

A sealed trunk with contents:

*Plain sword.*

Any help would be appreciated!

4

1 回答 1

0

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

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

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

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

编辑添加:

这并不像我最初想象的那么容易;你必须作为参数传入$cultureRewardsrollRewards因为这是你要查看是否获得额外骰子的地方,对吧?

于 2012-07-13T18:52:55.080 回答