## Forum / General / Suicide bombing

I wrote a simulator to find the optimal number of suicide bombers in a n vs m cowboy battle where n is the number of allied cowboys and m is the number of enemy cowboys with a few assumptions. Here is the result. Each element of the array shows the (number of bombers + 1) (to allow for signs when the optimal number is 0 bombers). A negative number means we lose. A positive number means we win.

//how many robots should suicide. negative number if we will lose. n-1 = the number of bombs
// n vs m is in element (n-1)*25+(m-1)
final static int[] battleInfo = { 1, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2,
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -1,
-1, -1, -1, -1, -1, -1, 1, 1, 2, -4, -4, -4, -4, -4, -4, -4, -4,
-4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, 1, 1, 1, 2,
4, -4, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
-5, -5, -5, -5, 1, 1, 1, 2, 4, 4, 5, -5, -6, -6, -6, -6, -6, -6,
-6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, 1, 1, 1, 1, 1, 4, 4, 5,
5, -6, -6, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7,
1, 1, 1, 1, 2, 4, 4, 5, 5, 6, -6, -7, -7, -7, -8, -8, -8, -8, -8,
-8, -8, -8, -8, -8, -8, 1, 1, 1, 1, 1, 1, 4, 5, 5, 5, 6, -6, -6,
-7, -8, -8, -8, -9, -9, -9, -9, -9, -9, -9, -9, 1, 1, 1, 1, 1, 1,
1, 4, 5, 5, 5, 6, -6, -6, -6, -6, -8, -9, -9, -9, -10, -10, -10,
-10, -10, 1, 1, 1, 1, 1, 1, 1, 4, 5, 5, 5, 6, 6, 7, -7, -7, -8, -8,
-9, -9, -10, -10, -10, -11, -11, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5,
6, 6, 6, 7, 7, -7, -7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1, 1, 1, 1, 1, 1, 1, 1, 5, 6, 6, 6, 7, 7,
8, 9, 9 };

//Also here is the code should anyone wish to play with it.

/*
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY Andrew Wells ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrew Wells OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of Andrew Wells.
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int comp(const void *, const void *);

int main(int argc, char *argv[]) {
double army1[100];
double army2[100];
int size1, size2;
int effectiveness = 3;
for (int s1 = 1; s1 < 26; s1++) {
for (int s2 = 1; s2 < 26; s2++) {
double lastScore = -1000000000;
int bestB = 0;
for (int numB = 0; numB <= s1; numB++) {
size1 = s1;
size2 = s2;
int bombers = numB;
// size1 = 1;
// size2 = 1;
// bombers = 1;
for (int i = 0; i < size1; i++) {
army1[i] = 100;
}
for (int i = 0; i < size2; i++) {
army2[i] = 100;
}
//simulate bombers
if (bombers > 0) {
size1 -= bombers;
int extra = size2 % (bombers * 15);
bombers -= size2 / (bombers * 15);

if (bombers > 0) {
for (int i = 0; i < 3 && i < size2; i++) {
army2[i] -= 40 + 0.5 * (100 - (extra * 15));
}
}
bombers--;
int j = 0;
for (int i = size2 - 1;
i >= 0 && j < effectiveness * bombers - 1;
i--, j++) {
army2[i] -= 90;
}
}
//sort army2 by damage:
qsort(army2, size2, sizeof(*army2), comp);

int n;
// printf("start\n");
// for(n = 0; n < size2; n++) {
// printf("%lf ",army2[n]);
// }
for (n = 0; n < size2; n++) {
if (army2[n] <= 0) {
break;
}
}
size2 = n;

//simulate fighting
// printf("%lf %lf %lf %lf %d\n", army2[0], army2[1], army2[2], army2[size2-1], size2);
// int round = 0;
size1--;
size2--;
while (size1 >= 0 && size2 >= 0) {
for (int i = 0;
(i <= size1 || i <= size2) && size1 >= 0
&& size2 >= 0; i++) {
if (i <= size1) {
army2[size2] -= 10;
if (army2[size2] <= 0) {
size2--;
}
}
if (i <= size2) {
army1[size1] -= 10;
if (army1[size1] <= 0) {
size1--;
}
}
}
// printf("round: %d size 1: %d %lf size 2: %d %lf\n",
// ++round, size1, army1[size1], size2, army2[size2]);
}

double score;
if (size1 > size2) {
double sumHP = 0;
size1++;
for (int i = 0; i < size1; i++) {
sumHP += (army1[i] < 0 ? 0 : army1[i]);
}
score = (size1) * sumHP;
} else if (size1 < size2) {
// printf("c2");
double sumHP = 0;
size2++;
for (int i = 0; i < size2; i++) {
sumHP += (army2[i] < 0 ? 0 : army2[i]);
}
score = -(size2) * sumHP;
} else {
// printf("else");
if (size1 == -1)
score = 0;
else {
double a1 = army1[size1];
double a2 = army2[size2];
score = (a1 < 0 ? 0 : a1) - (a2 < 0 ? 0 : a2);
}
}
if (score > lastScore) {
lastScore = score;
bestB = numB;
// printf("score: %lf %d size1: %d size2: %d, bombers: %d\n",
// score, size1, s1, s2, numB);
}
}
if (lastScore >= 0)
printf("%d,", bestB + 1);
else
printf("%d,", -(bestB + 1));
}
}
return 0;
}

int comp(const void * elem1, const void * elem2) {
int f = *((double*) elem1);
int s = *((double*) elem2);
if (f < s)
return 1;
if (f > s)
return -1;
return 0;
}