Jump to content

While Loops


supanoob

Recommended Posts

I have the following code and it seems to never stop :P its an infinite loop and i dont know why. i check in several place if life is < 1 and in which case it should stop because of the DIE();

[code]<?php
include_once('top_html.php');
?>
<TD bgcolor=#000000 style="border: 1px solid #363636">
<?php
if($step == 'npc'){
//NPC Battle
if($action == 'normal'){
  //Query For NPC Stats
  $query2="select npc_name, npc_power, npc_level, npc_speed, npc_dexterity, npc_health, npc_max_health, npc_gold from npcs where npc_id='$search'";
  $result2=mysql_query($query2) or die (mysql_error());
  $num_rows=mysql_num_rows($result2);
  $row=mysql_fetch_array($result2);
  $npc_name=($row['npc_name']);
  $npc_power=($row['npc_power']);
  $npc_speed=($row['npc_speed']);
  $npc_dexterity=($row['npc_dexterity']);
  $npc_health=($row['npc_health']);
  $npc_max_health=($row['npc_max_health']);
  $npc_gold=($row['npc_gold']);
  $npc_level=($row['npc_level']);
  //Query For NPC Stats End

  //information for the battle formula
  //power is determined by *ing it by 5 then taking it away from dex.
  //dexterity is determined by *inf it by a random number.
  //the random number is between 1 and 10 and then divided by 10.
  //both players will have a chance to attack randomly throughout the battle
  //this is determined by their speed.
  //that will be divided by the oponents dexterity and then *sed by 100 to give a percent.
  //that percent will be the chance they have of hitting more often.
  //however they will never have more than 80% chance of hitting more.
  //if the npc class is Necromancer they will have a chance of a skeleton attacking.
  //this will be determined by overall level of npc that will be turned into a percent.
  //if npc class is Mage they will have a chance of casting a spell.
  //if class fire mage - fire ball, frost mage - ice blast, white mage - heal, black mage - destruction.
  //the chance of casting magic will be determined by overall level
  //if class is thief they will have a chance of stealing extra gold during the battle.
  //if class is warrior they will have the chance of doing battle charge during battle and hitting for extra damage.
  //THIS IS NOT THE ACTUAL BATTLE, THESE WILL NEED PLACING IN THE WHILE LOOP.

  //NPC's Formula
  //$npc_power_1 = ($npc_power*2);
  //$npc_rand_1 = rand(1,10);
  //$npc_rand_2 = ($npc_rand_1/10);
  //$npc_dexterity_1 = ($npc_dexterity*$rand_2);
  //$npc_speed_1 = (($npc_speed/$npc_dexterity)*100);
  //$npc_damage = ($npc_dexterit_1-$npc_power_1);
  //if ($npc_damage < '0') {$npc_damage = '0';}
  //Necro Class random numbers
  //$npc_necro_rand = rand(1,100);
  //mage class random numbers
  //$npc_mage_rand =  rand(1,100);
  //thief class random numbers
  //$npc_thief_rand = rand(1,100);
  //warrior class random numbers
  //$npc_warrior_rand = rand(1,100);
  //end NPC formula

  //Player's Formula
  //$player_power_1 = ($power*5);
  //$player_rand_1 = rand(1,10);
  //$player_rand_2 = ($player_rand_1/10);
  //$player_dexterity_1 = ($dexterit/$rand_2);
  //$player_speed_1 = (($speed/$dexterity)*100);
  //$player_damage = ($player_dexterit_1-$player_power_1);
  //if ($player_damage < '0') {$player_damage = '0';}
  //Necro Class random numbers
  //$player_necro_rand = rand(1,100);
  //mage class random numbers
  //$player_mage_rand =  rand(1,100);
  //thief class random numbers
  //$player_thief_rand = rand(1,100);
  //warrior class random numbers
  //$player_warrior_rand = rand(1,100);
  //end Players formula 

  //If Win and check if level.
  //if ($npc_health < '1')
  //{
  //$gold_gain = rand(10,25);
  //$exp_gain_rand = rand(90,100);
  //$level_difference = ($npc_level-$level);
  //if ($level_difference < 0){$level_difference = '0';}
  //$exp_gain = ($exp_gain_rand*$level_difference);
  //$sql2="UPDATE accounts SET gold=gold+$gold_gain, level_exp=level_exp+$exp_gain, npc_wins=npc_wins+1 WHERE char_id='$char_id'";
  //if(mysql_query($sql2))
  //echo "You won $npc_name. You gain $gold_gain gold and $exp_gain exp.
";
  //if ($level_exp > $level_exp_need)
  //{
  //$sql2="UPDATE accounts SET exp='0', level_exp_need=$exp_need_1, battle_points=battle_points+3, level=level+1 WHERE char_id='$char_id'";
  //if(mysql_query($sql2))
  //echo "You also gained a level. You recieve 3 Battle points.";
  // }
  //die(include_once('bottom_html.php'));
  //}
  //End If Win
  //If Lose
  //if ($npc_health < '1')
  //{
  //$gold_lose = rand(10,25);
  //$sql2="UPDATE accounts SET gold=gold+$gold_lose, npc_wins=npc_wins-1 WHERE char_id='$char_id'";
  //if(mysql_query($sql2))
  //echo "You lost to $npc_name. You lose $gold_lose gold..";
  //die(include_once('bottom_html.php'));
  //}
  //End If Lose   
  //END ALL NOTES.
  //THIS IS THE BATTLE.

  while($health>0 && $npc_health>0){
  //Formula's In The Loop
  //NPC's Formula
  $npc_power_1 = ($npc_power*2);
  $npc_rand_1 = rand(1,10);
  $npc_rand_2 = ($npc_rand_1/10);
  $npc_dexterity_1 = ($npc_dexterity*$npc_rand_2);
  $npc_speed_1 = (($npc_speed/$dexterity)*100);
  $npc_damage = ($npc_power_1-$player_dexterity_1);
  $npc_damage = round($npc_damage);
  if($npc_damage < '0'){ $npc_damage='0'; }
  //Necro Class random numbers
  $npc_necro_rand = rand(1,100);
  //mage class random numbers
  $npc_mage_rand =  rand(1,100);
  //thief class random numbers
  $npc_thief_rand = rand(1,100);
  //warrior class random numbers
  $npc_warrior_rand = rand(1,100);
  //end NPC formula

  //Player's Formula
  $player_power_1 = ($power*2);
  $player_rand_1 = rand(1,10);
  $player_rand_2 = ($player_rand_1/10);
  $player_dexterity_1 = ($dexterity/$player_rand_2);
  $player_speed_1 = (($speed/$npc_dexterity)*100);
  $player_damage = ($player_power_1-$npc_dexterity_1);
  $player_damage = round($player_damage);
  if($player_damage<'0'){ $player_damage='0'; }
  //Necro Class random numbers
  $player_necro_rand = rand(1,100);
  //mage class random numbers
  $player_mage_rand =  rand(1,100);
  //thief class random numbers
  $player_thief_rand = rand(1,100);
  //warrior class random numbers
  $player_warrior_rand = rand(1,100);
  //end Playersformula
  //End In Loop Formula's
  //determine who strikes first using the speed.
  if($player_speed_1 > $npc_speed_1){
    $sql2="UPDATE npcs SET npc_health=npc_health-$player_damage WHERE npc_id='$search'";
    if(mysql_query($sql2)) echo "<font color="Green\">You Attacked $npc_name for $player_damage life.</font>";
    $npc_health_1=($npc_health-$player_damage);
    if($npc_health_1 < 1){
    $gold_gain = rand(10,25);
    $exp_gain_rand = rand(90,100);
    $level_difference = ($npc_level-$level);
    if($level_difference<0){ $level_difference='0'; }
    $exp_gain = ($exp_gain_rand*$level_difference);
    $sql2="UPDATE accounts SET gold=gold+$gold_gain, level_exp=level_exp+$exp_gain, npc_wins=npc_wins+1 WHERE char_id='$char_id'";
    if(mysql_query($sql2))
    echo "You won $npc_name. You gain $gold_gain gold and $exp_gain exp.";
    if($level_exp > $level_exp_need){
      $sql2="UPDATE accounts SET exp='0', level_exp_need=$exp_need_1, battle_points=battle_points+3, level=level+1 WHERE char_id='$char_id'";
      if(mysql_query($sql2)) echo "You also gained a level. You recieve 3 Battle points.";
    }
    die(include_once('bottom_html.php'));
    }
  }
  //End the first attack.
  //into the battle
  //Player gets first hit
  echo "<font color=\"Green\">You Attacked $npc_name for $player_damage life.</font>";
  //update npc life
  $sql2="UPDATE npcs SET npc_health=npc_health-$player_damage WHERE npc_id='$search'";
  if(mysql_query($sql2)) $npc_health_1=($npc_health-$player_damage);
  if($npc_health_1 < 1){
    $gold_gain = rand(10,25);
    $exp_gain_rand = rand(90,100);
    $level_difference = ($npc_level-$level);
    if($level_difference<0){ $level_difference='0'; }
    $exp_gain = ($exp_gain_rand*$level_difference);
    $sql2="UPDATE accounts SET gold=gold+$gold_gain, level_exp=level_exp+$exp_gain, npc_wins=npc_wins+1 WHERE char_id='$char_id'";
    if(mysql_query($sql2))
    echo "You won $npc_name. You gain $gold_gain gold and $exp_gain exp.";
    if($level_exp > $level_exp_need){
    $sql2="UPDATE accounts SET exp='0', level_exp_need=$exp_need_1, battle_points=battle_points+3, level=level+1 WHERE char_id='$char_id'";
    if(mysql_query($sql2)) echo "You also gained a level. You recieve 3 Battle points.";
    }
    die(include_once('bottom_html.php'));
  }
  echo "<font color=\"Red\">$npc_name attacked you for $npc_damage life.</font>";
  //update player health.
  $sql2="UPDATE accounts SET health=health-$npc_damage WHERE char_id='$char_id'";
  if(mysql_query($sql2)) $health_1=($health-$npc_damage);
  //If Lose
  if($health_1<1){
    $gold_lose = rand(10,25);
    $sql2="UPDATE accounts SET gold=gold-$gold_lose, npc_losses=npc_losses+1, health='0' WHERE char_id='$char_id'";
    if(mysql_query($sql2)) echo "You lost to $npc_name. You lose $gold_lose gold.";
    die(include_once('bottom_html.php'));
  }
  //End If Lose 
  }
}
}
?>
</TD>
<?php
include_once('bottom_html.php');
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/34296-while-loops/
Share on other sites

i do modify in several places

[code]$sql2="UPDATE npcs SET npc_health=npc_health-$player_damage WHERE npc_id='$search'";[/code]

[code]$sql2="UPDATE npcs SET npc_health=npc_health-$player_damage WHERE npc_id='$search'";[/code]

[code]$sql2="UPDATE accounts SET health=health-$npc_damage WHERE char_id='$char_id'";[/code]

they are all the updates then i have

[code]$npc_health_1=($npc_health-$player_damage);
    if($npc_health_1 < 1){...blah...}[/code]

[code]$npc_health_1=($npc_health-$player_damage);
  if($npc_health_1 < 1){...blah...}[/code]

[code]$health_1=($health-$npc_damage);
  //If Lose
  if($health_1<1){...blah...}[/code]

after each thing to make sure it is defined
Link to comment
https://forums.phpfreaks.com/topic/34296-while-loops/#findComment-161533
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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