Programming Risk Combat
In a simplified version of the board game Risk, combat follows the following rules:
- Two players, an attacker and a defender have a squad of units. Each unit in a squad rolls a 6-sided die to determine their damage value.
- The rolls of the units are ranked in order and rolls of the same rank on each side are compared against each other. So the largest roll on the defender’s side to compared against the largest roll on the attacker’s side, the second-largest roll on the defender’s is compared against the second-largest roll on the attack’s side, etc. In each match-up, the side with the higher roll gains a point.
- If there is a tie in a match-up, the defender gains a point.
- If the attacker and defender have a different number of units, only match-ups between ranks less than or equal to the number of units in the squad with a lower number of units are considered.
- For example, if there are 2 units vs 3 units, only the first and second highest rolls of each squad are used for comparison.
- The side with the higher number of points wins the battle (the defender wins if there is a tie in the number of points).
Write a function to simulate a battle in Risk given the
defender_squad_size of the two players. Return a tuple of
atk_rollsis the rolls of the attacker
def_rollsis the rolls of the defender
def_winis a boolean that says if the defender won or not
random rather than
numpy to generate random values
defender_squad_size = 6 attacker_squad_size = 6
Next question: Promoting Instagram
def combat(defender_squad_size, attacker_squad_size) -> ([6, 4, 3, 2, 2, 1], [6, 6, 5, 4, 3, 1], True)