Control Abstraction
In the last day we created simple functions that made the robot go left or right in 90 degree angles and move forward to the next cell. But those rotation movements are relative to the robot.
Now we wanted to have control relative to the board itself, like a map view. Instead of naming "North / South / West / East" we named respectively "Top / Bottom / Left / Right". For no particular reason. That was just the way we started to refer to the concepts and that was what we used.
The map uses coordinates from (0, 0) to (5, 5) for the 6x6 game board.
We always assume that the robot starts on (0, 0) orientated to the bottom (South), just like in a Spreadsheet you start in A1 and go down.
Orientation
We therefore needed to handle Orientation (void changeOrientation(int new_orientation)). And to move the robot from one cell to another, we must rotate the robot from the actual orientation to the desired heading. Then move the robot that way.
To solve this problem, we use two approaches simultaneously. By a orientation code defining orientations clockwise (0 - Bottom; 1 - Left; 2 - Top; 3 - Right) and direct textual reference ("bottom", "left", "top", "right").
The orientation codes serve to calculate the desired orientation by use of a subtraction function between actual and next orientation codes. It will use the minimum notations needed to turn the right way.
The textual references are used in the higher level function "move (direction)", as an easy way to read code.
For this we use another function ( nextCellOnMove(string direction) ) that calculates the next cell coordinates into a global variable of type position (x,y) and returns the orientation code to be used.

Sem comentários:
Enviar um comentário