 <p>I would suggest, that you define a path in another more suitable way:</p> <pre><code> int pathDeltas[][] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1}, // Up, down, left, right {1, 1}, {-1, 1}, {1, -1}, {-1, -1}, // diagonal paths }; </code></pre> <p>Then you can start from the kind position and adds deltas to x and y coordinates until you hit 1 or 8 value. Also you could calculate the knight paths like this:</p> <pre><code>int knightDeltas[][] {{1, 2}, {2, 1}, {-1, 2}, {-2, 1}, {1, -2}, {2, -1}, {-1, -2}, {-2, -1}}; </code></pre>