We aim to define a \(\texttt{Robot}\) ADT moving in a two dimensional space with functions allowing to:
- Create a robot from a position \((x, y)\) and a direction (\(\texttt{North}\), \(\texttt{South}\), \(\texttt{East}\), \(\texttt{West}\)) that becomes its current direction;
- Determine the current position;
- Determine the current direction;
- Move a step forward from the current direction;
- Move a step backward from the current direction;
- Rotate 90 degrees clockwise.
Define the \(\texttt{Robot}\) ADT.
Difficulty level
This exercise is mostly suitable for students
Type robot
Parameter
Use real, direction
Operations
Create : real x real x direction -> robot
Current_pos : robot -> real x real
Current_dir : robot -> direction
Step_fwd : robot -> robot
Step_bkwd : robot -> robot
Rotate : robot -> robot
Constructors
Create
Preconditions
-
Axioms
Let d be a direction, x and y 2 reals :
Current_pos(Create(x,y,d)) = (x,y)
Current_dir(Create(x,y,d)) = d
Step_fwd(Create(x,y,d)) = If d = East then Create(x+1,y,d) else id d = West then Create(x-1,y,d) else if d =
North then Create(x,y+1,d) else Create(x,y-1,d)
Step_bkwd(Create(x,y,d)) = If d = East then Create(x-1,y,d) else If d = West then Create(x+1,y,d) else If d =
North then Create(x,y-1,d) else Create(x,y+1,d)
Rotate(Create(x,y,d)) = If d = East then Create(x,y,South) else If d = West then Create(x,y,North) else If d =
North then Create(x,y,East) else Create(x,y,West)
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Factorial of a number using recursion