简体   繁体   中英

Algorithm Help: Building game board, but need to know when square is locked in

I have built a gameboard that consists of a grid, the grid is then randomly assigned, "Walls" to a cell. Once the cells are built, how can I check to see if a certain cell is 'locked in' so that I don't place a player there.

I have thought about this and the first ago I came up with check all sides for four walls, but obviously, a cell could be surrounded by open cells, which are then surrounded by walls.

The other is a "escape to outside" algo, which basically tries to find a path to an outside wall, which would then mean it is not locked in, but if the block is on an outside wall and surrounded by blocks it would be locked in.

How is this typically handled? I'm using python if that matters for any code examples.

Thanks!

You basically want a floodfill algorithm.
http://en.wikipedia.org/wiki/Floodfill

edit
I think I misunderstood your definitions of 'locked' and 'escape'.
If you have limited game board, every cell there is locked in some space. If I understand you correctly, you just want that space to be big enough. Well, you compute its area easily with flood fill algorithm.

It really depends on the size of your game board. If we are speaking of small boards, a quick path finding algorithm can give you the distance between every cell and each other. This might have a double use if you do not want to place the player too close to other players or other features.

A second option is to design a wall generator that by principle cannot create locked rooms. The roguelike community has done a decent amount of research into the topic of generating random (dungeon-sized) maps without closed rooms.

Finally, using a level of detail approach can help you if you have to find a suitably large empty space on a huge map. Find an interconnection graph for a set of sparse points distributed over your map (this can be a direct result of your map generator). That should be sufficient to place a player. But if you need more detail on a specific point, finding a path to one of these points can tell you if a room is locked in. This might not work in extremely dense labyrinths.

I'm not sure exactly what your game board can look like, but if you have something like this:

+----------------------+
|      +-----+         |
|      | c   |         |
|      |     |         |
|      +-----+         |
|                      |
|                      |
|                      |
+----------------------+

And you want to avoid putting the character on the c because it's 'walled in', even though it's it's not exactly surrounded by the walls, you could easily implement the Left-or-Right Hand algorithm - only instead of trying to get out of the maze you're checking to see if you make it back to the same coordinate.

Maybe this previous post is what you are looking for. In that post, I answered how to count the number of different areas of dots there was on a grid on which dots could be placed. Your problem is similar, except instead of having "dots", you have walls. The algorithm given in that post will give you a version of your grid G, where

G[x1][y1] == G[x2][y2]

only if it is possible for a player to get from (x1,y1) to (x2,y2), ie, if there is a clear path with no walls from that first point to the second point.

Is this what you are looking for? Do you want additional details, or do you want me to adapt that code to your particular problem?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM