Jump to content

[SOLVED] O.O.P Help


V34

Recommended Posts

Hello, sorry for not being more specific in my Subject, but i don't know how explain my problem in few words.

 

<?php
class person {
    var $health;
    var $armour;
    var $aim;
    var $luck;

    function setSkill($health) {
       $this->health = $health;
    }
    
    function isAlive() {
    if ($this->health > 0) {
       return true;
    }
    }

    function attack($perAtt,$number) {
       if ($perAtt->isAlive()) {
       if ($number == 1) {$perAction = "<b>You: </b>";} else {$perAction = "Him: ";}
          $perAtt->health -= 10;
          echo $perAction." Attack<br>";
       }
    }
}

$attacker = new person();
$attacker->setSkill("100");
$victim = new person();
$victim->setSkill("100");

while ($attacker->isAlive() || $victim->isAlive()) {
   $attacker->attack($victim,1);
   $victim->attack($attacker,2);
}

echo "<br><br>You: ".$attacker->health."<br>Him: ".$victim->health."|";

?>

 

Here i have my kind of RPG script, but the problem is the While Loop never ends, because the attack() function isn't functional. It's not attacking the person in the $perAtt variable.

 

Can anyone sort this out?

 

*Sorry, forgot to mention I'm using PHP 4*

 

- Jesper Eiby

Link to comment
https://forums.phpfreaks.com/topic/51859-solved-oop-help/
Share on other sites

it might be more of a logical thing...

 

your while loop says if the attacker OR the victim are alive, keep going. now - if either die, surely the other one cannot be killed as the only one that COULD have killed them is dead. hence a never ending loop.

 

what i think you need is an AND:

 

while ($attacker->isAlive() && $victim->isAlive()) {

 

which would mean that whilst both fighters are still alive, keep fighting.

 

right?

Link to comment
https://forums.phpfreaks.com/topic/51859-solved-oop-help/#findComment-255600
Share on other sites

right?

 

Right.

 

The loop in its current form will only terminate when both combatants are dead.  This is because a while loop terminates when the predicate is not true, in this case:  "The loop will stop when it is not the case that the attacker is alive or the victim is alive".  My DeMorgan we can simplify this to: "The loop will stop when the attacker is not alive and the victim is not alive".

 

Now that I've completely butchered Mark's elegant solution by introducing negations and formal logic theorems I should say: just do what Mark suggested in the previous post.

 

Best,

 

Patrick

Link to comment
https://forums.phpfreaks.com/topic/51859-solved-oop-help/#findComment-255606
Share on other sites

It's checking right here:

 

<?php
    function isAlive() {
    if ($this->health > 0) {
       return true;
    }
    }
?>

 

$person->isAlive() returns true if $persion->health > 0.

 

Might want to cahnge it to, for clarity... I can't remember if PHP regards a void return type as false...

 

<?php
    function isAlive() {
    if ($this->health > 0) {
       return true;
    }else{
       return false;
    }
    }
?>

Link to comment
https://forums.phpfreaks.com/topic/51859-solved-oop-help/#findComment-255612
Share on other sites

Sorry, but this is not the problem.

 

<?php
function attack($perAtt,$number) {
if ($perAtt->isAlive()) {
if ($number == 1) {$perAction = "<b>You: </b>";} else {$perAction = "Him: ";}
   $perAtt->health -= 10;
   echo $perAction." Attack<br>";
}
}
?>

 

If i run the function attack($victim,1);, will this line "if ($perAtt->isAlive()) {" not look like "if ($victim->isAlive()) {"

Link to comment
https://forums.phpfreaks.com/topic/51859-solved-oop-help/#findComment-255628
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.