About placing the keys...


0...Duplicate the data-structure holding your map, so we don't mess up the original.
1...Do a flood-fill with P(player start) as your starting node and space (the empty area) as your target color.
2...Modify the flood-fill algorithm to find doors adjacent to the empty areas that it traverses and store them in a queue. We'll call it, door-queue. I'll write the modified* flood-fill algorithm at the end of this post.
3...If door-queue is empty, return (this is the only exit. means all doors have been opened)
4...While door-queue is not empty:
5.......Set D to the front node of door-queue.
6.......Select a replacement-color painted node at random. (replacement-color are the nodes visited in the last flood-fill).
7.......Create a key of the color of the door found in D node and place it on the node selected in Step 6. Also place the same key at the same position on the ORIGINAL MAP.
8.......Remove the door from D node on the map and replace it with replacement-color, if you haven't done it already in 0 step of Modified Flood-fill.
9.......Dequeue door-queue (remove the front node).
10..Repaint all replacement-color nodes as target-color (white space).
11..Loop back to 1. Map should now be just like it was at the beginning (no replacement-color painted nodes) except that the doors examined in this cycle are replaced with white space (opened) and the keys that open them lay around. Since these doors are now white spaces, the next flood-fill will examine a bigger area of the map since it will now be able to traverse beyond them. The algorithm ends in step 3 if the last flood-fill didn't find any doors (they have all been removed, or there wasn't any in the first place).


*About Step 2. Assuming you're using the recursive implementation (you should. It's the simplest one and lite-c can handle it). Add the following zero line at the beginning:

Code:
Flood-fill (node, target-color, replacement-color):
 0. If node is a door, enqueue node to door-queue and set the color of node to replacement-color (so the same door won't be enqueued twice). Make sure all info of door (color, location on map, etc, is preserved and can still be found from the door-queue node).
 1. If the color of node is not equal to target-color, return.
 2. Set the color of node to replacement-color.
 3. Perform Flood-fill (one step to the west of node, target-color, replacement-color).
    Perform Flood-fill (one step to the east of node, target-color, replacement-color).
    Perform Flood-fill (one step to the north of node, target-color, replacement-color).
    Perform Flood-fill (one step to the south of node, target-color, replacement-color).
 4. Return.




INTENSE AI: Use the Best AI around for your games!
Join our Forums now! | Get Intense Pathfinding 3 Free!