-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze.mch
104 lines (86 loc) · 2.49 KB
/
Maze.mch
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
/* Maze
* Author: NUVIN
* Creation date: 18/12/2022
*/
MACHINE
Maze
SETS
MAZE_MESSAGE = {Yes, No, Internal_Wall_NA, Invalid_Error}
CONSTANTS
entranceSquare,
exitSquare,
mazeBoundry,
internalWalls,
validSquares
PROPERTIES
mazeBoundry: POW (NATURAL1 * NATURAL1) &
mazeBoundry = { xx, yy |
xx : NATURAL1 &
yy : NATURAL1 &
xx <= 7 &
yy <= 5 } &
internalWalls <: mazeBoundry &
internalWalls = {(1 |-> 3), (2 |-> 3), (2 |-> 5), (2 |-> 1), (4 |-> 2), (4 |-> 3), (4 |-> 4), (3 |-> 3), (6 |-> 1), (6 |-> 2), (6 |-> 4), (7 |-> 4)} &
validSquares = mazeBoundry - internalWalls &
entranceSquare: validSquares &
entranceSquare = (1 |-> 1) &
exitSquare: validSquares &
exitSquare = (1 |-> 5)
VARIABLES
visitedPath,
squareCord
INVARIANT
squareCord: validSquares &
visitedPath: seq(INTEGER * INTEGER)
INITIALISATION
squareCord, visitedPath := entranceSquare, []
OPERATIONS
// keeps track of the path the robot has taken
updatePath(squareCoordinate) =
PRE
squareCoordinate : validSquares
THEN
squareCord := squareCoordinate ||
visitedPath := visitedPath <- squareCord
END;
// extract the current square coordinates of the robot
position <-- getPosition =
BEGIN
position := squareCord
END;
// checks if robot has found the exit
isExitFound <-- foundExit =
IF (squareCord = exitSquare)
THEN
isExitFound := Yes
ELSE
isExitFound := No
END;
// checks if the square has already been visited
isVisitedSquare <-- hasVisitedSquare (xCord, yCord) =
PRE
xCord : dom(validSquares) & yCord: ran(validSquares) & ((xCord |-> yCord) : validSquares)
THEN
IF ((xCord |-> yCord) : validSquares)
THEN
IF ((xCord |-> yCord) : ran(visitedPath))
THEN
isVisitedSquare := Yes
ELSE
isVisitedSquare := No
END
ELSE
isVisitedSquare := Internal_Wall_NA
END
END;
// returns the route the robot took
route <-- robotsRoute =
BEGIN
route := visitedPath
END;
// resets the maze variables
resetMaze =
BEGIN
squareCord, visitedPath := entranceSquare, []
END
END