douque8861 2014-05-19 21:23
浏览 61
已采纳

PHP - 两个数组之间的计算 - 键和值

Scenario of Web Game. We have two connected buildings. The second one is something like upgrade but not exactly, never mind this is not important.

Every building have experience level and this level its dependеnt of produced materials from the building. When we produce materials we have calculation between levels of the buildings. Example: we produce 2000 gold, every building produce the gold like:

Building B level - Building A level x Produced Gold

notes:

Always Building B(Upgrade building) have bigger priority so its: level B - level A x Gold

Always the produced gold is the same on both buildings

$building_A = array(4=>2000);
$building_B = array(5=>2000);
  • Building A Level 4 -> 2000 Gold

  • Building B Level 5 -> 2000 Gold

Calculation is : 5 - 4 x 2000 gold = 1 x 2000


Variant 0.1: Its possible Base building to Level UP and second building to be the same level: Array:

$buildingA = array(4=>1000, 5=>1000);
$buildingB = array(5=>2000);

Building А

  • Level 4: 1000 gold
  • Level 5: 1000 gold

Building B:

  • level 5: 2000 gold

Calculation:

5 - 4 x 1000 = 1 x 1000

5 - 5 x 1000 = 0 x 1000


Variant 0.2: Its possible and opposite situation: Array:

$buildingA = array(4=>2000);
$buildingB = array(4=>700,5=>1300);

Building A:

  • Level 4 : 2000 Gold

Building B:

  • Level 5 : 700 Gold
  • Level 6 : 1300 Gold

Calculation:

5-4 x 700 = 1 x 700

6-4 x 1300 = 1 x 1300

Here is the code of variants 0.1 & 0.2

http://sandbox.onlinephpfunctions.com/code/ef0adce304554392266b8193b292051c0d98bae8

//v.01
    $buildingA = array(4=>1000, 5=>1000);
    $buildingB = array(5=>2000);
//v.02  
    //$buildingA = array(4=>2000);
    //$buildingB = array(4=>700,5=>1300);

if(sizeof($buildingB) == 1)
{
    foreach($buildingA as $level_a => $gold)
    {
        $level_b = array_keys($buildingB);
        $level_b = $level_b[0];

        $level_diff = $level_b - $level_a;


        $tmp['calc'] = $level_b . ' - ' .$level_a.' = '.$level_diff . ' x ' . $gold;

        $x[] = $tmp;
    }
}
elseif(sizeof($buildingA) == 1) 
{
    foreach($buildingB as $level_b => $gold)
    {

        $level_a = array_keys($buildingA);
        $level_a = $level_a[0];

        $level_diff = $level_b - $level_a;

        $tmp['calc'] = $level_b . ' - ' .$level_a.' = '.$level_diff . ' x ' . $gold;

        $x[] = $tmp;
    }
}

Everything is good to here, but have and turd variant when the both buildings increase his level. I make several code's for this variant but non of them looks and work good. Can somebody with more clear mind give some suggestion how easy to calculate difference.

v.1

$buildingA = array(4=>500, 5=>1500);
$buildingB = array(4=>1000, 5=>1000);

Calculation: ()

  • 4 - 4 x 500
  • 4 - 5 x 500
  • 5 - 5 x 1000

v.2

$buildingA  = array(4=>1500, 5=>500);
$buildingB = array(4=>1000, 5=>1000);

Calculation: (B-A)

  • 4 - 4 x 1000
  • 5 - 4 x 500
  • 5 - 5 x 500

v.3

$buildingA = array(4=>1000, 5=>1000);
$buildingB = array(4=>500, 5=>1500);

Calculation: (B-A)

  • 4 - 4 x 500
  • 5 - 4 x 500
  • 5 - 5 x 1000

V.4

$buildingA = array(4=>1400, 5=>600);
$buildingB = array(5=>700, 6=>1300);

Calculation: (B-A)

  • 5 - 4 x 700
  • 6 - 4 x 700
  • 6 - 5 x 600

V.5

$buildingA = array(1=>300, 2=>700, 3=>1000);
$buildingB = array(5=>500, 6=>1500);

Calculation: (B-A)

  • 5 - 1 x 300
  • 5 - 2 x 200
  • 6 - 2 x 500
  • 6 - 3 x 1500

Lets try explan with v.3:

$buildingA = array(4=>1000, 5=>1000);
$buildingB = array(4=>500, 5=>1500);

Building A will produce 1000 gold in level 4 then level up and the rest is on level 5

Building B will produce 500 gold in level 4 then will level up and the rest is on level 5

So how we calculate:

  • "B" - 500 gold is given by level 4, we have 4 - 4 x 500, until now we use 500 from 2000 gold
  • "B" now become level 5
  • "A" its lv4 and need 500 more gold to level 5, so we give 5 - 4 x 500 (its 5 because "A" level up before that), until now we use (500 + 500) from 2000 gold
  • "A" now its level 5
  • "B" its level 5 and "A" is level 5, so all other gold its 5 - 5 x 1000, (here is the rest of not used gold: 1000)

Here i make some test script, but the code not look very well and its not work with v.04: http://sandbox.onlinephpfunctions.com/code/6f12fda4877ad781da52f908f3cba79aa0c69f33

I need to calculate how match gold will produce the buildings together when both level UP. I not see easy way to calc this. Any suggestions ?

I am out of ideas how to make that. its hard to explain it good, but i hope its writen clear - please do not judge me about that :)

  • 写回答

1条回答 默认 最新

  • douzhun8615 2014-05-21 12:06
    关注

    One a genius colleague provided me easy mathematical solution:

    function f ($x1, $x2)
    {    
        foreach ($x2 as $k2 => $v2)
        {
            if ($v2 == 0)
                continue;
    
            foreach ($x1 as $k1 => $v1)
            {
                if ($v2 == 0 || $v1 == 0)
                    continue;
    
                $m = ($v2 >= $v1) ? $v1 : $v2;
    
                $v2 -= $m;
                $v1 -= $m;
    
                $x2[$k2] = $v2;
                $x1[$k1] = $v1;
    
                echo '(' . $k2 . ' - ' . $k1 . ') = ' . ($m) . '<br />';
            }
        }
    }
    
    f($buildingA, $buildingB);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计