Advertisement Jump to content


This topic is now archived and is closed to further replies.


Lists of adjacent territories for risk clone

This topic is 5436 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey folks, As a bit of a learning excercise in C++ I''m writing a simple clone of risk in GTK. Each territory is represented by a class in order to keep track of who currently owns it, how many armies are on it, its name and other similar details. I also need some way of keeping track of which other territories share borders with this one and can therefore be attacked by a player. At the moment I''m using vector (from the STL) to do this - each Territory has an integer ID and the vector for each territory object holds the IDs of all adjacent territories. However this seems to me to be a bad way of doing it - not least because entering all the lists takes ages! Does anyone have any better suggestions? Cheers, Steve

Share this post

Link to post
Share on other sites
A good old two dimensional array of bools would do the trick, or a a 2D vector if you need it to be flexible. There are ways you could compress this if memroy is a concern.

Share this post

Link to post
Share on other sites
Well, I came up with this program. Maybe it will help.

#include <set>
#include <map>
#include <iostream>

typedef int ID;

class Territory
void setNeighbor(Territory &t)
t.adjacent.insert(this); // If we already have them, they should already have us.


void printInfo() const
std::cout << this << " linked to: ";

for(std::set<Territory *>::const_iterator c = adjacent.begin(); c != adjacent.end(); ++c)
std::cout << *c << ' ';

std::cout << std::endl;

std::set<Territory *> adjacent;

std::map<ID, Territory> territory;

ID connections[][2] = {{0, 1},
{1, 2},
{2, 0}}; // Three teritories connected in a triangle.

int main(int argc, char *argv[])
for(int c = 0; c < sizeof connections / sizeof *connections; ++c)

// Show the connections.

for(std::map<ID, Territory>::const_iterator c = territory.begin(); c != territory.end(); ++c)

connections is which territory to connect to which other territory. It won't screw anything up if you connect two territories more than once.

Once it's done, each element in Territory::adjacent is a pointer to a neighboring territory.

[edited by - smart_idiot on March 6, 2004 1:56:47 PM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!