Interview Query

Programming Risk Combat

1
Have you seen this question before?

In a simplified version of the board game Risk, combat follows the following rules:

  1. 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.
  2. 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.
  3. 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 attacker_squad_size and defender_squad_size of the two players. Return a tuple of (atk_rolls,def_rolls,def_win) where:

  • atk_rolls is the rolls of the attacker
  • def_rolls is the rolls of the defender
  • def_win is a boolean that says if the defender won or not

Note: Use random rather than numpy to generate random values

Example:

Input:

defender_squad_size = 6
attacker_squad_size = 6

Output:

def combat(defender_squad_size, attacker_squad_size) -> ([6, 4, 3, 2, 2, 1], [6, 6, 5, 4, 3, 1], True)
Next question: Promoting Instagram
.....
Python 3.9.6
Loading editor
Use Shift + Enter to run code