I'm just trying to figure out how to set up the code for a game like risk...

I would like it to be extensible, so that I can have multiple maps (ie- World, North America, Eurasia, Africa) so hardcoding in the map doesn't seem to make a whole lot of sense

I'm a bit confused on how/where items should be stored/accessed.

Here are the objects I see the game theoretically using: -Countries/Territories -Cities (Can be contained within territories) -Capitols -Connections -Continents -Map -Troops

At the moment, I feel like: -A map should have a list of continents and countries. The continents would be more of a 'logical' thing where the continents would just be lists of countries that are checked for bonuses at the start of turns -Countries should have a list of countries that they're connected to for the connections

What I can't figure out is: Where do I store the troops? Do I have an object for every single troop or do I just store the number of troops on a country object as an integer?

What about capitols and cities? Do those just have a reference to the country they reside in?

Is there anything I'm not seeing here that's going to screw me over in the long run with the way that I'm thinking about things now?

Any advice would be appreciated.

  • \$\begingroup\$ when you store the troops, if you did it just as a number how would you know what player owns those troops? might want to include that in the object \$\endgroup\$
    – Tom Prats
    Sep 19, 2012 at 15:35
  • \$\begingroup\$ The troops would most likely get stored on the country and the owner of the country would be the one that owns those troops. \$\endgroup\$
    – jyanks
    Sep 20, 2012 at 4:25

2 Answers 2


I would keep data tables (in a file most likely), then read at the start and stored into classes, using IDs, something like:

ID, Display 
NA, North America 
SA, South America 
EU, Europe
AS, Asia 

ID, Display, Continent ID, Capital City ID (see below for City list)
CA, Canada, NA, OT
US, United States, NA, WA
MX, Mexico, NA, MC
RS, Russia, EU, MO // Ha, EU or AS (I Googled it, they didn't even know!)

Country Connection:
ID, Connected To

Technically, since MX is connected to US, therefore you don't need the 'redundant' entry saying US is connected to MX. But this style is easy for looking up connections based on the current country you are querying, and not have to check all entries etc. Also, as a bonus feature, this way lets you define 'one way' relationships for whatever reason. US can access MX, but maybe MX can't access US (terrain issues, country capabilities such as no navy presence, etc).

For cities, the ID should be unique across the entire world, no duplicates, so a four letter number / code would be recommended:

ID, Country ID, Display
TO, CA, Toronto
OT, CA, Ottawa
DT, US, Detroit
NY, US, New York
MC, MX, Mexico City
MO, RS, Moscow

If you want to track which cities connect to other cities (spanning country borders too):

City Connection:
TO, OT // Toronto connects to Ottawa
TO, DT // Toronto connects to Detroit

Then to keep track of troops, track each player separately, using perhaps dynamic lists, such as (and update when new countries are lost / acquired):

Player Troops:
Player ID, Country ID, Qty
01, CA, 5 // Player 01 has 5 troops in Canada
01, US, 7 // Player 01 has 7 troops in the US
02, MX, 14 // Player 02 has 14 troops in Mexico

If you want to track troops in cities rather than countries, use the city id instead. If there is only one style of troop, the above works. But if you have multiple troop styles (navy, land, air force) then give each style an ID, and update the above to something like:

01, NV, CA, 5 // Player 01 has 5 NAVY troops in Canada
01, AF, CA, 14 // Player 01 has 14 AIRFORCE troops in Canada

You could even store each countries' borders as polygon lines in a table, for displaying, or checking if a country is clicked, or highlighting, etc.

[EDIT] Thinking over this I think I would create a 'troops' class, and a 'player' class that has an instance of a 'troop' object. The troop class would have no reference to any player. I was thinking too 'databasey' at the time!


Slydog's answer answers most of your questions, i guess. I would prefer to have this answer as a comment, as it may not contribute to what you are looking for here but I don't have the permissions to comment and I don't want that to stop me from giving my contribute to eventually help you.

Anyway, if you need a database with polygon information about countries borders, etc, there are a couple online. For example this: http://thematicmapping.org/downloads/world_borders.php

  • \$\begingroup\$ Great resource! Ha, in my first answer I ever made, I wanted to make it a comment too, but didn't have the permissions yet. It even got a few votes! \$\endgroup\$ Sep 19, 2012 at 17:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .