Strange Problem with a 2D Maze Lighting Engine
in
Programming Questions
•
2 years ago
I'm making a maze game, and right now I'm adding a lighting engine to it. Before I tell you about the lighting glitch, let me explain how the code works. The maze is generated during setup() and stored in a 2D array of ints called maze. The value of a certain index ( [x][y] ) represents the type of that specific cell. The format is like this:
In the draw() function, the maze is displayed and you can run through it with the arrow keys. To make it more of a challenge, I've added a lighting system that uses a breadth first search style approach with nodes ( http://en.wikipedia.org/wiki/Breadth-first_search). The code is rather simple, basically it starts with the player's position as the first node, then spreads outwards a certain number of times based on the value of visibility, with the color getting dimmer as it spreads more. The nodes are all added to a string so I can use match() to check if a certain node was already visited. Code:
The problem is that the light sometimes just drops off at a certain node apparently at random. Here's 3 examples of this happening:
I seriously don't know why this is happening. My guess is that the problem lies with the visitedCells string, but I don't see anything wrong with it. Any ideas?
P.S. - Here's the full code if you want to take a look:
http://pastebin.com/Z7ycN5BZ
- int maze[x][y]
- 0 - cell unvisited
1 - cell visited
2 - wall unremoved
3 - wall removed
4 - part of the solution
In the draw() function, the maze is displayed and you can run through it with the arrow keys. To make it more of a challenge, I've added a lighting system that uses a breadth first search style approach with nodes ( http://en.wikipedia.org/wiki/Breadth-first_search). The code is rather simple, basically it starts with the player's position as the first node, then spreads outwards a certain number of times based on the value of visibility, with the color getting dimmer as it spreads more. The nodes are all added to a string so I can use match() to check if a certain node was already visited. Code:
- void draw()
{
background(0);
noSmooth();
noStroke();
rectMode(CORNER);
ArrayList<String> nodes = new ArrayList<String>();
nodes.add(posX + " " + posY);
String visitedCells = "";
visibility = 40;
for(int a = 0; a <= visibility; a++) {
ArrayList<String> newNodes = new ArrayList<String>();
for(int i = 0; i < nodes.size(); i++) {
int nX = int(nodes.get(i).split(" ")[0]);
int nY = int(nodes.get(i).split(" ")[1]);
visitedCells += nX + " " + nY + ",";
int cVal = (int) ((1.0 - a / ((float) visibility)) * 255.0);
fill(255, 255, 255, cVal);
rect(nX * drawSize, nY * drawSize, drawSize, drawSize);
if(isCell(nX - 1, nY) && match(visitedCells, (nX - 1) + " " + (nY) + ",") == null) { // check cell left
newNodes.add( (nX - 1) + " " + (nY) );
}
if(isCell(nX + 1, nY) && match(visitedCells, (nX + 1) + " " + (nY) + ",") == null) { // check cell right
newNodes.add( (nX + 1) + " " + (nY) );
}
if(isCell(nX, nY - 1) && match(visitedCells, (nX) + " " + (nY - 1) + ",") == null) { // check cell up
newNodes.add( (nX) + " " + (nY - 1) );
}
if(isCell(nX, nY + 1) && match(visitedCells, (nX) + " " + (nY + 1) + ",") == null) { // check cell down
newNodes.add( (nX) + " " + (nY + 1) );
}
}
nodes = newNodes;
}
fill(255, 0, 0);
rect(posX * drawSize, posY * drawSize, drawSize, drawSize);
}
boolean isCell(int xp, int yp)
{
if((xp >= 0) && (yp >= 0) && (xp <= sizeX - 1) && (yp <= sizeY - 1)) {
// not a wall
return maze[xp][yp] != 2;
} else {
// outside the bounds of the maze, return false
return false;
}
}
The problem is that the light sometimes just drops off at a certain node apparently at random. Here's 3 examples of this happening:
I seriously don't know why this is happening. My guess is that the problem lies with the visitedCells string, but I don't see anything wrong with it. Any ideas?
P.S. - Here's the full code if you want to take a look:
http://pastebin.com/Z7ycN5BZ
1