 <p>You should print values of <code>x</code> and <code>y</code> in your <code>hopper</code> function. Since <code>chips2</code> contains indices in the matrix with value 2, then the index <code>[9][9]</code> will be contained in <code>chips2</code> implying <code>x = 9</code> or <code>y = 9</code> at some point. </p> <p>Therefore <code>matrix[x+1][y+1]</code> would mean <code>matrix[10][10]</code> which does not exist. You should review your code.</p> <p>you should do:</p> <pre><code>if x &lt; 8: if matrix[x+1][y+1] != 0 and matrix[x+2][y-2] == 0: #up + right . . . </code></pre> <p>In that way, you don't perform a check on an index that is out of range</p>