supanoob Posted May 22, 2006 Share Posted May 22, 2006 Well i am trying to make a battle system using the following code:[code]<?phpif ($health < 1){echo "you do not have enough life to do this";die();}//battle forumula for player$rand=rand(1,10);$rand=$rand;$damage=($power2*$rand-$dex);$truedam=($damage/2);$newhealth=($health-$truedam);$sql2="UPDATE players SET health='$newhealth' WHERE user='$user'";if(mysql_query($sql2))//battle formula for dragon$rand2=rand(1,10);$rand2=$rand2;$damage2=($power*$rand2-$dex2);$truedam2=($damage/2);$newhealth2=($health2-$truedam2);$sql2="UPDATE players SET health='$newhealth2' WHERE user='Dragon'";if(mysql_query($sql2))echo "Dragon Hit You for $damage ($newhealth)<br>";echo "You hit dragon for $damage2 ($newhealth2) <br>";?>[/code]now what i would like to do is make it so it does it in rounds, so it repeats the code until newhealth is less than 1. i just dont know how to loop it if you get what i mean. i would like it to look like:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--][b]Round 1:[/b]Dragon hit you for $damage ($newhealth)You hit dragon for $damage2 ($newhealth2)[b]Round 2:[/b]Dragon hit you for $damage ($newhealth)You hit dragon for $damage2 ($newhealth2)[/quote]but ontop of that i would like it to randomly choose which person hits first using a a stat called Speed this would be / by 100 and * 10 to give it a percent chance.any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/10182-lopping/ Share on other sites More sharing options...
V-Man Posted May 22, 2006 Share Posted May 22, 2006 Take a look at the for() and while() loops. See the PHP manual as well as the tutorials here at PHP Freaks. You might also try googling 'PHP + For Loops'Cheers.Pat Quote Link to comment https://forums.phpfreaks.com/topic/10182-lopping/#findComment-37944 Share on other sites More sharing options...
obsidian Posted May 22, 2006 Share Posted May 22, 2006 ok, here's a psueduocode example of what you want to do... i'm not giving you any actual code (except for a loop recomendation). just fill in the needed code where the comments are, and you should be off to a good start:[code]while (/* Player Health > 0 AND Enemy Health > 0 */){ // Figure out who hits first (keep in mind that the opponent // can only strike back if he still has more than 0 HP). // First Strike // If opponent's health is greater than 0, you haven't killed // them yet, so do Second Strike now }[/code]that's really all there is to the logic. i'd recommend you write a class that handles all the attacking and defending logic within it so you can simply do something like this for your loop:[code]while ($player->health > 0 && $dragon->health > 0){ if ($first == 1) { $player->attack(); if ($dragon->health > 0) $dragon->attack(); else continue; } else { $dragon->attack(); if ($player->health > 0) $player->attack(); else continue; }}[/code]hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/10182-lopping/#findComment-37945 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.