In another question, you helped me to build a simulation algorithm for soccer. I got some very good answers there. Thanks again!
Now I\'ve coded this algorithm. I would
In general, it looks like this is a fairly complicated problem, and I'm not sure how efficient you'll get it.
That said, I have seen some things which would decidedly help you.
First I would type the variables in the parameters. This may not necessarily make your code faster, but it would make it easier to read and debug. Next, I would remove the $teamname_att, $teamname_def parameters and simply have those as values in the associative $strength_att, $strength_def arrays. Since this data is always paired up anyway, this will reduce the risk of accidentally using one team's name as a reference to the other team.
This will make it so you will not have to continually look up values in arrays:
// replace all $tactics[$teamname_att] with $attackers
$attackers = $tactics[$teamname_att];
$defenders = $tactics[$teamname_def];
// Now do the same with arrays like $_POST[ "team1" ];
You have three helper functions which all have the pattern:
function foo( $arg ){
$bar = $arg * $value;
return $bar;
}
Since this means that you have to create an extra variable (something which can be costly) each time you run the function, use these instead:
function tactics_weight($wert) {
return $wert*0.1+0.8;
}
function strengths_weight($wert) {
return log10($wert+1)+0.35;
}
/*
Perhaps I missed it, but I never saw Chance_Percent( $num1, $num2 )
consider using this function instead: (one line instead of four, it also
functions more intuitively, Chance_Percent is your chance out of 100
(or per cent)
function Chance_Percent( $chance ) {
return (mt_rand(1, 100) <= $chance);
}
*/
function Chance_Percent($chance, $universe = 100) {
$chance = abs(intval($chance)); // Will you always have a number as $chance?
// consider using only abs( $chance ) here.
$universe = abs(intval($universe));
return (mt_rand(1, $universe) <= $chance);
}
I couldn't help but notice this pattern coming up consistently:
$matchReport .= ' ' . comment_action($teamname_att, 'attack');
My general experience is that if you move the concatenation of $matchReport into comment_action, then it will be just slightly faster (Generally less than a dozen milliseconds, but since you're calling that function a half-dozen times inside of a recursive function, this could shave a couple tenths of a second per running).
I think that this would flow much better (both from a reader's perspective, and from
Finally, there are several times where you will use the same call to the same function with the same parameter. Make that call up front:
$goalieStrength = strengths_weight($strength_def['goalkeeper']);
Hope this helps.