As far as I understood, your main problem is the following: you have been shown how to use minimax in a situation where max / min go in cycle and now you have a game where sometimes one player can do multiple moves in a row.
I will explain you the general approach which works for basically any game, and then I will add a couple of things that can be done differently for mancala.
So the general approach
Standard minimax goes like this:
function minimax(node, depth, maximizingPlayer)
if depth = 0 or node is a terminal node
return the heuristic value of node
if maximizingPlayer
bestValue := -∞
for each child of node
val := minimax(child, depth - 1, FALSE)
bestValue := max(bestValue, val)
return bestValue
else
bestValue := +∞
for each child of node
val := minimax(child, depth - 1, TRUE)
bestValue := min(bestValue, val)
return bestValue
Where you initialize the minimax call with max/min and then it constantly changes. In your case you only need to add one tiny check.
function minimax(node, depth, maximizingPlayer)
if depth = 0 or node is a terminal node
return the heuristic value of node
if maximizingPlayer
bestValue := -∞
for each child of node
# here is a small change
if freeTurn(child):
isMax := TRUE
else:
isMax := FALSE
val := minimax(child, depth - 1, isMax)
bestValue := max(bestValue, val)
return bestValue
else
bestValue := +∞
for each child of node
# here is a small change
if freeTurn(child):
isMax := FALSE
else:
isMax := TRUE
val := minimax(child, depth - 1, isMax)
bestValue := min(bestValue, val)
return bestValue
Where your function freeTurn
returns you whether you have a free turn after your current move. Note that there is no difference for this algorithm whether you can do only 2 moves in a row or 5 moves in a row.
Regarding Mancala
There are many variations of mancala, but the branching factor of the game is pretty small (in the one that I know is <= 6). Now assume that you have three moves A
, B
, C
, D
and move C
allows you to play one more time. From the position C
you can do moves C1
, C2
. So you can combine them (C + C1
, C + C2
) and treat them as just one move (small bookkeeping should be done to remember that this is actually two moves). So right now you end up with your min max iterations where you have not 4 but 5 moves: A
, B
, C + C1
, C + C2
, D
. Actually there is nothing wrong to use this approach for games with bigger branching factor.