Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only. T&Cs apply

Find out more

Implementing a basic version

Learn algorithms, logic, and Python basics. Create simple programs, grasp computer science fundamentals, and see its impact across various fields.

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 env1 in 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 X are obstacles, if the robot attempts to go into such cells the program will terminate.
  • The cell with the letter G is the goal cell the robot needs to get to.
(Note: You will come across some code provided in separate Python files in different directories. You do not need to understand this code or attempt to modify the code. This code creates the text-based and graphical/visual-based interface for the project.)

The functions available to you

This section will describe how you can use the functions provided for you (and what each does) so you can use this interface to solve the problem.
You will need to modify the main.py file only and write your code in that file. Please do not change any of the other files.
You can run the program in the terminal using: python main.py env1. This will run the program using env1.
In main.py you will find the following line:
robot = utils.get_environment(sys.argv[1])
This creates a robot in the specified environment (env1, env2 etc).
With the 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.
This is what you need to know for now!

The problem you need to solve

Let’s start with the simplest solution possible: hard-code the move steps for the robot to move to the goal.
Look at the board above and decide what the first step is for the robot to move (left, right, up or down)? What is the second step? What is the third step? Find the correct steps the robot needs to execute sequentially to reach the goal and write them into main.py.
However, here are some rules:
  1. 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.
  2. 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):
  3. 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 the main.py file. 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:

Hooray! The robot is in the goal cell (7,4).

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!

This article is from the free online

An Introduction to Programming Using Python

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now