-
Notifications
You must be signed in to change notification settings - Fork 1
/
isValid.m
110 lines (105 loc) · 3.56 KB
/
isValid.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
function [valid] = isValid(y, x, geometryMatrix, elementsPerCm)
valid = true;
hasMoved = false;
bonusMove = false;
graceSteps = 5;
remainingSteps = elementsPerCm ^ 2;
if (geometryMatrix(y, x) == 0)
direction = 1;
xPos = x;
yPos = y;
maxDimension = 2 + (5 * elementsPerCm);
while (yPos ~= 1)
if direction == 1
fBlocked = geometryMatrix(yPos - 1, xPos) == 1;
if (xPos == maxDimension)
rBlocked = true;
else
rBlocked = geometryMatrix(yPos, xPos + 1) == 1;
end
if (xPos == 1)
lBlocked = true;
else
lBlocked = geometryMatrix(yPos, xPos - 1) == 1;
end
elseif direction == 2
if (xPos == 1)
fBlocked = true;
else
fBlocked = geometryMatrix(yPos, xPos - 1) == 1;
end
rBlocked = geometryMatrix(yPos - 1, xPos) == 1;
if (yPos == maxDimension)
lBlocked = true;
else
lBlocked = geometryMatrix(yPos + 1, xPos) == 1;
end
elseif direction == 3
if (yPos == maxDimension)
fBlocked = true;
else
fBlocked = geometryMatrix(yPos + 1, xPos) == 1;
end
rBlocked = geometryMatrix(yPos, xPos - 1) == 1;
if (xPos == maxDimension)
lBlocked = true;
else
lBlocked = geometryMatrix(yPos, xPos + 1) == 1;
end
else
if (xPos == maxDimension)
fBlocked = true;
else
fBlocked = geometryMatrix(yPos, xPos + 1) == 1;
end
rBlocked = geometryMatrix(yPos + 1, xPos) == 1;
lBlocked = geometryMatrix(yPos - 1, xPos) == 1;
end
%behavior
if ~hasMoved && (graceSteps == 0)
valid = false;
return;
elseif (remainingSteps == 0)
valid = false;
return;
elseif (yPos == 1)
return
elseif (xPos == x) && (yPos == y) && (graceSteps == 0 || hasMoved == true)
valid = false;
yPos = 1;
return
elseif ((direction == 1) || rBlocked || bonusMove) && not(fBlocked)
%move forward
if (direction == 1)
yPos = yPos - 1;
elseif (direction == 2)
xPos = xPos - 1;
elseif (direction == 3)
yPos = yPos + 1;
else
xPos = xPos + 1;
end
hasMoved = true;
elseif not(rBlocked)
%% turn right
if (direction == 1)
direction = 4;
else
direction = direction - 1;
end
bonusMove = not(lBlocked);
else
%turn left
if (direction == 4)
direction = 1;
else
direction = direction + 1;
end
end
if (~hasMoved)
graceSteps = graceSteps - 1;
end
remainingSteps = remainingSteps - 1;
end
end
end