-
Notifications
You must be signed in to change notification settings - Fork 1
/
fncEliminateWalls.m
83 lines (69 loc) · 2.48 KB
/
fncEliminateWalls.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function M = fncEliminateWalls(M,E)
% Wall elimination algorithm
% -------------------------------------------------------------------------
%
% Function :
% [M] = fncEliminateWalls(M,E)
%
% Inputs :
% M - Multi layer cell array containing the maze structure
% E - Number walls to be eliminated
%
% Outputs :
% M - Multi layer cell array containing the maze structure, where
% layers 1-4 (left,up,right,down) contain the wall locations.
% Layer 5 contains the dead-end locations.
%
% -------------------------------------------------------------------------
% Author : P.C. Luteijn
% email : [email protected]
% Date : July 2017
% Comment : Function eliminates wall's from the multi layer maze
% structure randomly per row/column. The routine terminates
% automatically after the squared number of to be eliminated
% walls have been reached.
%
% -------------------------------------------------------------------------
% Get size
[nr,nc,~] = size(M);
% Number of cycles till cutt-off
cutoff = 500;
% Setup colum range
arrRow = 2:nr-1;
arrCol = 2:nc-1;
% Eliminate walls for number of E passes
nElim = 0; nCutOff = 0;
while nElim <= E
% Select random row/column
i = arrRow(ceil(rand*(nr-2)));
j = arrCol(ceil(rand*(nc-2)));
% Get walls
W = reshape(M(i,j,1:4),[1,4]);
% Number of walls
nW = 4 - sum(W);
% Continue if walls are pressent
if nW > 0
% Increment
nElim = nElim + 1;
% Locate walls
[~,wIdx] = find(W==0);
% Wall to be eliminated
elimWall = wIdx(randi(nW));
% Eliminate wall
M(i,j,elimWall) = 1;
% Update neigbouring maze-cell
if elimWall == 1; M(i,j-1,3) = 1;
elseif elimWall == 2; M(i+1,j,4) = 1;
elseif elimWall == 3; M(i,j+1,1) = 1;
elseif elimWall == 4; M(i-1,j,2) = 1;
end
else
% Cut-off loop when it gets harder to find cells
nCutOff = nCutOff + 1;
if nCutOff > cutoff
fprintf('Wall elimination has been cut-off\n')
break;
end
end
end
end