void fillDeadEnd(std::vector<std::vector<int> >* walls, int size, int start, int end, int index, int recurLevel = 0, int maxRecurLevel = -1) { std::vector<int>* cell = &(*walls)[index]; if (index != start && index != end && isDeadEnd(*cell)) { int free = getFreeDirection(*cell); int next = getRelative(index, free, size); (*cell)[free] = 1; (*walls)[next][getOppositeDirection(free)] = 1; if (maxRecurLevel == -1 || recurLevel < maxRecurLevel) { fillDeadEnd(walls, size, start, end, next, recurLevel + 1, maxRecurLevel); } } }
void isDeadEnd_test(void) { WorldMap wm; XYPos pos; bool test; /* * +-+-+-+ * | | | * + + +-+ * |X . | * +-+ + + * | X| | * +-+-+-+ */ wm.xlen = 3; wm.ylen = 3; initWorldMap(&wm); pos.x = 1; pos.y = 1; addToWorldMap(&wm, pos, N, OPEN); addToWorldMap(&wm, pos, S, OPEN); addToWorldMap(&wm, pos, W, OPEN); pos.x = 0; pos.y = 1; markSquareAsDead(&wm, pos); pos.x = 1; pos.y = 2; markSquareAsDead(&wm, pos); pos.x = 0; pos.y = 0; test = isDeadEnd(wm, pos, S); assert(test == true); test = isDeadEnd(wm, pos, E); assert(test == false); }
/* escapes dead-end and creates a wall to block that node */ int blockDeadEnd(GraphNode *current, Graph *graph) { int x = current -> position -> x; int y = current -> position -> y; GraphNode *westNode; GraphNode *northNode; GraphNode *southNode; GraphNode *eastNode; if (x > 0) { westNode = graph -> nodes[x - 1][y]; } if (y > 0) { northNode = graph -> nodes[x][y - 1]; } if (y < graph -> height - 1) { southNode = graph -> nodes[x][y + 1]; } if (x < graph -> width - 1) { eastNode = graph -> nodes[x + 1][y]; } /* if current node is not a dead-end, return NULL */ if (!isDeadEnd(current, graph)) { return -1; } /* move out of node, block with wall, and return direction */ if (current -> west != &WALL && westNode -> deadEnd != 1) { return 0; } else if (current -> north != &WALL && northNode -> deadEnd != 1) { return 1; } else if (current -> south != &WALL && southNode -> deadEnd != 1) { return 2; } else if (current -> east != &WALL && eastNode -> deadEnd != 1) { return 3; } /* shouldn't happen */ return -3; }