我沒有在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] = " <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>");
}
}
如果我投擲兩個密封的箱子(或更多無限)與裏面的東西,它看起來像這樣:
一個密封的箱子內容:
一種樂器。
人物真實(和豐富多彩)家族的歷史。
寶石匕首。
密封后備箱內容:
寶石劍。
一張地圖。
一個密封的主幹與內容:
平原劍。
我希望它看起來像:
一個密封的內容幹線:。
*樂器*
*字符真(多彩)家族史*
。 *滿寶石匕首*
一種密封樹幹與內容:
*滿寶石的劍*
* A *地圖
一個密封的內容幹線:。
*普通的劍*
任何幫助,將不勝感激!
是否有必要玩D&D來理解這個問題或什麼? – netcoder 2012-07-13 18:51:05
我不明白你在做什麼.. FOR循環中的第一個獎勵@ cultureRoll函數在rollRewards的列表中生成d4多個項目。我將如何調用這個並重新遍歷該循環以包含d4多個項目? (是的,如果它擲出一個1,然後另一個10,d4 MORE items !!對你實際上想要做的事情還不太清楚 – 2012-07-13 18:52:53
我不認爲你需要玩D&D來理解它,但它當然幫助! – andrewsi 2012-07-13 18:57:38