Damond Posted September 20, 2015 Share Posted September 20, 2015 OK guys I'm still working on my minions system but now I am trying to get them to fight each other. I have the basic coding for it done but I'm having a little trouble with the win/lose system. I'm trying to use a WHILE statement with two conditions. while (($lose >= 0) || ($lose2 >= 0)){ // Attack $myrand = rand(1,3); if ($myrand ==1){ $type = "Human"; $q = $db->fetch_row($db->query("SELECT base FROM minions WHERE id=1")); $base = $q['base']; $suc = rand(1,10); $tot = $suc + $base; } else if ($myrand==2){ $type = "Elf"; $q = $db->fetch_row($db->query("SELECT base FROM minions WHERE id=2")); $base = $q['base']; $suc = rand(1,10); $tot = $suc + $base; } else { $type = "Dwarf"; $q = $db->fetch_row($db->query("SELECT base FROM minions WHERE id=3")); $base = $q['base']; $suc = rand(1,10); $tot = $suc + $base; } //Defend $myrand2 = rand(1,3); if ($myrand2 ==1){ $type2 = "Human"; $q2 = $db->fetch_row($db->query("SELECT base FROM minions WHERE id=1")); $base2 = $q2['base']; $suc2 = rand(1,10); $tot2 = $suc2 + $base2; } else if ($myrand==2){ $type2 = "Elf"; $q2 = $db->fetch_row($db->query("SELECT base FROM minions WHERE id=2")); $base2 = $q2['base']; $suc2 = rand(1,10); $tot2 = $suc2 + $base2; } else { $type2 = "Dwarf"; $q2 = $db->fetch_row($db->query("SELECT base FROM minions WHERE id=3")); $base2 = $q2['base']; $suc2 = rand(1,10); $tot2 = $suc2 + $base2; } if ($tot > $tot2){ $lose = $lose; $lose2 = $lose2 - 1; $result = "Player two loses 1 of ".number_format($perc).""; } else if ( $tot == $tot2) { $lose = $lose; $lose2 = $lose2; $result = "It was a tie"; } else { $lose = $lose-1; $lose2 = $lose2; $result = "Player one loses 1 of ".number_format($perc).""; } print" $result<br>"; } I have been searching and reading on trying to do this in this fashion but I can't find anything pointing me in the right direction. Now this code DOES actually work. It just doesn't stop when one or the other hits 0. Player two loses 1 of 10 Player two loses 1 of 10 Player two loses 1 of 10 Player one loses 1 of 10 Player one loses 1 of 10 Player one loses 1 of 10 Player one loses 1 of 10 Player two loses 1 of 10 Player two loses 1 of 10 Player two loses 1 of 10 It was a tie Player two loses 1 of 10 Player one loses 1 of 10 Player two loses 1 of 10 Player two loses 1 of 10 Player two loses 1 of 10 Player two loses 1 of 10 Player one loses 1 of 10 Player one loses 1 of 10 Player two loses 1 of 10 Player one loses 1 of 10 Player two loses 1 of 10 Player two loses 1 of 10 It was a tie Player one loses 1 of 10 Player one loses 1 of 10 Player one loses 1 of 10 Random: 3 Type: Dwarf Base: 6 Multi: 1 Total: 7 Win/lose: -1 <----- Here Random: 2 Type: Dwarf Base: 6 Multi: 5 Total: 11 Win/lose: -4 <----- And here Quote Link to comment Share on other sites More sharing options...
~Rob0t Posted September 20, 2015 Share Posted September 20, 2015 I'm on my phone so I'm not going to write it all, but you can use break; and continue; to manipulate loops. That knowledge will come in handy ;) Put the max turns as the while condition ($i <= 100) Increment $i each iteration If $lose > 0 then break; Etc... Quote Link to comment Share on other sites More sharing options...
Sim Posted October 5, 2015 Share Posted October 5, 2015 Condition is messed up your condition while (($lose >= 0) || ($lose2 >= 0)){ //only ONE needs to be true to CONTINUE LOOP Correct Condition while (($lose >= 0) && ($lose2 >= 0)){ //both must be true to CONTINUE LOOP I don't see why Robot mentioned iteration (max turns), but I would change it to a for loop then for($I = 0; $I < $max_turns; $I++) { //all your battle code if($lose <= 0 || $lose2 <= 0)//exit if loser before max turns { break; //exit loop } } Quote Link to comment 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.