Monday, 23 March 2015

Slaying The Foe Of Minimax

*Official "revisitation" post is on April 2nd

Finally! After multiple revisitations, I finally managed to crack Minimax. Shipping the non-functional code for assignment 2 was a disappointing experience, albeit slightly relieving because I could now justify my withdrawal from the agony of trying to get it to work like the doc string says.

The problem? My get_score function did not handle recursive calls in a way that actually fit GameState's properties. I split Minimax into two functions: suggest_move to return a move to the game state, and get_score to see what the best move(s) were for the computer. Most of the heavy lifting was done in get_score, which would check the GameState it was passed first and (pending a non-terminal state) would proceed recursively until it found finished games. Where I ran into trouble, and where I will guess many others did too, is with how many levels down (to analogize possible GameStates to a tree) the function checked.

If get_score begins by asking "Can I win?" of the current GameState, it is left with a response that satisfies a base case. The game is either over (leading to a win, lose or tie), or the game is not over and the possible subsequent game states must be assesed from the frame of reference of the same player. The answer may seem simple: Because of our game's zero-sum nature, we assume that the score of Minimax's opponent can simply be multiplied by minus one; but this does not really inform us of what the possibilities are for the computer. Supposing, at level 2, the game was still incomplete and the game is winnable for the opponent. This does not mean that the game is not winnable for Minimax! The only case we care about at this level, is if the computer has won or tied the game.

What I had to do was go two layers below any game state passed into get_score, to get to the next state where the computer was the state.next_player. This subsequent GameState could be asked, again, "Can I win?" and the framework would therefore be set for a recursive algorithm. Each recursive call would therefore not only check the GameStates one move away, but also the ones two moves away. The next recursive call would start where the last one left off, resetting the state.next_player to the computer.

No comments:

Post a Comment