So I was looking up Mini-max for a Tic-Tac-Toe Game, but couldn’t understand how the recursion worked? Okay, so basically here are my questions:
- How does minimax know whose turn is it? Whats the best way to indicate the player whose turn it is generating?
- How do you generate possible moves?
- How do you know when you are at a terminal node, and how do you generate the terminal nodes?
For example in this Pseudo-code
function integer minimax(node, depth)
if node is a terminal node or depth <= 0:
return the heuristic value of node
α = -∞
for child in node: # evaluation is identical for both players
α = max(α, -minimax(child, depth-1))
return α
A node is a board correct? And is the depth how many plies the code has to go down in recursion? Also what is the max function and where are the nodes being generated from?
Now, so far I have this code for creating a board:
class Board{
public:
Board();
~Board(){};
public: // The board
// In the board, 1 is x, 2 is o, 0 is empty square.
int board[3][3];
};
But how would I know whose turn is it? And how do I generate the child nodes for the board?
We’ll use your tic-tac-toe as an example first.
Looking at your pseudocode:
max(a, b)is any function that returns the larger ofaorb. This is usually provided by a math library or similar.depthis the maximum depth to which you will search.1for a board position that wins for the player doing the analysis,-1for a board position that wins for the other player, and0for any inconclusive position. In general, you’ll have to cook up a heuristic yourself, or use a well-accepted one.If you haven’t worked with graphs or trees yet, I suggest you do so first; the tree primitive, in particular, is essential to this problem.
As an answer to a comment in this thread asking for an example of determining whose turn it is for a given node, I offer this pseudo-Python:
Each node is capable of keeping track of its absolute depth from the ‘root’ node. When we try to determine how we should generate board positions for the next move, we check to see whose move it is based on the parity of our depth (the result of
self.depth % 2) and our record of who moved first.