* @see <a href=http://en.wikipedia.org/wiki/Maze_generation_algorithm#Randomized_Prim.27s_algorithm>Wikipedia</a>
*/
public static Set<Vector> generate(int size, float redundantProbability, Random random) {
// Add origin as a maze tile
Set<Vector> tiles = new HashSet<Vector>();
tiles.add(new Vector());
Set<Vector> walls = new HashSet<Vector>();
walls.add(new Vector(1, 0));
walls.add(new Vector(-1, 0));
walls.add(new Vector(0, 1));
walls.add(new Vector(0, -1));
// Repeat specified number of times
for (int n = 1; n < size;) {
// Choose randomly a wall
Vector wall = Algorithms.random(walls, random);
walls.remove(wall);
// Check if both endpoints already exist
Move dir = wall.getX() % 2 == 0 ? Move.North : Move.East;
Vector a = wall.add(dir), b = wall.sub(dir);
boolean ac = tiles.contains(a), bc = tiles.contains(b);
if (ac && bc && random.nextFloat() >= redundantProbability)
continue;
// Add tiles to maze
tiles.add(wall);
++n;
if (!ac) {
tiles.add(a);
++n;
}
if (!bc) {
tiles.add(b);
++n;
}
// Add neighbors as walls
for (Move m : Move.getNonzeros()) {
Vector am = a.add(m);
if (!tiles.contains(am))
walls.add(am);
Vector bm = b.add(m);
if (!tiles.contains(bm))
walls.add(bm);
}
}
return tiles;