-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.mch
More file actions
170 lines (141 loc) · 5.51 KB
/
Robot.mch
File metadata and controls
170 lines (141 loc) · 5.51 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Reasoning About Programs coursework
* Machine : Robot
* Robot.mch
* Author: Duneesha Suloshini
* ID : 2017336/ w1697801
* Creation date: 25/11/2020
*/
MACHINE
Robot
INCLUDES
Maze
PROMOTES
foundExit, hasVisitedSquare, robotsRoute
SETS
DIRECTIONS = {North, South, East, West};
STATUS_MESSAGE = {
SuccessfullyMoved,
CannotMoveInternalMazewallDetected,
CannotMoveExternalBoundarywallDetected,
SuccessfullyTeleported,
CannotTeleportToExitOrOnStartSquare,
CannotTeleportToMazeInternalWalls
}
VARIABLES
visitedRobotDirections, visitedPathofRobot, xPosition, yPosition
INVARIANT
visitedRobotDirections: seq(DIRECTIONS) &
visitedPathofRobot: seq(INTEGER * INTEGER) &
xPosition: NATURAL1 &
yPosition: NATURAL1 &
xPosition: xAxisRobot &
yPosition: yAxisRobot
INITIALISATION
visitedRobotDirections := [] ||
visitedPathofRobot := [] ||
xPosition := 1 ||
yPosition := 1
OPERATIONS
//Robot's movement to the North direction
outputMessage <-- moveNorth =
IF
// xPosition and yPosition as an ordered pair(maplet)
((xPosition |-> yPosition + 1 ) /: mazeWallbarriers & (yPosition + 1 : yAxisRobot))
THEN
yPosition := yPosition + 1 ||
//append element North to tail of the sequences of visited directions
visitedRobotDirections := visitedRobotDirections <- North ||
//append element of route (x and y position) to the tail of the visited paths sequence
visitedPathofRobot := visitedPathofRobot <- (xPosition |-> yPosition) ||
updateMaze(xPosition, yPosition +1, visitedPathofRobot <- (xPosition |-> yPosition)) ||
outputMessage := SuccessfullyMoved
ELSIF
(yPosition +1 /: yAxisRobot)
THEN
outputMessage := CannotMoveExternalBoundarywallDetected
ELSE
outputMessage := CannotMoveInternalMazewallDetected
END;
//Robot's movement to the South direction
outputMessage <-- moveSouth =
IF
((xPosition |-> yPosition -1) /: mazeWallbarriers & (yPosition - 1 : yAxisRobot))
THEN
yPosition := yPosition - 1 ||
visitedRobotDirections := visitedRobotDirections <- South ||
visitedPathofRobot := visitedPathofRobot <- (xPosition |-> yPosition) ||
updateMaze(xPosition, yPosition -1, visitedPathofRobot <- (xPosition |-> yPosition)) ||
outputMessage := SuccessfullyMoved
ELSIF
(yPosition - 1 /: yAxisRobot)
THEN
outputMessage := CannotMoveExternalBoundarywallDetected
ELSE
outputMessage := CannotMoveInternalMazewallDetected
END;
//Robot's movement to the East direction
outputMessage <-- moveEast =
IF
((xPosition +1 |-> yPosition) /: mazeWallbarriers & (xPosition + 1 : xAxisRobot))
THEN
xPosition := xPosition + 1 ||
visitedRobotDirections := visitedRobotDirections <- East ||
visitedPathofRobot := visitedPathofRobot <- (xPosition |-> yPosition) ||
updateMaze(xPosition + 1, yPosition, visitedPathofRobot <- (xPosition |-> yPosition)) ||
outputMessage := SuccessfullyMoved
ELSIF
(xPosition + 1 /: xAxisRobot)
THEN
outputMessage := CannotMoveExternalBoundarywallDetected
ELSE
outputMessage := CannotMoveInternalMazewallDetected
END;
// Robot's movement to the West direction
outputMessage <-- moveWest =
IF
((xPosition - 1 |-> yPosition) /: mazeWallbarriers &
(xPosition - 1 : xAxisRobot))
THEN
xPosition := xPosition - 1 ||
visitedRobotDirections := visitedRobotDirections <- West ||
visitedPathofRobot := visitedPathofRobot <- (xPosition |-> yPosition) ||
updateMaze(xPosition - 1, yPosition, visitedPathofRobot <- (xPosition |-> yPosition)) ||
outputMessage := SuccessfullyMoved
ELSIF
(xPosition - 1 /: xAxisRobot)
THEN
outputMessage := CannotMoveExternalBoundarywallDetected
ELSE
outputMessage := CannotMoveInternalMazewallDetected
END;
//outputs the current position of the robot in the maze.
currentPosition <-- getPosition =
BEGIN
currentPosition := {(xPosition, yPosition)}
END;
//Teleport to a path square within the maze
/* The operation inputs a square that the Robot attempts to teleport to.
If the input square is suitable for the Robot to teleport to it does so and reports success.*/
teleportMessage <-- teleport(xCellpoint, yCellpoint) =
PRE
xCellpoint : xAxisRobot & yCellpoint : yAxisRobot
THEN
IF
((xCellpoint |-> yCellpoint) /: mazeWallbarriers &
((xCellpoint |-> yCellpoint) /: exitSquare or (xPosition |-> yPosition) /: entrySquare))
THEN
xPosition := xCellpoint ||
yPosition := yCellpoint ||
teleportMessage := SuccessfullyTeleported ||
visitedPathofRobot := visitedPathofRobot <- (xPosition |-> yPosition) ||
updateMaze(xCellpoint, yCellpoint, visitedPathofRobot <- (xPosition |-> yPosition))
ELSE IF
((xCellpoint |-> yCellpoint) : mazeWallbarriers)
THEN
teleportMessage := CannotTeleportToMazeInternalWalls
ELSE
teleportMessage := CannotTeleportToExitOrOnStartSquare
END
END
END
END