Jump to content

PHP Loops (Turn based attacks)


Ruddy

Recommended Posts

Hey guys,

 

I am having a problem with making a system where it is turn based attacks.

 

I want it so a user can attack a monster and the monster can attack back, the output I want is like.

 

You hit the monster for 5.
The monster hit you for 7.
You hit the monster for 3.
The monster hit you for 6. 

 

and so on.

 

I have been going at this for ages and cannot get what I want.

 

I had problems such as:

The monster attacking when it should be dead.

Even when the monster was dead the user would still attack.

 

This is the code I have now that is maybe totaly wrong, thats why I need your guys help!

 

	
        $monster_hp = 10;
$userhp =10;

for ($i=1,$n=1; $i<=$monster_hp, $n<=$userhp; $n+=$damtaken, $i+=$damdone){

$damtaken = 1*rand(1,3) + rand(1,4) + 1 + 1;
$damdone = 1*rand(1,2) + rand(1,2) + 1 + 1;	

echo "You hit the monster for $damdone.<br />";

echo "The monster hit you for $damtaken. <br />";
}

 

The output for that is:

You hit the monster for 5.
The monster hit you for 7.
You hit the monster for 5.
The monster hit you for 5. 

That is wrong due to the monster having 10 hp and I have hit him for 10hp overall. I know that thats because the echo is in the loop so it has to run but I have tried so many ways to get this to work and cannot do it.

 

If I die, it should stop with the monsters attack and a message saying "The monster has killed you", if I win it should stop his next attack and say "You have killed the monster".

 

Please help me guys, you have helped me before so I know how good some of you are.

 

Thank you so much for anyhelp that is givin.

Ruddy.

Link to comment
Share on other sites

This is just pseudo code, you'll have to implement it your self, but your basic setup would go something like this:

 

$whosTurn = 'player';
do {
    switch ($whosTurn){
       case 'player':
            //generate & print players attack
            $whosTurn='monster';
            break;
       case 'monster':
            //generate & print monsters attack
            $whosTurn='player';
            break;
     }

} while ($player->isAlive() && $monster->isAlive());

 

In other words, you only do one attack per loop iteration, and have a state variable to keep track of whos turn it is so.

 

Link to comment
Share on other sites

This is just pseudo code, you'll have to implement it your self, but your basic setup would go something like this:

 

$whosTurn = 'player';
do {
    switch ($whosTurn){
       case 'player':
            //generate & print players attack
            $whosTurn='monster';
            break;
       case 'monster':
            //generate & print monsters attack
            $whosTurn='player';
            break;
     }

} while ($player->isAlive() && $monster->isAlive());

 

In other words, you only do one attack per loop iteration, and have a state variable to keep track of whos turn it is so.

 

Im not really sure what you mean, I get why what your trying to do but have no idea how to complete it. Would you be able to explain abit more? I came up with this but it does not work.

 

$whosTurn = 'player';
$hp='10';
$mhp = '10';
$mtotaldam='1';
$totaldam='1';

do {
    switch ($whosTurn){
       case 'player':
          $damdone = 1*rand(1,2) + rand(1,2) + 1 + 1;	
          echo "You hit the monster for $damdone.<br />";
          $totaldam+=$damdone;
            $whosTurn='monster';
            break;

       case 'monster':
          $damtaken = 1*rand(1,3) + rand(1,4) + 1 + 1;
            echo "The monster hit you for $damtaken. <br />";

            $mtotaldam+=$damtaken;
            $whosTurn='player';
            break;
     }

} while ($totaldam <= $mhp || $mtotaldam <= $hp);

Link to comment
Share on other sites

What you have is close.  You would add your checks to determine if someone died inside the switch statement, one for each case.  Your bottom while condition should use &&, not ||.  You only want it to keep looping if both the player and the monster are alive, not if just either one is alive.

 

Your also starting them out with one damage each, probably it should start at zero. You also shouldn't quote numeric values in your script.

 


<?php

$whosTurn = 'player';
$hp=10;
$mhp = 10;
$mtotaldam=0;
$totaldam=0;

do {
    switch ($whosTurn){
        case 'player':
                $damdone = 1*rand(1,2) + rand(1,2) + 1 + 1;
                echo "You hit the monster for $damdone.<br />";
                $totaldam+=$damdone;

                if ($totaldam >= $mhp){
                        echo "You have killed the monster!";
                }

                $whosTurn='monster';
                break;

        case 'monster':
                $damtaken = 1*rand(1,3) + rand(1,4) + 1 + 1;
                echo "The monster hit you for $damtaken. <br />";
                $mtotaldam+=$damtaken;

                if ($mtotaldam >= $hp){
                        echo "You have been killed!";
                }

                $whosTurn='player';
                break;
     }

} while ($totaldam < $mhp && $mtotaldam < $hp);

Link to comment
Share on other sites

What you have is close.  You would add your checks to determine if someone died inside the switch statement, one for each case.  Your bottom while condition should use &&, not ||.  You only want it to keep looping if both the player and the monster are alive, not if just either one is alive.

 

Your also starting them out with one damage each, probably it should start at zero. You also shouldn't quote numeric values in your script.

 


<?php

$whosTurn = 'player';
$hp=10;
$mhp = 10;
$mtotaldam=0;
$totaldam=0;

do {
    switch ($whosTurn){
        case 'player':
                $damdone = 1*rand(1,2) + rand(1,2) + 1 + 1;
                echo "You hit the monster for $damdone.<br />";
                $totaldam+=$damdone;

                if ($totaldam >= $mhp){
                        echo "You have killed the monster!";
                }

                $whosTurn='monster';
                break;

        case 'monster':
                $damtaken = 1*rand(1,3) + rand(1,4) + 1 + 1;
                echo "The monster hit you for $damtaken. <br />";
                $mtotaldam+=$damtaken;

                if ($mtotaldam >= $hp){
                        echo "You have been killed!";
                }

                $whosTurn='player';
                break;
     }

} while ($totaldam < $mhp && $mtotaldam < $hp);

 

Hey,

 

I did it this way. What do you think?

$hp=10;
$mhp = 10;
$mtotaldam=0;
$totaldam=0;

do {

$damdone = rand(2,4);
$mdamdone = rand(2,4);
$totaldam+=$damdone;
$mtotaldam+=$mdamdone;
if($totaldam>=$mhp){ 
echo "you hit it with a finishing blow for $damdone <br />";
echo"You killed it <br />";
break;
}
else{
echo "you hit it for $damdone <br />";
}


if($mtotaldam>=$hp){
echo "It hit you with a finishing blow for $mdamdone  <br />";
echo"It killed you. <br />";
break;
}
else{
echo "It hit you for $mdamdone <br />";
}
} while ($win=1);//cause an INF loop

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.