A Live Developer Journal

Building Mars Rover Solution Object Oriented Tdd With Pharo

Writing in progress...

The Mars Rover problem is a classic programming challenge that has been set by many companies who value test-driven development (TDD) and object-oriented approaches to programming.

As someone who is still trying to learn TDD and object-oriented design, I pair programmed with two other experienced developers to help me learn how to do this, with a lot of feedback iterations.

People who influenced this shared approach

One of the things I like to do when documenting my process is to credit the people/resources who influenced my thought patterns and approach. When you are exposed to many amazing ideas it can be difficult to know where they came from, especially when those ideas have become a part of your own intuition. Taking a snapshot of them is a good way to let other people know where to go if they want to learn the things I am learning too.

Marco Consolaro spent an hour pair programming with me remotely. He explained how to approach TDD by testing one behaviour completely before moving on, and using an 'obvious' implementation approach where you write the simplest possible code to pass the test and refactor regularly. We also talked about acceptance testing which is where we test behaviours at the business domain level, in comparison to unit testing which is testing the tiny steps that make this behaviour work internally.

Marco also co-wrote a book that made TDD a lot more accessible for someone like me who was new to it. It's called Agile Technical Practices Distilled.

Andy Palmer spent a large amount of time discussing object-oriented and TDD concepts with me. After our talks I would write up my understanding of what we discussed. Afterwards, he would read over it and point out any areas where there were misunderstandings and also where I had understood things well. This happened over several iterations. He also built the Mars Rover with me the first time through so any particularly good design practice here is credited to him. He is fantabulous.

Another resource that really influenced me was The World's Best Intro to TDD by Joe B. Rainsberger who gave the invaluable advice to learn how to ship a hello world program to version control with tests set up. I learned a lot about Pharo just by spending a solid week learning how to make a simple shippable program that other people could use.

Mars Rover Problem Statement

A squad of robotic rovers are to be landed by NASA on a plateau on Mars.

This plateau, which is curiously rectangular, must be navigated by the rovers so that their on board cameras can get a complete view of the surrounding terrain to send back to Earth.

A rover's position is represented by a combination of x and y coordinates and a letter representing one of the four cardinal compass points. The plateau is divided up into a grid to simplify navigation. An example position might be 0, 0, N, which means the rover is in the bottom left corner facing North.

In order to control a rover, NASA sends a simple string of letters. The possible letters are 'L', 'R' and 'M'.

'L' and 'R' makes the rover spin 90 degrees left or right respectively, without moving from its current spot. 'M' means move forward one grid point, and maintain the same heading.

Assume that the square directly North from (x, y) is (x, y+ 1).

Input

The first line of input is the upper-right coordinates of the plateau, the lower-left coordinates are assumed to be 0,0.

The rest of the input is information pertaining to the rovers that have been deployed. Each rover has two lines of input. The first line gives the rover's position, and the second line is a series of instructions telling the rover how to explore the plateau.

The position is made up of two integers and a letter seperated by spaces, corresponding to the x and y co-ordinates and the rover's orientation.

Each rover will be finished sequentially, which means that the second rover won't start to move until the first one has finished moving.

Output

The output for each rover should be it's final coordinates and heading.


Test Input:

  
  5 5
  1 2 N
  LMLMLMLMM
  3 3 E
  MMRMMRMRRM
  
  

Expected Output:

  
  1 3 N
  5 1 E
  
  

Working out examples of the problem by hand

Before writing any code or trying to break down the problem statement into objects, it really helps to get a piece of paper and work out a couple of examples by hand. I'm a visual learner to so this really helps me get 'closer' to the problem and get a real sense of meaning from the text and numbers.

blue 5 by 5 grid with a purple and orange triangle with a path to their start and end points, workings out shown

The first time I drew out the problem above, I thought the rovers were inside the grid squares instead of at the intersections. Solving problems by hand like this will let you catch errors that would otherwise cause problems if you encountered them for the first time in the build itself.

Build Process