Jump to content
  • Advertisement

Archived

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

yottskry

Lists of adjacent territories for risk clone

This topic is 5274 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
Advertisement
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
{
public:
void setNeighbor(Territory &t)
{
if(adjacent.insert(&t).second)
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;
}

protected:
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)
territory[connections[c][0]].setNeighbor(territory[connections[c][1]]);

// Show the connections.

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


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 GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!