Go Back   openSUSE Forums > Archives > SF Archives > ARCHIVES - Programming & Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read


ARCHIVES - Programming & Scripting A place to discuss website design, programming, shell scripts, etc

 
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-Nov-2007, 11:16
Phoenix76
Guest
 
Posts: n/a
Default

If you've played the Risk strategy/conquest board game before, you know that sometimes there are battles that involve multiple rolls of the dice. Sometimes even 50 or 100 dice rolls are required to determine the outcome of a battle. The attacker has a slight advantage in this game because he can roll up to three dice while attacking, and the defender can only roll two. With each roll of the dice, the attacker's two highest dice are paired with the defender's two dice. Comparing each die singly, if the defender's die is equal or higher, the attacker loses one unit. If the attacker's die is higher, the defender loses one unit. This program prompts the user to enter the size of the attacking army and the size of the defending army on an adjacent territory, then "rolls" the dice automatically by generating random numbers. It then compares the attacker's and defender's dice to determine who has won that particular roll. The program rolls continuously until the battle is almost concluded, and then the players can finish off the battle by hand with one final roll of the dice.

One of the side effects of writing this program is that it is found how much of an advantage the attacker has over the defender. Since the attacker can roll up to three dice to the defender's two, he has approximately a 15% advantage over the defender. By running the program with large and equal numbers of forces on both sides, it is found that the attacker usually loses 15% fewer forces than the defender in large battles. It is also found that the defender fares much better in smaller battles than in large ones.

Code:
/* This program determines the outcome of a battle in the Risk board game */

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

main(){

srand((unsigned) time(NULL));

int done = 0; /* Used to run the program continuously */
int attackers, defenders; /* Number of attackers and defenders */
int init_attackers, init_defenders; /* Initial numbers of attackers, defenders */
int ratio; /* Ratio of attackers to defenders */
int atk_lost, def_lost; /* Number of casualties on both sides */
int i, j; /* used as a counter */

while(!done){
** printf("\nNumber of attacking forces: ");
** scanf("%d", &attackers);
** printf("Number of defending forces: ");
** scanf("%d", &defenders);
** printf("\n");

** init_attackers = attackers;
** init_defenders = defenders;

** /* atk_dice and def_dice are character arrays that hold the value of the
** dice rolls for the attacker and defender. The rolls range from 0-5 instead
** of 1-6, so if the dice rolls are to be displayed, then 1 must be added to
** each die. atk_ord and def_ord are character arrays that hold the rolls in
** order from highest die to lowest die. This is so that the attacker's
** highest dice can be compared to the defender's highest dice, making it
** easier to determine who has won or lost pieces. */

** while( attackers > 4 && defenders > 3){

** char atk_dice[]={'0','0','0'}; /* The attacker's roll, in random order */
** char def_dice[]={'0','0'};**** /* The defender's roll, in random order */
** char atk_ord[]={'0','0','0'};**/* The attacker's roll, highest to lowest */
** char def_ord[]={'0','0'};******/* The defender's roll, highest to lowest */
** char temp[]={'0','0','0'};**** /* Temporary storage array */
** for (i = 0; i < 3; i++) {atk_dice[i] = rand() % 6;} /* Generates random dice rolls */
** for (j = 0; j < 2; j++) {def_dice[j] = rand() % 6;}

** /* First pass through the dice to get them in order */
** if (atk_dice[0] < atk_dice[1]){
******temp[0] = atk_dice[0];
******temp[1] = atk_dice[1];
******atk_dice[0] = temp[1];
******atk_dice[1] = temp[0];
** }

** if (atk_dice[1] < atk_dice[2]){
******temp[1] = atk_dice[1];
******temp[2] = atk_dice[2];
******atk_dice[1] = temp[2];
******atk_dice[2] = temp[1];
** }

** /* Second pass */
** if (atk_dice[0] < atk_dice[1]){
******temp[0] = atk_dice[0];
******temp[1] = atk_dice[1];
******atk_dice[0] = temp[1];
******atk_dice[1] = temp[0];
** }

** if (atk_dice[1] < atk_dice[2]){
******temp[1] = atk_dice[1];
******temp[2] = atk_dice[2];
******atk_dice[1] = temp[2];
******atk_dice[2] = temp[1];
** }

** atk_ord[0] = atk_dice[0];
** atk_ord[1] = atk_dice[1];
** atk_ord[2] = atk_dice[2];


** /* Only one pass needed to get defense dice in order */
** if (def_dice[0] > def_dice[1]){
******def_ord[0] = def_dice[0];
******def_ord[1] = def_dice[1];
** }
** else{
******def_ord[1] = def_dice[0];
******def_ord[0] = def_dice[1];
** }
** /* Add 1 to each die so that they range from 1-6 instead of 0-5 */
** printf("Attacker's roll: %d %d %d\n", (atk_ord[0] + 1), (atk_ord[1] + 1), (atk_ord[2] + 1));
** printf("Defender's roll: %d %d\n\n", (def_ord[0] + 1), (def_ord[1] + 1));

******if (atk_ord[0] > def_ord[0])
******** defenders--;
******else attackers--;

******if (atk_ord[1] > def_ord[1])
******** defenders--;
******else attackers--;
** }

** printf("Remaining attackers: %d. \tAttacker lost %d forces.\n", attackers, (init_attackers - attackers));
** printf("Remaining defenders: %d. \tDefender lost %d forces.\n\n", defenders, (init_defenders - defenders));


}

return 0;
}
Some output from the program:
Code:
Number of attacking forces: 10
Number of defending forces: 8

Attacker's roll: 2 2 1
Defender's roll: 5 4

Attacker's roll: 5 4 2
Defender's roll: 3 1

Attacker's roll: 4 2 1
Defender's roll: 4 2

Attacker's roll: 5 4 2
Defender's roll: 6 4

Remaining attackers: 4.******** Attacker lost 6 forces.
Remaining defenders: 6.******** Defender lost 2 forces.


Number of attacking forces:
 

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2