English 中文(简体)
php 数学 lo格
原标题:php maths loop query

这将难以解释我所希望的是什么,因此我只想尽力。

我有游戏,游戏中的参与者也有健康的反响,也有身体卫士。 每名保.有100头盔,一旦所有保.都走过,损失就落在他们的肩上。

因此,这些都是我拥有的价值观:

$dmg // the damage
$bgs // the amount of bodyguards the player has
$bg_hp // the bodyguards health. Starts at 100, drains with $dmg fired at
$hp // the players health

当他们回枪时,如果他们有钱,就需要检查。 这是一个基本的例子。

if ($bgs > 0) {
  $bgs_hp = $bgs_hp - $dmg;
     if ($bg_hp <= 0) { $bg = $bg - 1; $bg_hp = 100; }
  } else {
  $hp = $hp - $dmg;
  }

Anyway, the part I need help with is this. I want the damage to overlap. So say the $dmg is 200, that would kill 2 bodyguards (they have 100 hp each). Or someone could shoot all their bodyguards and a remainder of the $dmg comes off the $hp too.

我需要帮助的原因是,在数学上不乏力,并且存在着一些裁军部的职能或我需要使用的东西。

实例;

(1) 运动员有3个保.。 在他身上有大约150 d。 它将杀死1名保.,造成50人受伤。

(2) 运动员有1名保.,为一半(50)。 有些人开枪射击他160人,杀死了保.(50hp),而其他损失(110)也将杀死该参与者。

最佳回答

我只保留一个阵列,每个保 the都有一席之地。

$bgs = array(100); // one bg with 100 hp

$bgs = array(100, 300, 50); // one bg with 100 hp, then one with 300, etc

如果你知道那里有多少保.,那么一个简单的<编码>($bgs)就告诉你。

This will make it much easier f或 you to expand your game in the future, and it also makes it much easier to resolve these types of calculations. You can now write:

$hp = 100;
$bgs = array(100, 100);
$damage = 101; // f或 example

// As long as there s still damage to take and bgs to take it:
while($damage && !empty($bgs)) {
    $soak = min($bgs[0], $damage); // not m或e than the first bg can take
    $bgs[0] -= $soak; // remove hps from the first bg
    $damage -= $soak; // deduct from the amount of damage to tage
    if ($bgs[0] == 0) {
        // bodyguard dead, remove him from the array
        array_shift($bgs);
    }
}

// If there s any damage left over, it goes to hp
$hp -= $damage;

<http://ideone.com/CdAW3”rel=“nofollow> 见行动

Update: Here s code that w或ks with your current scheme. It s not really simpler, and it is m或e restrictive on what you can do:

$max_bg_health = 100;
$hp = 100;
$bgs = 2;
$bg_hp = $max_bg_health; // so that makes 2 "full health" bgs
$damage = 101; // f或 example
if($bgs) {
    $bg_capacity = ($bgs - 1) * $max_bg_health + $bg_hp;
    $soak = min($bg_capacity, $damage);
    $bg_capacity -= $soak;
    $damage -= $soak;

    // update bodyguard status; this is extra tricky because $bgs is not
    // "the amount of full hp bgs" (which would make it a lot simpler)
    // but "the amount of bgs including the one which may not have full hp"
    $bgs = $bg_capacity ? intval($bg_capacity - 1 / $max_bg_health) + 1 : 0;
    $bg_hp = $bg_capacity % $max_bg_health;
}

// If there s any damage left over, it goes to hp
$hp -= $damage;
问题回答

一种可能性是,只储存全部保健点(即你和你的保).)。 然后,你可以每当你需要时,计算自己的健康点和保 body的数量(因此,你不需要拯救这些保障)。

$totalhp = 499; // you have 100 health and 4 bodyguards, the last one has 99 health

$bgs = $totalhp > 100 ? (int)(($totalhp-1)/100) : 0; // Number of active bodyguards
$hp = min($totalhp, 100); // Your health
$bg_hp = $bgs > 0 ? ((($totalhp-1) % 100) + 1) : 0; // the health of your "current" bodyguard

然后,当你遭受一些损害时,简单地从<条码>中减去损失。

$totalhp = $totalhp - $dmg;
<?php

$bg = 2;

$health_max = 100;

$health_total = $bg * $health_max + $health_max;

$damage = 100;

if($damage >= $health_total)
{
    die( You are caput. );
}
else
{
    $health_total = $health_total - $damage;
    $bg = floor($health_total / $health_max);
}

if($bg > 0)
{
    $tmp = ($health_total - $health_max) % $health_max;
    for($i = 1; $i < $bg; $i++)
        $bg_health[] = $health_max;
    $bg_health[] = $tmp;
    $bg_health = implode( ,  , $bg_health);
}

if(empty($bg_health)) $tmp = 0;
$health = $health_total - $tmp;

echo "You have {$health} health and {$bg} bodyguards left with health of {$bg_health}.";
?>

这将产生......

你们有100个健康机构,有1个有77个卫生机构。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签