Implementing a basic version
In the previous step, we visited the problem of a robot moving in space while avoiding obstacles. Let’s implement a first basic version of a program to solve the problem.
Please read this all sections carefully before attempting the exercise.
The files you will need to complete this exercise, and the solution to it, can be found in the ‘Supporting files and solutions‘ .zip file. Use the files in the folder ‘lab 7’.
Start here
- Open Visual Studio Code.
- Select Terminal → New Terminal from the file menu.
- Run
python main.py env1in the terminal pane. You should see the following output:
- The robot is currently in cell 1,1 (row=1, column=1) indicated by the letter R.
- Cells that are marked with
Xare obstacles, if the robot attempts to go into such cells the program will terminate. - The cell with the letter
Gis the goal cell the robot needs to get to.
The functions available to you
python main.py env1. This will run the program using env1.main.py you will find the following line:robot = utils.get_environment(sys.argv[1])
robot object above, you can use the following commands:| Function | Explanation |
|---|---|
robot.move(Direction.Up)robot.move(Direction.Down)robot.move(Direction.Left)robot.move(Direction.Right) |
Moves the robot to the next cell towards the direction provided. For example, if the robot is currently in cell (1, 1) and you command robot.move(Direction.Right) then the robot will be in cell (1, 2). |
robot.position |
Returns the position of the robot (as a cell). You can use robot.position.row to access the row and robot.position.column to access the column. |
robot.goal_cell |
Returns the goal cell. You can use robot.goal_cell.row to access the row and robot.goal_cell.column to access the column. |
robot.num_motions |
Returns the number of times the robot moved from initial position. If the robot performed 10 motions, this will return 10.
|
The problem you need to solve
main.py.- If you need to move the robot in the same direction for more than one step, use a loop instead of typing the same command over and over again. Use loops to group moves in the same direction and that need to happen consecutively for more than one step. If the robot needs to move Up, Up, Up, Left, Right, Right you can group Up, Up, Up in a loop, execute a single Left and group Right, Right in another loop.
- Your program should print, ‘Hooray! Robot reached the goal!!’. But this should only be printed if the robot has indeed reached the goal (i.e., you should write code to check and verify):
- Finally, after you print the success message, your program should also print the number of steps the robot took to reach the goal. Have a look at the table above to find out a useful command for this sub-task.
Prepare
Open Visual Studio Code and then open themain.pyfile. You will see a skeleton code with multiple TODO comments to help you complete this exercise. Each TODO block you will see (they appear as Python comments with # and the keyword TODO) is a small sub-task for you.Complete the code
Have a look at all the TODO comments and complete the code required for each.
You should see the following result:
Of course, this is not a great solution, and it will easily break if the robot starts at a different position or if the environment changes. We will work on it and improve our solution! But this was the first step! Well done!
If you get stuck, please have a look at the solution for a hint.
Solutions
As ever, remember, there is usually more than one solution. If you thought of something else, it’s not necessarily wrong. If your solution works, that’s great. Read the code regardless and see if there is something different in the solution and reflect on whether it is better.
As you have heard a number of times now, a good programmer is one that reads and learns from others’ code!
An Introduction to Programming Using Python
An Introduction to Programming Using Python
Register to receive updates
-
Create an account to receive our newsletter, course recommendations and promotions.
Register for free







