Ruddy Posted February 26, 2012 Share Posted February 26, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257800-php-loops-turn-based-attacks/ Share on other sites More sharing options...
kicken Posted February 26, 2012 Share Posted February 26, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257800-php-loops-turn-based-attacks/#findComment-1321327 Share on other sites More sharing options...
batwimp Posted February 26, 2012 Share Posted February 26, 2012 It doesn't look like (from the code you posted) you are subtracting the damage done from the hit points, which is used to check for loop termination. In other words, you should work into the end of your loop: $monster_hp -= $damdone; $userhp -= $damtaken; Quote Link to comment https://forums.phpfreaks.com/topic/257800-php-loops-turn-based-attacks/#findComment-1321331 Share on other sites More sharing options...
Ruddy Posted February 26, 2012 Author Share Posted February 26, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/257800-php-loops-turn-based-attacks/#findComment-1321400 Share on other sites More sharing options...
kicken Posted February 26, 2012 Share Posted February 26, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/257800-php-loops-turn-based-attacks/#findComment-1321410 Share on other sites More sharing options...
Ruddy Posted February 26, 2012 Author Share Posted February 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257800-php-loops-turn-based-attacks/#findComment-1321414 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.