Variable repetition problem in PHP

Asked

Viewed 55 times

0

I’m playing a little game where you simulate turn-based role-playing. I created two variables for the life of the player and the enemy, and then created two variables for their actions (if you fall into a certain number, you will do such an action).

The problem is that I want a defense action, which would reduce the attack received by half, but I don’t know how to put it. I tried to put a variable that would change in each different if, but this would cause the attack of one of them not to be updated, causing the character to receive the last number that was registered in the variable.

       $player =100;
        $enemy= 100;

        while ($enemy > 0)
        {
            $ataque = rand(1, 4)
        echo '
        1 - ataque<br>
        2- defesa <br>
        3- tomar poção<br>
        4- bola de fogo<br>
        <input type="text" name="action">
        <input type="submit" value="Enviar">"   ';
            if(isset($_GET["action"]))
            {
                    $action= $_GET["action"];
                    if($action== 1)
                    {
                        $player = $player - 15;
                        echo "<br>você deu um ataque no seu inimigo. causando 15 de dano";
                        echo "<br>Você: ".$player."<br>Inimigo: ".$enemy;
                    }

                    if($action== 2)
                    {

                        $dano = $golpe/2;
                        $player = $player - $dano;
                        echo "<br>você se defendeu do ataque do inimigo, recebendo metade do dano";
                        echo "<br>Você: ".$player."<br>Inimigo: ".$enemy;
                    }
                    if($ataque == 1)
                    {
                        $golpe = 15;
                        echo "<br>ele te deu um ataque, causando 15 de dano.";
                        echo "<br>Você: ".$player."<br>Inimigo: ".$enemy;
                    }
                    if($ataque == 2)
                    {
                        $enemy = $enemy - 5;
                        echo "<br>Ele se defendeu, recebendo só metade do dano";
                        echo "<br>Você: ".$player."<br>Inimigo: ".$enemy;
                    }

1 answer

1


Your code has some logic errors, and some ';' is missing (not if you just transcribed it wrong or if it’s in the code), and while at first generates infinite inputs, so I rewrote the code and it’s working.

<? 
if($_GET["player"] != null) { //verifica se já foi enviado a vida do player
    $player = $_GET['player']; 
} else {
    $player = 100; //valor inicial
}
if($_GET["enemy"] != null) { //verifica se já foi enviado a vida do player
    $enemy = $_GET['enemy'];
} else {
    $enemy = 100; //valor inicial
}

$dano = 15; //valor padrão do golpe
        
if(isset($_GET["action"])) {

    $ataque = rand(1, 2); 
    $action= $_GET["action"];
    
    if($action == 1 && $ataque == 1) { //Se ambos atacarem
        $enemy -= $dano;
        $player -= $dano;
        echo "<br>ele te deu um ataque, causando 15 de dano.";
        echo "<br>você deu um ataque no seu inimigo. causando 15 de dano";
        echo "<br>Você: ".$player."<br>Inimigo: ".$enemy;
    } elseif($action == 1 && $ataque == 2) { //Se vc atacar e o inimigo defender
        $enemy -= $dano / 2;
        echo "<br>Ele se defendeu, recebendo só metade do dano";
        echo "<br>Você: ".$player."<br>Inimigo: ".$enemy;
    } elseif($action == 2 && $ataque == 1) { //Se vc defender e o inimigo atacar
        $player -= $dano / 2;
        echo "<br>você se defendeu do ataque do inimigo, recebendo metade do dano";
        echo "<br>Você: ".$player."<br>Inimigo: ".$enemy;
    } else { // se ambos se defenderem
        echo "<br>ambos se defenderam, ninguém leva dano";
        echo "<br>Você: ".$player."<br>Inimigo: ".$enemy;
    }
}

if($enemy <= 0) { //verifica se a vida do inimigo zerou
    echo '<br>você venceu o inimigo';
} elseif($player <= 0) { //verifica se sua vida zerou
    echo '<br>você perdeu para o inimigo';
} else {
    echo '
    <form action="?" method="get">
        <br>1- ataque<br>
        2- defesa <br>
        3- tomar poção<br>
        4- bola de fogo<br>
        <input type="hidden" name="player" value="'.$player.'"> // envia a sua vida para o próximo evento
        <input type="hidden" name="enemy" value="'.$enemy.'"> // envia a vida do seu inimigo para o próximo evento
        <input type="text" name="action">
        <input type="submit" value="Enviar">
    </form>';
}

  • I was aware of the mistakes, this because I was adding more things, as a healing option and a stronger attack, so it gave conflict with the option to block. But the code you wrote is excellent, thank you for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.