Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data structure for a random world

So, I was thinking about making a simple random world generator. This generator would create a starting "cell" that would have between one and four random exits (in the cardinal directions, something like a maze). After deciding those exits, I would generate a new random "cell" at each of those exits, and repeat whenever a player would get near a part of the world that had not yet been generated. This concept would allow a "infinite" world of sorts, all randomly generated; however, I am unsure of how to best represent this internally.

I am using C++ (which doesn't really matter, I could implement any sort of data structure necessary). At first I thought of using a sort of directed graph in which each node would have directed edges to each cell surrounding it, but this probably won't work well if a user finds a spot in the world, backtracks, and comes back to that spot from another direction. The world might do some weird things, such as generate two cells at one location.

Any ideas on what kind of data structure might be the most effective for such a situation? Or am I doing something really dumb with my random world generation?

Any help would be greatly appreciated. Thanks, Chris

like image 881
Chris Covert Avatar asked Oct 22 '10 16:10

Chris Covert


2 Answers

I recommend you read about graphs. This is exactly an application of random graph generation. Instead of 'cell' and 'exit' you are describing 'node' and 'edge'.

Plus, then you can do things like shortest path analysis, cycle detection and all sorts of other useful graph theory application.

This will help you understand about the nodes and edges:

and here is a finished application of these concepts. I implemented this in a OOP way - each node knew about it's edges to other nodes. A popular alternative is to implement this using an adjacency list. I think the adjacency list concept is basically what user470379 described with his answer. However, his map solution allows for infinite graphs, while a traditional adjacency list does not. I love graph theory, and this is a perfect application of it.

Good luck!

-Brian J. Stianr-

like image 180
Brian Stinar Avatar answered Sep 22 '22 21:09

Brian Stinar


A map< pair<int,int>, cell> would probably work well; the pair would represent the x,y coordinates. If there's not a cell in the map at those coordinates, create a new cell. If you wanted to make it truly infinite, you could replace the ints with an arbitrary length integer class that you would have to provide (such as a bigint)

like image 35
user470379 Avatar answered Sep 20 '22 21:09

user470379