Jump to content

Helping tighten this code with variables


cliftonbazaar

Recommended Posts

I currently use long variable names so I know what each one is at a glance, I also use a two dimensional array.

 

In the current piece of code I have tried to tighten it from

for($i=1; $i<12; $i++) {
$battingPlayer[$battingLineUp[$i]]['energy'] += mt_rand(10, floor($battingPlayer[$battingLineUp[$i]]['fitness']/10+10)); #If energy is low then player gets a bit of it back
$bowlingPlayer[$bowlingLineUp[$i]]['energy'] += mt_rand(10, floor($bowlingPlayer[$bowlingLineUp[$i]]['fitness']/10+10)); #If energy is low then player gets a bit of it back
if($battingPlayer[$battingLineUp[$i]]['energy'] > $battingPlayer[$battingLineUp[$i]]['fitness'])
$battingPlayer[$battingLineUp[$i]]['energy'] = $battingPlayer[$battingLineUp[$i]]['fitness']; #Energy cannot be more than fitness
if($bowlingPlayer[$bowlingLineUp[$i]]['energy'] > $bowlingPlayer[$bowlingLineUp[$i]]['fitness'])
$bowlingPlayer[$bowlingLineUp[$i]]['energy'] = $bowlingPlayer[$bowlingLineUp[$i]]['fitness']; #Energy cannot be more than fitness
}

to this

for($i=1; $i<12; $i++) {
for($j=0; $j<2; $j++) {
if($j) {$x = $battingPlayer[$battingLineUp[$i]];} else {$x = $bowlingPlayer[$bowlingLineUp[$i]];}
echo "<br>old energy is ".$x['energy']." and now they have ";
$x['energy'] -= mt_rand(10, floor($x['fitness']/10+10)); #If energy is low then player gets a bit of it back
if($x['energy'] > $x['fitness']) $x['energy'] = $x['fitness']; #Energy cannot be more than fitness
echo $x['energy'];
}
}

 

While the echo statements show an increase in the number($x['energy']) the variable is not increased when it is used with the full variable name! ($battingPlayer[$battingLineUp[$i]]['energy']).

 

I know one method is to use shorter variable names but I'm very happy with how they are :)

You can throw in references to get a shorter variable temporarily.

for($i=1; $i<12; $i++) {
$batter =& $battingPlayer[$battingLineUp[$i]];
$bowler =& $bowlingPlayer[$bowlingLineUp[$i]];
$batter['energy'] += mt_rand(10, floor($batter['fitness']/10+10)); #If energy is low then player gets a bit of it back
$bowler['energy'] += mt_rand(10, floor($bowler['fitness']/10+10)); #If energy is low then player gets a bit of it back
if($batter['energy'] > $batter['fitness'])
$batter['energy'] = $batter['fitness']; #Energy cannot be more than fitness
if($bowler['energy'] > $bowler['fitness'])
$bowler['energy'] = $bowler['fitness']; #Energy cannot be more than fitness
}
unset($batter, $bowler);

Note the unset(): references can be tricky and might bite you if you're not careful.

 

Can also throw in a couple calls to min().

for($i=1; $i<12; $i++) {
$batter =& $battingPlayer[$battingLineUp[$i]];
$bowler =& $bowlingPlayer[$bowlingLineUp[$i]];
$batter['energy'] += mt_rand(10, floor($batter['fitness']/10+10)); #If energy is low then player gets a bit of it back
$bowler['energy'] += mt_rand(10, floor($bowler['fitness']/10+10)); #If energy is low then player gets a bit of it back
$batter['energy'] = min($batter['energy'], $batter['fitness']); #Energy cannot be more than fitness
$bowler['energy'] = min($bowler['energy'], $bowler['fitness']); #Energy cannot be more than fitness
}
unset($batter, $bowler);

Can even combine the two pairs of $player['energy']= together, though the line gets pretty long.

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.