0

I am trying to make a text game in PHP but i have problem since i am programing in php for few days. I need to call function attack (I need $_mainAttack since it is combination of $_baseAttack and special attack) from defend function so i can calculate the health loss I have placed -5 just to see if it is working..

Also in health i need attack to be able to lower hp so i could calculate the hp loss. My do while loop is wrong and i need help to make it functional. I want when health goes lower than 0 to exit the loop. When it exits the loop it will print game over. I am stuck in endless loop and i have no idea how to fix this.

This is index.php:

<?php

include 'Duel.php';

$duel = new Duel();
$duel->attack();
$duel->defend();

?>

This is my class duel:

<?php

class Duel{

public $_maxHealth = 20;
public $_currentHealth;
public $_baseAttack, $_specialAttack, $_mainAttack;
public $_specialChance, $deflectChance;
public $_defense;

function __construct()
{
    echo 'begining of attack <br/>';
}


function attack()
{

    $_specialChance = rand(0, 20);

    $_specialAttack = 0;

    if ($_specialChance < 10) {
        $_specialAttack = 0;
    } elseif ($_specialChance < 15) {
        $_specialAttack = (int) rand(0, 5);
    } elseif ($_specialChance <= 20) {
        $_specialAttack = (int) rand(5, 10);
    }
    $_baseAttack = rand(1, 6);
    $_mainAttack = $_baseAttack + $_specialAttack;

    echo "Base attack is $_baseAttack:  and special attack is : $_specialAttack  attack is : $_mainAttack<br/>";        

}



function defend()
{
    $_maxHealth = 20;
    do{
        $deflectChance = rand(1, 10);
        $deflect = 0;

        if ($deflectChance < 5) {
             $deflect = 0;
             echo 'attack cannot be deflected';
        } 
        elseif ($deflectChance > 5) {
            $deflect = (int) rand(0, 3);
            echo "attack is deflected for {$deflect} damage";
        }

        $_currentHealth = $_maxHealth + $deflect - 5;

        echo "<br/>health is {$_currentHealth} <br/>";
    }while($_currentHealth > 0);
     if($_currentHealth > 0) echo "Game over";
}



} //end of class
4
  • Use $this-> to access an object's own properties from methods. Commented Jun 3, 2018 at 9:34
  • First of, I don't quite follow the logic. Why is game over when $_currentHealth is greater than 0? Shouldn't the game be over if $_currentHealth is less than or equal to 0? Why have a while loop to check for it in the first place? Wouldn't the if statement be enough? Commented Jun 3, 2018 at 9:37
  • Underscores at the beginning of property names, are usually there to indicate non public properties (old outdated convention). You can use visibility keywords as a 'modern' alternative. Commented Jun 3, 2018 at 9:37
  • I'd have thought you'd have a Player object, and a Duel function that takes two players and returns one. (A Duel observer, could report the attack.) Commented Jun 3, 2018 at 9:50

3 Answers 3

1

You are always calculating $_currentHealth based on $_maxHealth, not the previous $_currentHealth.

Add before the loop:

$_currentHealth = $_maxHealth;

And change $_currentHealth = $_maxHealth + $deflect - 5; to:

$_currentHealth = $_currentHealth + $deflect - 5;
Sign up to request clarification or add additional context in comments.

2 Comments

This is working $_currentHealth + $deflect - 5; Thanks. How could i replace -5 with $_mainAttack
Unfortunately, I'm not sure about the logic in this "game". My answer is targeted only to endless loop problem, but the rest of the code needs much more work based on what you are really trying to accomplish, and I think this is not really the place for this. Hey, you are not even using those public properties in your Duel class (no $this->).
1

You can try returning the main attack variable from the attack function and simply call it on the defend function.

2 Comments

If i try to return it and call it on defend function i get : Fatal error: Uncaught Error: Call to undefined function
You should call it this way: $this->attack().
0

you are calculation $_mainAttack but doesn't subtract it from your health, therefore your player can't die and you end within an endless loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.