V34 Posted May 17, 2007 Share Posted May 17, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/51859-solved-oop-help/ Share on other sites More sharing options...
redbullmarky Posted May 17, 2007 Share Posted May 17, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/51859-solved-oop-help/#findComment-255600 Share on other sites More sharing options...
utexas_pjm Posted May 17, 2007 Share Posted May 17, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/51859-solved-oop-help/#findComment-255606 Share on other sites More sharing options...
V34 Posted May 17, 2007 Author Share Posted May 17, 2007 Thanks for the reply, but the problem is this: if ($perAtt->isAlive()) { $perAtt->health -= 10; I it's checkking if $perAtt is alive, and not the content of the variable. Quote Link to comment https://forums.phpfreaks.com/topic/51859-solved-oop-help/#findComment-255610 Share on other sites More sharing options...
utexas_pjm Posted May 17, 2007 Share Posted May 17, 2007 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51859-solved-oop-help/#findComment-255612 Share on other sites More sharing options...
V34 Posted May 17, 2007 Author Share Posted May 17, 2007 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()) {" Quote Link to comment https://forums.phpfreaks.com/topic/51859-solved-oop-help/#findComment-255628 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.