我的PHP数组问题


My PHP array issue

这将是一个非常奇怪的问题,但请耐心等待。

我正在编写一个基于浏览器的游戏,每个玩家都有一定数量的警卫,每个警卫有100个生命值。每次他们中枪,警卫就会失去生命。如果所有的守卫都死了,玩家就会获得生命值。

射击伤害也总是重叠的,所以如果玩家有3个后卫,而最高后卫有60点生命值,那么100的射击将杀死后卫3,留下60点生命。

我使用php数组对此进行排序,它很有效,除非涉及到玩家的健康状况。它计算不正确,例如所有球员的后卫都死了,球员还有60点生命值。他中了100枪,但健康状况并没有恶化,所以他有一些其他数字的健康状况,而不是-40。

$p_bg = array(); // players guard count
$p_bg[0] = $rs[bgs_hp2]; // only the top guard hp is saved (bgs_hp2)
$p_hp = $rs[hp2]; // players health
$dmg = 80 // shot damage
$x = 0;
while($x < $rs[BGs2]) { $p_bg[$x] = 100; $x++; } 
// As long as there's still damage to take and bgs to take it:
while($dmg && !empty($p_bg)) {
   $soak = min($p_bg[0], $dmg); // not more than the first bg can take
   $p_bg[0] -= $soak; // remove hps from the first bg
   $dmg -= $soak; // deduct from the amount of damage to tage
   if ($p_bg[0] == 0) {
      // bodyguard dead, remove him from the array
      array_shift($p_bg);
      }
   }
// If there's any damage left over, it goes to hp
$p_hp = $p_hp - $dmg;

在不知道$rs的内容或不知道常数bgs_hp2hp2BGs2是什么的情况下,很难说,但问题似乎在于这几行的组合:

$p_bg = array(); // Create an empty array
$p_bg[0] = $rs[bgs_hp2]; // Populate the first index, possibly null?
$x = 0;
while($x < $rs[BGs2]) { $p_bg[$x] = 100; $x++; }

我怀疑BGs2是玩家的保镖?似乎每次点击此代码时,您都将顶级保镖的健康设置为100。如果按照以下方式重新安排,也许会更清楚:

$p_bg = array($rs[bgs_hp2]);
for ($x = 0; $x < $rs[BGs2]; $x++) { $p_bg[] = 100; }

除此之外,请注销您的变量(根据需要使用print_r($var)var_dump($var)),以查看您正在执行的实际数据。祝您好运!