2

好的,假设我在 PHP 中有一堆 else-if 的小片段

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

但是,我会做到这一点吗?按级别获取经验,所以如果我输入getExpByLevel(3)它会输出:392

4

2 回答 2

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

于 2012-12-06T09:51:24.040 回答
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];
}
于 2012-12-06T09:56:15.390 回答