Code to calculate Risk dice odds

/*
 * odds.c -- calculate the odds of winning/losing various dice
 *   configurations in the game "Risk"
 */

#include	<stdio.h>

main()
{
	int a1=0, a2=0, a3=0, d1=0, d2=0;
	int atotal=0, dtotal=0;
	int atotalhi=0, atotalmid=0;
	int dtotalhi=0, dtotalmid=0;
	int total=0;
	int ahi=0, amid=0, dhi=0, dmid=0;
	int hiwin=0, midwin=0;
	int bothwin=0, bothlose=0, oneandone=0;
	float atotalperc=0.0, dtotalperc=0.0, oneandonetotalperc=0;

/*
 * One attacker die vs. one defender die (this one is easy: just whether
 *   or not the defender's die is equal to or higher than the attacker's)
 */

	for(a1 = 1; a1 <= 6; a1++) {
	  for(d1 = 1; d1 <= 6; d1++) {
	    ++total;
#ifdef DEBUG2
	    printf("a1 is %d, d1 is %d, total is %d\n",a1,d1,total);
#endif DEBUG2
	    if(a1 > d1) {
	      ++atotal;
#ifdef DEBUG
	    printf("%d vs. %d: Attacker wins!\n",a1,d1);
#endif DEBUG
	    } else {
	      ++dtotal;
#ifdef DEBUG
	    printf("%d vs. %d: Defender wins!\n",a1,d1);
#endif DEBUG
	    }
#ifdef DEBUG2
	    printf("atotal is %d, dtotal is %d\n",atotal,dtotal);
#endif DEBUG2
	  }
	}
	atotalperc = (float) atotal * 100 / total;
	dtotalperc = (float) dtotal * 100 / total;
	printf("\n");
	printf("Attacker: one die; Defender: one die:\n");
	printf("\n\t");
	printf("Attacker wins %d out of %d (%.2f %%)\n",atotal,total,atotalperc);
	printf("\t");
	printf("Defender wins %d out of %d (%.2f %%)\n",dtotal,total,dtotalperc);
	printf("\n");

/*
 * reset all the variables
 */

	a1=0; a2=0; a3=0; d1=0; d2=0; atotal=0; dtotal=0;
	atotalhi=0; atotalmid=0; dtotalhi=0; dtotalmid=0;
	total=0; ahi=0; amid=0; dhi=0; dmid=0; hiwin=0; midwin=0;
	bothwin=0; bothlose=0; oneandone=0;
	atotalperc=0.0; dtotalperc=0.0; oneandonetotalperc=0;

/*
 * Two attacker dice vs. one defender die (this one is pretty easy also:
 *   if the defender's die is greater than or equal to both of the attacker's
 *   dice, then the defender wins)
 */

	for(a1 = 1; a1 <= 6; a1++) {
	  for(a2 = 1; a2 <= 6; a2++) {
	    for(d1 = 1; d1 <= 6; d1++) {
	      ++total;
#ifdef DEBUG2
	      printf("a1 is %d, a2 is %d, d1 is %d, total is %d\n",a1,a2,d1,total);
#endif DEBUG2
	      if(a1 > d1 || a2 > d1) {
	        ++atotal;
#ifdef DEBUG
	    printf("%d - %d vs. %d: Attacker wins!\n",a1,a2,d1);
#endif DEBUG
	      } else {
	        ++dtotal;
#ifdef DEBUG
	    printf("%d - %d vs. %d: Defender wins!\n",a1,a2,d1);
#endif DEBUG
	      }
#ifdef DEBUG2
	      printf("atotal is %d, dtotal is %d\n",atotal,dtotal);
#endif DEBUG2
	    }
	  }
	}
	atotalperc = (float) atotal * 100 / total;
	dtotalperc = (float) dtotal * 100 / total;
	printf("\n");
	printf("Attacker: two dice; Defender: one die:\n");
	printf("\n\t");
	printf("Attacker wins %d out of %d (%.2f %%)\n",atotal,total,atotalperc);
	printf("\t");
	printf("Defender wins %d out of %d (%.2f %%)\n",dtotal,total,dtotalperc);
	printf("\n");


/*
 * reset all the variables
 */

	a1=0; a2=0; a3=0; d1=0; d2=0; atotal=0; dtotal=0;
	atotalhi=0; atotalmid=0; dtotalhi=0; dtotalmid=0;
	total=0; ahi=0; amid=0; dhi=0; dmid=0; hiwin=0; midwin=0;
	bothwin=0; bothlose=0; oneandone=0;
	atotalperc=0.0; dtotalperc=0.0; oneandonetotalperc=0;

/*
 * Three attacker dice vs. one defender die (still pretty simple: if the
 *   defender's die is greater than or equal to all three of the attacker's
 *   dice, then the defender wins)
 */

	for(a1 = 1; a1 <= 6; a1++) {
	  for(a2 = 1; a2 <= 6; a2++) {
	    for(a3 = 1; a3 <= 6; a3++) {
	      for(d1 = 1; d1 <= 6; d1++) {
	        ++total;
#ifdef DEBUG2
	        printf("a1 is %d, a2 is %d, d1 is %d, total is %d\n",a1,a2,d1,total);
#endif DEBUG2
	        if(a1 > d1 || a2 > d1 || a3 > d1) {
	          ++atotal;
#ifdef DEBUG
	    printf("%d - %d - %d vs. %d: Attacker wins!\n",a1,a2,a3,d1);
#endif DEBUG
	        } else {
	          ++dtotal;
#ifdef DEBUG
	    printf("%d - %d - %d vs. %d: Defender wins!\n",a1,a2,a3,d1);
#endif DEBUG
	        }
#ifdef DEBUG2
	      printf("atotal is %d, dtotal is %d\n",atotal,dtotal);
#endif DEBUG2
	      }
	    }
	  }
	}
	atotalperc = (float) atotal * 100 / total;
	dtotalperc = (float) dtotal * 100 / total;
	printf("\n");
	printf("Attacker: three dice; Defender: one die:\n");
	printf("\n\t");
	printf("Attacker wins %d out of %d (%.2f %%)\n",atotal,total,atotalperc);
	printf("\t");
	printf("Defender wins %d out of %d (%.2f %%)\n",dtotal,total,dtotalperc);
	printf("\n");

/*
 * reset all the variables
 */

	a1=0; a2=0; a3=0; d1=0; d2=0; atotal=0; dtotal=0;
	atotalhi=0; atotalmid=0; dtotalhi=0; dtotalmid=0;
	total=0; ahi=0; amid=0; dhi=0; dmid=0; hiwin=0; midwin=0;
	bothwin=0; bothlose=0; oneandone=0;
	atotalperc=0.0; dtotalperc=0.0; oneandonetotalperc=0;

/*
 * One attacker die vs. two defender dice (this one is also pretty simple:
 *   the attacker's die must be greater than both the defender's dice)
 */

	for(a1 = 1; a1 <= 6; a1++) {
	  for(d1 = 1; d1 <= 6; d1++) {
	    for(d2 = 1; d2 <= 6; d2++) {
	      ++total;
#ifdef DEBUG2
	      printf("a1 is %d, d1 is %d, d2 is %d, total is %d\n",a1,d1,d2,total);
#endif DEBUG2
	      if(a1 > d1 && a1 > d2) {
	        ++atotal;
#ifdef DEBUG
	    printf("%d vs. %d - %d: Attacker wins!\n",a1,d1,d2);
#endif DEBUG
	      } else {
	        ++dtotal;
#ifdef DEBUG
	    printf("%d vs. %d - %d: Defender wins!\n",a1,d1,d2);
#endif DEBUG
	      }
#ifdef DEBUG2
	      printf("atotal is %d, dtotal is %d\n",atotal,dtotal);
#endif DEBUG2
	    }
	  }
	}
	atotalperc = (float) atotal * 100 / total;
	dtotalperc = (float) dtotal * 100 / total;
	printf("\n");
	printf("Attacker: one die; Defender: two dice:\n");
	printf("\n\t");
	printf("Attacker wins %d out of %d (%.2f %%)\n",atotal,total,atotalperc);
	printf("\t");
	printf("Defender wins %d out of %d (%.2f %%)\n",dtotal,total,dtotalperc);
	printf("\n");

/*
 * reset all the variables
 */

	a1=0; a2=0; a3=0; d1=0; d2=0; atotal=0; dtotal=0;
	atotalhi=0; atotalmid=0; dtotalhi=0; dtotalmid=0;
	total=0; ahi=0; amid=0; dhi=0; dmid=0; hiwin=0; midwin=0;
	bothwin=0; bothlose=0; oneandone=0;
	atotalperc=0.0; dtotalperc=0.0; oneandonetotalperc=0;

/*
 * Two attacker dice vs. two defender dice (now things get a bit more
 *   complicated since we must compare the attacker's hi die to the
 *   defender's hi die, and the attacker's low die to the defender's
 *   low die)
 */

	for(a1 = 1; a1 <= 6; a1++) {
	  for(a2 = 1; a2 <= 6; a2++) {
	    for(d1 = 1; d1 <= 6; d1++) {
	      for(d2 = 1; d2 <= 6; d2++) {
	        ++total;
		/*
		 * Figure out which attacker die is highest
		 */
		if(a1 >= a2) {
			ahi = a1;
			amid = a2;
		} else {
			ahi = a2;
			amid = a1;
		}
		/*
		 * Figure out which defender die is highest
		 */
		if(d1 >= d2) {
			dhi = d1;
			dmid = d2;
		} else {
			dhi = d2;
			dmid = d1;
		}
#ifdef DEBUG2
	        printf("a1 is %d, a2 is %d, d1 is %d, d2 is %d, total is %d\n",a1,a2,d1,d2,total);
	        printf("Attacker: %d - %d: high is %d, low is %d\n",a1,a2,ahi,amid);
	        printf("Defender: %d - %d: high is %d, low is %d\n",d1,d2,dhi,dmid);
#endif DEBUG2
		/*
		 * Now, compare attacker hi to defender hi and attacker low
		 *   to defender low
		 */
	        if(ahi > dhi) {
	          hiwin = 1;
		  ++atotalhi;
	        } else {
	          hiwin = 0;
		  ++dtotalhi;
	        }
	        if(amid > dmid) {
	          midwin = 1;
		  ++atotalmid;
	        } else {
	          midwin = 0;
		  ++atotalmid;
		}
		if(hiwin && midwin) {
			++bothwin;
#ifdef DEBUG
	    printf("%d - %d vs. %d - %d: Attacker wins both!\n",a1,a2,d1,d2);
#endif DEBUG
		} else if((hiwin == 0) && (midwin == 0)) {
			++bothlose;
#ifdef DEBUG
	    printf("%d - %d vs. %d - %d: Defender wins both!\n",a1,a2,d1,d2);
#endif DEBUG
		} else {
			++oneandone;
#ifdef DEBUG
	    printf("%d - %d vs. %d - %d: ONE AND ONE!\n",a1,a2,d1,d2);
#endif DEBUG
		}
#ifdef DEBUG2
	      printf("atotal is %d, dtotal is %d\n",atotal,dtotal);
#endif DEBUG2
	      }
	    }
	  }
	}
	atotalperc = (float) bothwin * 100 / total;
	dtotalperc = (float) bothlose * 100 / total;
	oneandonetotalperc = (float) oneandone * 100 / total;
	printf("\n");
	printf("Attacker: two dice; Defender: two dice:\n");
	printf("\n\t");
	printf("Attacker wins both: %d out of %d (%.2f %%)\n",bothwin,total,atotalperc);
	printf("\t");
	printf("Defender wins both: %d out of %d (%.2f %%)\n",bothlose,total,dtotalperc);
	printf("\t");
	printf("Both win one: %d out of %d (%.2f %%)\n",oneandone,total,oneandonetotalperc);
	printf("\n");

/*
 * reset all the variables
 */

	a1=0; a2=0; a3=0; d1=0; d2=0; atotal=0; dtotal=0;
	atotalhi=0; atotalmid=0; dtotalhi=0; dtotalmid=0;
	total=0; ahi=0; amid=0; dhi=0; dmid=0; hiwin=0; midwin=0;
	bothwin=0; bothlose=0; oneandone=0;
	atotalperc=0.0; dtotalperc=0.0; oneandonetotalperc=0;

/*
 * Three attacker dice vs. two defender dice (the most complex configuration:
 *   we need to figure the attacker's highest die and second-highest die,
 *   then compare them respectively to the defenders hi and low)
 */

	for(a1 = 1; a1 <= 6; a1++) {
	  for(a2 = 1; a2 <= 6; a2++) {
	    for(a3 = 1; a3 <= 6; a3++) {
	      for(d1 = 1; d1 <= 6; d1++) {
	        for(d2 = 1; d2 <= 6; d2++) {
	          ++total;
	 	  if(a1 >= a2 && a1 >= a3) {
	  		ahi = a1;
			if (a2 >= a3)
			  amid = a2;
			else
			  amid = a3;
	  	  } else if(a2 >= a1 && a2 >= a3) {
	  		ahi = a2;
			if (a1 >= a3)
			  amid = a1;
			else
			  amid = a3;
	  	  } else if(a3 >= a1 && a3 >= a2) {
	  		ahi = a3;
			if (a1 >= a2)
			  amid = a1;
			else
			  amid = a2;
		  }
		  if(d1 >= d2) {
			dhi = d1;
			dmid = d2;
		  } else {
			dhi = d2;
			dmid = d1;
		  }
#ifdef DEBUG2
	        printf("a1 is %d, a2 is %d, a3 is %d, d1 is %d, d2 is %d, total is %d\n",a1,a2,a3,d1,d2,total);
	        printf("Attacker: %d - %d - %d: high is %d, mid is %d\n",a1,a2,a3,ahi,amid);
	        printf("Defender: %d - %d: high is %d, low is %d\n",d1,d2,dhi,dmid);
#endif DEBUG2
	          if(ahi > dhi) {
	            hiwin = 1;
		    ++atotalhi;
	          } else {
	            hiwin = 0;
		    ++dtotalhi;
	          }
	          if(amid > dmid) {
	            midwin = 1;
		    ++atotalmid;
	          } else {
	            midwin = 0;
		    ++atotalmid;
		  }
		  if(hiwin && midwin) {
			++bothwin;
#ifdef DEBUG
	    printf("%d - %d - %d vs. %d - %d: Attacker wins both!\n",a1,a2,a3,d1,d2);
#endif DEBUG
		  } else if((hiwin == 0) && (midwin == 0)) {
			++bothlose;
#ifdef DEBUG
	    printf("%d - %d - %d vs. %d - %d: Defender wins both!\n",a1,a2,a3,d1,d2);
#endif DEBUG
		  } else {
			++oneandone;
#ifdef DEBUG
	    printf("%d - %d - %d vs. %d - %d: ONE AND ONE!\n",a1,a2,a3,d1,d2);
#endif DEBUG
		  }
#ifdef DEBUG2
	      printf("atotal is %d, dtotal is %d\n",atotal,dtotal);
#endif DEBUG2
		}
	      }
	    }
	  }
	}
	atotalperc = (float) bothwin * 100 / total;
	dtotalperc = (float) bothlose * 100 / total;
	oneandonetotalperc = (float) oneandone * 100 / total;
	printf("\n");
	printf("Attacker: three dice; Defender: two dice:\n");
	printf("\n\t");
	printf("Attacker wins both: %d out of %d (%.2f %%)\n",bothwin,total,atotalperc);
	printf("\t");
	printf("Defender wins both: %d out of %d (%.2f %%)\n",bothlose,total,dtotalperc);
	printf("\t");
	printf("Both win one: %d out of %d (%.2f %%)\n",oneandone,total,oneandonetotalperc);
	printf("\n");
}