 <p>The first thing I notice is that you've got a lot of functions that do basically the same thing (with some numbers different). I would investigate adding a couple of parameters to that function, so that you can describe the direction you're going. So for example, instead of calling <code>right()</code> you might call <code>traverse(1, 0)</code> and <code>traverse(0, -1)</code> instead of <code>up()</code>.</p> <p>Your <code>traverse()</code> function declaration might look like:</p> <pre><code>int traverse(int dx, int dy) </code></pre> <p>with the appropriate changes inside to adapt its behaviour for different values of <code>dx</code> and <code>dy</code>.</p>