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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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