2012-12-06 95 views
2

OK,讓我們說我有一大堆其他-IFS的這個小SNIPPIT在PHPPHP其他如果數組?

function getLevelByExp($theExp) { 

    if ($theExp < 83) { $theLevel = 1; } 
else if ($theExp < 175) { $theLevel = 2; } 
else if ($theExp < 278) { $theLevel = 3; } 
else if ($theExp < 392) { $theLevel = 4; } 
else if ($theExp < 519) { $theLevel = 5; } 
else if ($theExp < 659) { $theLevel = 6; } 
else if ($theExp < 814) { $theLevel = 7; } 
else if ($theExp < 987) { $theLevel = 8; } 
else if ($theExp < 1178) { $theLevel = 9; } 
else if ($theExp < 1390) { $theLevel = 10; } 
else if ($theExp < 1624) { $theLevel = 11; } 
else if ($theExp < 1884) { $theLevel = 12; } 
else if ($theExp < 2172) { $theLevel = 13; } 
else if ($theExp < 2492) { $theLevel = 14; } 
else if ($theExp < 2845) { $theLevel = 15; } 
else if ($theExp < 3237) { $theLevel = 16; } 
else if ($theExp < 3671) { $theLevel = 17; } 
else if ($theExp < 4151) { $theLevel = 18; } 
else if ($theExp < 4683) { $theLevel = 19; } 
else if ($theExp < 5273) { $theLevel = 20; } 
else if ($theExp < 5926) { $theLevel = 21; } 
else if ($theExp < 6650) { $theLevel = 22; } 
else if ($theExp < 7451) { $theLevel = 23; } 
else if ($theExp < 8339) { $theLevel = 24; } 
else if ($theExp < 9322) { $theLevel = 25; } 
else if ($theExp < 10412) { $theLevel = 26; } 
else if ($theExp < 11618) { $theLevel = 27; } 
else if ($theExp < 12955) { $theLevel = 28; } 
else if ($theExp < 14436) { $theLevel = 29; } 
else if ($theExp < 16076) { $theLevel = 30; } 
else if ($theExp < 17893) { $theLevel = 31; } 
else if ($theExp < 19906) { $theLevel = 32; } 
else if ($theExp < 22137) { $theLevel = 33; } 
else if ($theExp < 24607) { $theLevel = 34; } 
else if ($theExp < 27345) { $theLevel = 35; } 
else if ($theExp < 30377) { $theLevel = 36; } 
else if ($theExp < 33737) { $theLevel = 37; } 
else if ($theExp < 37460) { $theLevel = 38; } 
else if ($theExp < 41584) { $theLevel = 39; } 
else if ($theExp < 46154) { $theLevel = 40; } 
else if ($theExp < 51217) { $theLevel = 41; } 
else if ($theExp < 56827) { $theLevel = 42; } 
else if ($theExp < 63042) { $theLevel = 43; } 
else if ($theExp < 69929) { $theLevel = 44; } 
else if ($theExp < 77560) { $theLevel = 45; } 
else if ($theExp < 86015) { $theLevel = 46; } 
else if ($theExp < 95383) { $theLevel = 47; } 
else if ($theExp < 105764) { $theLevel = 48; } 
else if ($theExp < 117266) { $theLevel = 49; } 
else if ($theExp < 130010) { $theLevel = 50; } 
else if ($theExp < 144132) { $theLevel = 51; } 
else if ($theExp < 159781) { $theLevel = 52; } 
else if ($theExp < 177120) { $theLevel = 53; } 
else if ($theExp < 196333) { $theLevel = 54; } 
else if ($theExp < 217623) { $theLevel = 55; } 
else if ($theExp < 241215) { $theLevel = 56; } 
else if ($theExp < 267356) { $theLevel = 57; } 
else if ($theExp < 296323) { $theLevel = 58; } 
else if ($theExp < 328421) { $theLevel = 59; } 
else if ($theExp < 363990) { $theLevel = 60; } 
else if ($theExp < 403404) { $theLevel = 61; } 
else if ($theExp < 447078) { $theLevel = 62; } 
else if ($theExp < 495475) { $theLevel = 63; } 
else if ($theExp < 549104) { $theLevel = 64; } 
else if ($theExp < 608531) { $theLevel = 65; } 
else if ($theExp < 674384) { $theLevel = 66; } 
else if ($theExp < 747357) { $theLevel = 67; } 
else if ($theExp < 828220) { $theLevel = 68; } 
else if ($theExp < 917826) { $theLevel = 69; } 
else if ($theExp < 1017122) { $theLevel = 70; } 
else if ($theExp < 1127154) { $theLevel = 71; } 
else if ($theExp < 1249084) { $theLevel = 72; } 
else if ($theExp < 1384199) { $theLevel = 73; } 
else if ($theExp < 1533925) { $theLevel = 74; } 
else if ($theExp < 1699840) { $theLevel = 75; } 
else if ($theExp < 1883698) { $theLevel = 76; } 
else if ($theExp < 2087436) { $theLevel = 77; } 
else if ($theExp < 2313207) { $theLevel = 78; } 
else if ($theExp < 2563392) { $theLevel = 79; } 
else if ($theExp < 2840632) { $theLevel = 80; } 
else if ($theExp < 3147852) { $theLevel = 81; } 
else if ($theExp < 3488295) { $theLevel = 82; } 
else if ($theExp < 3865553) { $theLevel = 83; } 
else if ($theExp < 4283609) { $theLevel = 84; } 
else if ($theExp < 4746873) { $theLevel = 85; } 
else if ($theExp < 5260236) { $theLevel = 86; } 
else if ($theExp < 5829115) { $theLevel = 87; } 
else if ($theExp < 6459514) { $theLevel = 88; } 
else if ($theExp < 7158087) { $theLevel = 89; } 
else if ($theExp < 7932204) { $theLevel = 90; } 
else if ($theExp < 8790038) { $theLevel = 91; } 
else if ($theExp < 9740641) { $theLevel = 92; } 
else if ($theExp < 10794045) { $theLevel = 93; } 
else if ($theExp < 11961369) { $theLevel = 94; } 
else if ($theExp < 13254932) { $theLevel = 95; } 
else if ($theExp < 14688387) { $theLevel = 96; } 
else if ($theExp < 16276862) { $theLevel = 97; } 
else if ($theExp < 18037122) { $theLevel = 98; } 
else if ($theExp < 20000000) { $theLevel = 99; } 
else { $theLevel = 99; } 

return $theLevel; 

} 

正如你所知道的,我可以輕鬆搞定水平,使用體驗點,像$currentLevel = getLevelByExp(300)這將輸出: 3

但是,我會這樣做,所以它被顛倒?通過Level獲得體驗,如果我輸入getExpByLevel(3)則輸出:392

+0

從那你給我們的用戶可以體驗278和392之間有3級。所以你不能準確地得到EX。您只能獲得級別3所需的最小或最大EX。 – shadyyx

+0

這些值從哪裏來?你是如何計算的,還是隻是隨機數?如果您計算了它們,則可以將其扭轉以獲得特定體驗的水平 –

+1

我建議您使用level和exp之間的數學關係。 – dognose

回答

2

這應該做的伎倆

// level 0: 0 exp, level 1: 83 exp, level 2: 175 exp, level 3: 278 exp, ... 
$levels = array(0, 83, 175, 278 /*, ... */); 

// get level for theExp 
$theExp = 180; 

foreach ($levels as $level => $exp) { 
    if($theExp < $exp) break; 
} 

// output level 
echo "the level for {$theExp} is {$level}"; 
// => 2 

爲了得到這個包裹起來使用一些非常棒的封裝

class Player 

    static private $levels = array(0, 83, 175, 278); 
    static private $max_level = 50; 

    private $exp; 

    public function __construct($exp) { 
     $this->exp = $exp; 
    } 

    public function getLevel() { 

     // find level based on exp 
     foreach (self::$levels as $level => $exp) { 
      if ($this->exp < $exp) 
       return $level; 
     } 

     // no level found; return max level 
     return self::$max_level; 
    } 
} 

這裏的用法

// create new player with 180 exp 
$ply = new Player(180); 

// get player level 
echo $ply->getLevel(); //=> 2 

感謝max level提示,@EugenRieck

+0

我不認爲我應該在凌晨4點編碼。我現在要去睡覺了.........(我認爲你應該考慮周圍的Excel和Level) – nn2

+0

@weka,因爲數組已經序列化了它們的數字索引,所以讓數組索引代表級別是非常合理的。 –

0
$EXP_LEVEL_MAP=array (0,83,175,278,392,....20000000); 

function lvlFromExp($exp) { 
    global $EXP_LEVEL_MAP; 
    foreach($EXP_LEVEL_MAP as $k=>$v) 
    if ($v>$exp) 
     return $k; 
    return 99; 
} 

function expFromLvl($lvl) { 
    global $EXP_LEVEL_MAP; 
    if ($lvl>99) $lvl=99; 
    else if ($lvl<1) $lvl=1M 
    return $EXP_LEVEL_MAP[$lvl]; 
}