Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The main issue here is I believe the usage of object variables instead of local variable.</p> <p><code>self.move</code> is an <em>object variable</em>, every time you change it - every level of the recursion "sees" the change, which is usually a bad thing for recursive algorithms.</p> <p>Recursive algorithms should be self contained, and do minimal if any change on the calling environment - it makes it much easier to walk over the flow of the algorithm.</p> <p>The two main issues I see in this code are:</p> <ol> <li><code>self.move</code> should be a <em>local</em> variable (declare it as <code>move</code>). When you later do: <code>board.unmakeMove(self.move, player)</code> - I suspect that the board is undoing a different move, which was set deeper in the recursion tree, and not the one you intended. Using a local variable will eliminate this undesired behavior.</li> <li>Each level of the recursive call is setting <code>self.max_eval = float('-infinity')</code> - so you constantly change it, though it is probably not what you want to do.</li> </ol> <p>The solution should be something like that:</p> <pre><code>def negaMax(self, board, rules, ply, player): """ Implements a minimax algorithm. """ if ply == 0: return self.positionEvaluation() max_eval = float('-infinity') move_list = board.generateMoves(rules, player) for move in move_list: board.makeMove(move, player) currentEval = -self.negaMax(board, rules, ply - 1, board.getOtherPlayer(player)) board.unmakeMove(move, player) if currentEval &gt; max_eval: max_eval = currentEval return max_eval </code></pre> <p>I am not 100% certain it will indeed solve everything in the code (but it will solve some of it) but I am 100% certain avoiding object variables will make your code much easier to understand and debug.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload