#define NUM_ATTACKERS 10
#define NUM_DEFENDERS 7
#define ATTACK_HIT 1
#define DEFEND_HIT 2
#define TEST_COUNT 10000000
#define OUTPUT_COUNT 200000

int main() {
  long def, att; /* current count for sides */
  long dwins = 0, awins = 0, none = 0; /* total number of wins for each side */
  long dhits, ahits; /* number of hits per round */
  long i, j;

  for (i = 0; i < TEST_COUNT; i++) {
    /* Run a trial combat */
    def = NUM_DEFENDERS;
    att = NUM_ATTACKERS;
    while (att > 0 && def > 0) {
      /* Go through each round until one side wins */
      dhits = ahits = 0;
      for (j = 0; j < def; j++) /* defender attacks */
	if (lrand48() % 6 < DEFEND_HIT)
	  dhits++;
      for (j = 0; j < att; j++) /* attacker attacks */
	if (lrand48() % 6 < ATTACK_HIT)
	  ahits++;
      def -= ahits;
      att -= dhits;
    }
    /* Determine who won */
    if (att > 0)
      awins++;
    else if (def > 0)
      dwins++;
    else
      none++;

    /* Output every so often */
    if (!(i % OUTPUT_COUNT) && i)
      printf("%7d defender wins, %7d attacker wins, %7d contested\n",
	     dwins, awins, none);
  }

  printf("\nTotal: %d defender wins, %d attacker wins, %d contested\n",
	 dwins, awins, none);
  printf("Probability of attacker success: %.3f%% with benefit to defender.\n",
	 100. * ((double) awins) / (double) TEST_COUNT);
  printf("                                 %.3f%% without contested combat.\n",
	 100. * ((double) awins) / (double) (TEST_COUNT - none));;
}

