Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts

    How to simplify code

    How can I re-write this code so it is easy to add new units to the calculation?
    the code:
    PHP Code:
        //Calaculate Attackers Damage 
        
    $attackerDamage = ($_POST[militia] * $pikeAttack)+($_POST[musketeer] * $muskAttack); 
    I was trying to do it by using a loop, but I can not figure out how to make it multiply the unit amount by the correct damage value as it changes for each unit. I made the following loop, but i have question marks in the area that needs to change for each damage value.

    PHP Code:
            $toCalaculate = array( 'militia''musketeer');
                
    $temp 0;
                
    $count count$toCalaculate );
                for( 
    $i 0$i $count$i++ ){
                   
    $damage = ($_POST[$toCalaculate[$i]] * ????) + $temp;
                   
    $temp $damage;
                } 
    Last edited by srule_; 01-16-2008 at 09:51 PM.

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Well, I'm still pretty green, and learning, but couldn't you simply just add the number of units to the math?
    PHP Code:
    $attackerDamage = ($_POST[militia] * $pikeAttack)+($_POST[musketeer] * $muskAttack);  
    $attackerDamage $attackerDamage $_POST[units]; 
    or this
    PHP Code:
    $attackerDamage = ( (($_POST[militia] * $pikeAttack)+($_POST[musketeer] * $muskAttack)) * $_POST[units] ); 

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    My prob is not finding the number of units, It is finding how much damage all the unit do since each unit type does different damage.

    Holds the number of some unit the person wants to use.
    PHP Code:
    $_POST[someUnit
    the total attack of of some unit to be used:
    PHP Code:
    $_POST[someUnit] * someAttack 

    I can simple keep adding units to my code but the problem is I will have lots of units (10+). If I keep adding them it will be annoying, hard to read & messy to add units.
    Last edited by srule_; 01-16-2008 at 09:07 PM.

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Posts
    376
    Thanks
    0
    Thanked 25 Times in 25 Posts
    A key=>value array should work
    PHP Code:
    <?php

    $toCalaculate 
    = array('militia' => 20'musketeer' => 30 /*, 'unit' => attack */)

    $damage 0;
    foreach(
    $toCalculate as $unit => $attack)
    {
      
    $damage += ($_POST[$unit] * $attack);
    }

  • #5
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    Thank, you that works good. But now the trick is I want to preform the exact same calculations but with HitPoints instead of Attack.

    should I make a new array with hitpoints as the key, or is there a better way?

  • #6
    Regular Coder
    Join Date
    Oct 2004
    Posts
    376
    Thanks
    0
    Thanked 25 Times in 25 Posts
    You could use a multidimensional array
    PHP Code:
    <?php

    $toCalaculate 
    = array(
      
    'militia' => array(
        
    'attack' => 20,
        
    'hit' => 3
        
    ),
      
    'musketeer' => array(
        
    'attack' => 30,
        
    'hit' => 3
        
    )/*,
      'unit' => array(
        'attack' => attack_points,
        'hit' => hit_points
        )*/
      
    );

    $damage $hitpointcount 0;
    foreach(
    $toCalculate as $unit => $points)
    {
      
    $damage += ($_POST[$unit] * $points['attack']);
      
    $hitpointcount += ($_POST[$unit] * $points['hit']);
    }
    Then if you want to add another type of points (health) you just add another key and value to the arrays
    PHP Code:
    $toCalaculate = array(
      
    'militia' => array(
        
    'attack' => 20,
        
    'hit' => 3,
        
    'health' => 5
        
    )/*,
      'unit' => array(
        'attack' => attack_points,
        'hit' => hit_points,
        'health' => health_points
        )*/
      
    ); 
    and access it with $points['health']


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •