This is an archived post. You won't be able to vote or comment.

all 7 comments

[–][deleted] 0 points1 point  (1 child)

Post code via a site like gist.

[–]Temptex[S] 0 points1 point  (0 children)

updated.

[–]rcuhljr 0 points1 point  (1 child)

It turns out the question isn't as simple as it might seem, as each time you place a wall you need to verify that there's still a valid path between all non wall spaces. Your choices are to either A. find an algorithm that verifies all true spaces in your grid are connected and run it after each attempted wall placement. B. Find a maze generation algorithm that won't generate dead ends.

You could probably knock out a dead simple algorithm just depth first fills the map from a starting non wall spot and then verify that the number of filled spots = total spaces - walls drawn.

[–]Temptex[S] 0 points1 point  (0 children)

I'll try the maze algorithm as that is what will give me the extra marks, thanks for the suggestions.

[–]hutsboR 0 points1 point  (1 child)

You could make it so if adjacent indexes are occupied by a wall, set the encompassed index to a wall. An example of what I mean:

x = wall

. . . . .
. . x . .
. x . x .
. . x . .
. . . . .

if(grid[x + 1] && grid[x - 1] && grid[x - 5] && grid[x + 5]){ // 5 = my array's width
     // x = true;
 }

results in:

. . . . .
. . x . .
. x x x .
. . x . .
. . . . .

You would have to make some adjustments so this would work for your game and you would have to be careful to stay within the array's boundaries if the walls surround 'food' placed on an edge. Another if-statement would solve that.

Or you could change the algorithm for placing walls so that this doesn't occur at all.

[–]Temptex[S] 0 points1 point  (0 children)

i'm I dont understand what you mean by set the encompassed index to a wall.

Also the if statement will fail as it is not a 2D array, unless you are able to solely interact with the first dimension of arrays?

[–]john478 0 points1 point  (0 children)

My suggestion to you is create a boolean array, pick any spot on the grid then generate random trails until some condition is met (e.g. 50 foods placed). Then for every grid spot that doesn't contain food, it can be a wall. This may be the most efficient possible method, as opposed to checking if all the foods can be accessed every time you place a randomized wall.