1

我正在尝试为我正在制作的基于文本的 RPG 创建一个调平系统。我遇到的问题是范围似乎不适用于开关盒。这是我所拥有的:

int exp = 0, level = 1, hp = 10, hpmax = 10;

if (exp >= 262144) exp = 262144;

switch (exp)            
{
case (4 - 15):
    level = 2;
    hp = 12;
    hpmax = 12;
    break;

case (16 - 63):
    level = 3;
    hp = 14;
    hpmax = 14;
    break;

case (64 - 255):
    level = 4;
    hp = 16;
    hpmax = 16;
    break;

case (256 - 1023):
    level = 5;
    hp = 18;
    hpmax = 18;
    break;

case (1024 - 4095):
    level = 6;
    hp = 20;
    hpmax = 20;
    break;

case (4096 - 16383):
    level = 7;
    hp = 20;
    hpmax = 20;
    break;

case (16384 - 65535):
    level = 8;
    hp = 22;
    hpmax = 22;
    break;

case (65536 - 262143):
    level = 9;
    hp = 24;
    hpmax = 24;
    break;

case (262144 - 999999999):
    level = 10;
    hp = 26;
    hpmax = 26;
    break;

是的,我意识到这并没有达到我想要的效果。我对找到的替代方案不太满意。我正在寻找最简单的解决方案。我是编程新手,因此将不胜感激。

4

5 回答 5

1

注意如果hpmax总是 = hp,你可以在最后设置它,你不需要在所有的cases 中都有它。

使用 if-else:

if (4 <= exp && exp <= 15)
{
  level = 2;
  hp = 12;
}
else if (16 <= exp && exp <= 63)
{
  level = 3;
  hp = 14;
}
else if ...

hpmax = hp; // set hpmax afterwards

简化上述内容:

if (exp < 4) // 0-3
  ; // do nothing
else if (exp < 16) // 4-15
  ...
else if (exp < 64) // 16-63
  ...

或者用函数简化:(类似于 utnapistim 的建议)

bool in(int exp, int start, int end) { return start <= exp && exp <= end; };

if (in(exp, 4, 15)) // 4-15
  ...
else if (in(exp, 16, 63)) // 16-63
  ...

使用 log-base-2:

switch((int)(Math.Log(exp)/Math.Log(2)))
{
  case 2: case 3: // 4-15
    level = 2;
    hp = 12;
    break;
  case 4: case 5: // 16-63
    ...
  default: // 262144-999999999
    ...
}
hpmax = hp;

数学(更短的)解决方案:(不适用于所有情况,只是从中得出解决方案的东西)

int log = (int)(Math.Log(exp)/Math.Log(2));
level = 1+log/2;
hp = hpmax = 10+level*2;
于 2013-02-14T13:59:29.280 回答
0

这就是我最终使用的:

静态无效级别系统()

    {
        if ((4 <= exp) && (exp <= 15))
        {
            level = 2;
            hpmax = 12;
        }

        else if ((16 <= exp) && (exp <= 63))
        {
            level = 3;
            hpmax = 14;
        }

        else if ((64 <= exp) && (exp <= 255))
        {
            level = 4;
            hpmax = 16;
        }

        else if ((256 <= exp) && (exp <= 1023))
        {
            level = 5;
            hpmax = 18;
        }

        else if ((1024 <= exp) && (exp <= 4095))
        {
            level = 6;
            hpmax = 20;
        }

        else if ((4096 <= exp) && (exp <= 16383))
        {
            level = 7;
            hpmax = 20;
        }

        else if ((16384 <= exp) && (exp <= 65535))
        {
            level = 8;
            hpmax = 22;
        }

        else if ((65536 <= exp) && (exp <= 262143))
        {
            level = 9;
            hpmax = 24;
        }

        else if ((262144 <= exp) && (exp <= 999999998))
        {
            level = 10;
            hpmax = 26;
        }
        else if (exp >= 999999999)
        {
            exp = 999999999;
            level = 99;
            hp = 999999999;
            hpmax = 999999999;
        }
    }

我为以后的测试目的做了这个荒谬的数字,但我把它全部放在一个方法中,这样我就可以在需要检查 exp 时调用它。我对此进行了测试,它完全按照我的意愿工作。

感谢大家的帮助!

于 2013-02-14T17:53:17.393 回答
0

我认为您应该使用定义数组和连续 if 的组合来找到字符的正确级别,我将向您展示一个具有随机(和更少)值的样本......

<?php
// Definition of experience needed per level, and hp given
$levelsData = array( 1 => array( "exp" => 0,
                                 "hp" => 10),
                     2 => array( "exp" => 4,
                                 "hp" => 12),
                     3 => array( "exp" => 15,
                                 "hp" => 16),
                     4 => array( "exp" => 27,
                                 "hp" => 34),
                     5 => array( "exp" => 500,
                                 "hp" => 120));

// Finding user level, giving $exp as current exp
foreach ($levelsData as $level => $data) {
    if ($exp < $data['exp']) {
        break;
    }
}

// Setting data, we already have $level as the actual level+1
$level--;
$data = $levelsData[$level];
$hp = $data['hp'];
$maxHp = $hp;
?>
于 2013-02-14T14:02:44.563 回答
0

您可能正在寻找 if-then-else 语句:

           int exp = 0, level = 1, hp = 10, hpmax = 10;


            if ((4 >= exp) && (exp <= 15)){
                level = 2;
                hp = 12;
                hpmax = 12;
            } elseif ((16 >= exp) && (exp <= 63)){
                level = 3;
                hp = 14;
                hpmax = 14;
            } elseif ((64 >= exp) && (exp <= 255)){
                level = 4;
                hp = 16;
                hpmax = 16;
            } elseif ((256 >= exp) && (exp <= 1023)){
                level = 5;
                hp = 18;
                hpmax = 18;
            } elseif ((256 >= exp) && (exp <= 1023)){

             .... etc, you know what do paste here, etc ....

            } elseif ((262144 >= exp) && (exp <= 999999999)){
                level = 10;
                hp = 26;
                hpmax = 26;
            } else {
                ... you probably don't need anything here ...
                ... you already set defaults in the init up top...
            }
于 2013-02-14T14:00:42.630 回答
0

从概念上讲,您可以使用函数进行检查。

使用 C++ 的示例:

inline bool in_range(int min, int max, int value)
{ return min <= value && value <= max; }

// ....

        int exp = 0, level = 1, hp = 10, hpmax = 10;

        if (exp >= 262144) exp = 262144;

        if (in_range(0, 15, exp)) {
            level = 2;
            hp = 12;
            hpmax = 12;
        } else if (in_range(16, 63, exp)) {
            level = 3;
            hp = 14;
            hpmax = 14;
        } else if ...
于 2013-02-14T13:58:19.880 回答