brandonhsiao/rgkit — pull request #50 by ncanceill (2013-11-24 15:02:36+00:00)
Title: Check walkability in rg.toward()

# Check walkability in `rg.toward()`

This improves the `toward(curr, dest)` method in `rg.py` by adding optional walkability checks

## Description

### The issue

In the current implementation, `rg.toward(curr, dest)` may return an `obstacle` location. I believe this makes it harder to write movement instructions for the robots.

For instance, something like
```python
def act(self,g):
return ['move', rg.toward(self.location,destination)]
```
may fail: with `self.location` at `(2,5)` and `destination` at `(3,3)`, `toward` will return `(2,4)`, which is an `obstacle`.

### The fix

This small change adds an optional argument `nocheck` — set to `True` by default to preserve compatibility. When set to `False`, it will make sure that the returned location is neither `invalid` nor an `obstacle`.

**WARNING:** the check will not be properly enforced if one of `curr` or `dest` is itself either an `obstacle` or an `invalid` location.

As a side-effect, this change adds a convenient `walkable(loc)` method, which acts as a shortcut to check if the `loc_types(loc)` define a walkable location — _ie_ `normal` and non-`obstacle`.

## Comments

* I am pretty happy with the optional argument trick to preserve the original behavior. As a result, people forcing `nocheck=False` should be aware that it is meant for walkable `curr` and `dest`, so there goes the above warning.
* I am not completely satisfied with the "code style" of my change, feel free to improve or criticize on that.
* I would love to write unit tests for that, but I am a bit puzzled by their implementation. I would greatly appreciate it if anyone could held me about that.