Chapter 5: Making a git commit

Robot with a position

face Josiah Wang

Now, it is time to make your robot project a bit more exciting!

So far, you have named your robot. We will now create a world for the robot to navigate in.

We will assume that your robot can navigate inside an N\times N grid. We assume that the origin (0, 0) is at the top left most grid cell. The bottom right most cell should be indexed as (9, 9). We will assume the first element is the row-coordinate, and the second is the col-coordinate, that is, (row, col). The reason for this choice of coordinate system, rather than (x, y), will become clearer in the future. Just be mindful that the first element is the row-coordinate (equivalent to y, not x)! 😵

The robot can also be classified as being positioned in one of the quadrant, depending on its current location (top left, bottom left, top right, bottom right).

Your robot navigates in a 10x10 grid, divided into quadrants. The origin is at the top-left.

Now, please update main.py of your robot project.

You should define a new variable named grid_size that represents the maximum length of the grid (we will assume that the grid is a square). Set this to 10 for now to represent a 10 \times 10 grid.

When the program runs, the program should:

  1. read in the robot’s name from the user, save this as name
  2. read in the row-coordinate of the robot from the user (make this an int for simplicity), save this as row
  3. read in the column-coordinate of the robot from the user (make this an int for simplicity), save this as col

You should assume that the user may enter any integer (including negative integers) as the coordinates.

Remember that your robot is currently restricted to be inside the N \times N grid, i.e. both the row-coordinates and column-coordinates are between 0 and grid_size-1 (9 in this case) (both inclusive). So, you should ‘clip’ both coordinates entered by the user so that they become 0 if the value is less than 0, and to grid_size-1 if the value is greater than grid_size-1.

The program will print out a message from the robot with its name and ID as before.

The program will then print out the robot’s current coordinates, and which quadrant it is in.

You can keep everything else you previously have about the robot.

Feel free to use any Python built-in functions as needed.

You have already committed your previous version on Git, so feel free to just update main.py directly.

Sample run #1

What is the name of the robot? Bender
What is its current row coordinate? 3
What is its current column coordinate? 20
Hello. My name is Bender. My ID is 1000.
My current location is (3, 9). I am in the top right quadrant.

Sample run #2

What is the name of the robot? Bender
What is its current row coordinate? 10
What is its current column coordinate? -2
Hello. My name is Bender. My ID is 1000.
My current location is (9, 0). I am in the bottom left quadrant.