I'm trying to make some design-level decisions about how best to do this and simulate something that seems statistically realistic.
The way I'm doing it now (for example) has two armies with various accuracy%, damage, health, and sizes.
Which side fires is currently random with 50% probability for either side regardless of the size of the armies.
Here is some example code (the actual game is a little more complicated with each side being able to have multiple types of units)
<?php$accuracy_1 = 50; //a percent$damage_1 = 20;$health_1 = 100;$size_1 = 50;$accuracy_2 = 50; //a percent$damage_2 = 20;$health_2 = 100;$size_2 = 60;//initialize the healths of army 1for ($i=0; $i<$size_1; $i++){ $current_health_1[$i] = $health_1;}echo "initialized all $size_1 of army 1's soldiers to their max current health of $health_1<br>";//initialize the healths of army 2for ($i=0; $i<$size_2; $i++){ $current_health_2[$i] = $health_2;}echo "initialized all $size_2 of army 2's soldiers to their max current health of $health_2<hr>";//main battle loopwhile (($size_1>0) && ($size_2>0)){ $dice = rand(1,2); if ($dice == 1) { //army 1 shoots with a random soldier at a random soldier in army 2 //first we choose a random soldier from army1: $dice = rand(0,$size_1-1); //now we do an accuracy roll to see if the soldier shoots successfully $dice = rand(1,100); if ($dice <= $accuracy_1) { //shot was successful, now we ramdomly pick a soldier he hit $dice = rand(0,$size_2-1); //now we subtract the firing soldier's damage from the targetted soldier's health $current_health_2[$dice] -= $damage_1; //now we see if the shot soldier is dead if ($current_health_2[$dice] <= 0) { //since this soldier died, we replace his health with the soldier who is //highest in the array and reduce the size of the army by 1 $current_health_2[$dice] = $current_health_2[$size_2-1]; $size_2--; } //no else condition needed since if soldier is still alive, there's nothing more to do } //no else condition needed since if the soldier misses, this side's turn is over } else { $dice = rand(1,$size_2); $dice = rand(1,100); if($dice <= $accuracy_2) { $dice = rand(0,$size_1-1); $current_health_1[$dice] -= $damage_2; if ($current_health_1[$dice] <= 0) { $current_health_1[$dice] = $current_health_1[$size_1-1]; $size_1--; } } }}echo "army 1 has $size_1 soldiers left<br>";echo "army 2 has $size_2 soldiers left";?>
Is this reasonable for combat (statistically) or would it make more sense for which side fires to be random with probability in proportion to army size?
example: size_1 = 100, size_2 = 200, so there would be a 2/3 chance of army 2 firing this round and a 1/3 chance of army 1 firing this round.
Also a bigger question:
Is this a reasonable way to simulate things? (eg. with flatly distributed probability, select one soldier from the army, then with flatly distributed probability, select one soldier from the opposing army, then do a single accuracy roll, then subtract 'damage' health)
The reason I lack confidence with my current design is that the results don't conform very well to Lanchester's equations.
Basically, the way I'm doing things now, the smaller 'damage' or 'accuracy' or 'health' is, the bigger the advantage of having a larger army seems to be.
For example, if size_1 = 50 and size_2 = 60, accuracy_1 = accuracy_2 = 50, damage_1 = damage_2 = 1, health_1 = health_2 = 100, then the result of the battle is that army 1 loses every soldier and army 2 loses no more than 5.
This seems too skewed a result for only a 20% larger force, causing me to question the statistical validity of my overall design for battle.