How to Conquer the World

Genetic algorithms and the game of Risk

Rogier van der Geer

Data Charmer @ GoDataDriven

Risk Analysis

In [1]:
%matplotlib inline

Overview

  • The game of Risk

  • Risk in Python

  • Genetic Algorithms

  • Using a genetic algorithm to play Risk

Risk

Invented in 1957 as The Conquest of the World,
since 1959: Risk: The Game of Global Domination.


According to the publishers:

  • Players: 2-6
  • Playing time: 1-8 hours

The game board

risk.png

Assignment of territories

risk.png

Placing armies

risk.png

A turn consists of three stages:

  • Reinforcement
  • Combat
  • Fortification
  • Reinforcement: the player places additional armies:
    • one army per three territories,
    • bonus armies for owning a full continent,
    • additional bonus for a set of reinforcement cards.
  • Combat: the player may attack neighboring territories:
    • the battle is decided using dice,
    • the attacker has a good chance to win if he has more armies,
    • if the attacker conquers a territory he gets a reinforcement card,
    • the player may attack indefinitely.
  • Fortification: the player may move armies:
    • may make only one fortification move,
    • from one territory to a neighboring territory,
    • with as many armies as he likes.

Missions

Each player is assigned a mission. By completing the mission the player wins the game.

Missions are, for example:

  • Conquer africa and north america
  • Destroy the yellow player
  • Conquer at least 24 territories
  • ...

Risk in Python

We built a framework that handles all aspects of the Risk game. It consists of five classes:

  • Board, which handles the armies on the game board,
  • Cards, which handles the reinforcement cards of a player,
  • Mission, which describes the mission of a player,
  • Player, which makes decisions on how to play, and
  • Game, which handles all other aspects of the game.

The Board

When initialized, the board randomly distributes the territories amongst the players.

In [2]:
import random
random.seed(42)
In [3]:
from board import Board
b = Board.create(n_players=4)

We can get a graphical representation of the board using plot_board():

In [4]:
b.plot_board()

We can easily manipulate the armies on the board:

In [5]:
b.set_owner(territory_id=0, player_id=5)
b.set_armies(territory_id=0, n=15)
b.plot_board()