...

View Full Version : How to simplify code



srule_
01-16-2008, 07:35 PM
How can I re-write this code so it is easy to add new units to the calculation?
the 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.



$toCalaculate = array( 'militia', 'musketeer');
$temp = 0;
$count = count( $toCalaculate );
for( $i = 0; $i < $count; $i++ ){
$damage = ($_POST[$toCalaculate[$i]] * ????) + $temp;
$temp = $damage;
}

ptmuldoon
01-16-2008, 07:55 PM
Well, I'm still pretty green, and learning, but couldn't you simply just add the number of units to the math?


$attackerDamage = ($_POST[militia] * $pikeAttack)+($_POST[musketeer] * $muskAttack);
$attackerDamage = $attackerDamage * $_POST[units];


or this

$attackerDamage = ( (($_POST[militia] * $pikeAttack)+($_POST[musketeer] * $muskAttack)) * $_POST[units] );

srule_
01-16-2008, 08:03 PM
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.


$_POST[someUnit]


the total attack of of some unit to be used:


$_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.

schleppel
01-16-2008, 10:28 PM
A key=>value array should work

<?php

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

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

srule_
01-17-2008, 02:07 AM
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?

schleppel
01-17-2008, 06:53 PM
You could use a multidimensional array

<?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

$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']



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum