Today"s guest blogger is Anoush Najarian that leads the MATLAB performance Team at And while she mostly concentrates on help MATLAB run fast, in she spare time, she likes to use MATLAB for hobby jobs in robotics, math, and games.

You are watching: How to win mancala in one move


Winning at Mancala

Let me tell you what occurred when i got exhausted of shedding at Mancala, and decided to write some MATLAB password to play it. Shout-out to my daughter, 6th grader Natalie, for introducing me come the game, and also being a partner in these experiments.


The Rules

Now, over there are numerous ways to play the gamings in the Mancala family. The rule collection we created the code for is: you pick from any kind of hole, and drop one rock at a time when circling the plank in counterclockwise fashion, drop a stone into your house whenever you pass with it. If girlfriend drop your last rock into her home, you get a "free" turn. If friend drop her last stone into a non-empty hole, you obtain to proceed with what I speak to an "automatic" move, choose up all stones from that hole. In the intial position, there are four stones in every hole.

See more: How Many Moles Are Present In 17.4 G Of Lead To Moles, How Many Moles Are Present In 17


Is over there a First-Player Advantage? (You Bet!)

You know exactly how some games have a first-player advantage? It turns out that in Mancala, girlfriend can uncover a means not just to success (which is nice), however to victory all the marbles (awesome), and also to execute so on her very an initial move!

% below is driver password to discover (one the many!)% all-48-marble-win-on-first-move solutions, which operation in ~20s top top my% laptop!gametrees = 0 <4 4 4 4 4 4 4 4 4 4 4 4> <>;nummoves = 0;while nummoves % display screen one highest-score entry for g = 1:L = gametreesg, :; for t = 1:12 if board(t) == 0, continue, end = mancalafirstmove (t, board); if freemove if nummoves maxscore * 0.75 % optmization to prune search tree newwinningstreak = ; newgametrees(end+1, :) = totalscore+score, newboard, newwinningstreak; end end if totalscore+score == 48 disp("Found 48-marble win sequence!"); disp(newwinningstreak); return; end end end gametrees = newgametrees; nummoves = nummoves + 1;end% The driver code calls a move duty which will runs v "automatic"% move recursively. Our password generates a 30-step-long succession of plays% for the scan 48-marble win on your very first move!function = mancalafirstmove (apick, board)score = 0;moves = eye(12);pickspot = apick;freemove = mancalamove(pickspot); duty freemove = mancalamove(pickspot) numpieces = board(pickspot); board(pickspot) = 0; addapiece = moves(pickspot, :); obtain = floor((numpieces-pickspot)/12)+1; score = score + gain; freemove = (pickspot == mod(numpieces, 12)); numpieces = numpieces - gain; for i=1:numpieces board = circshift(addapiece, -1*i) + board; end if (not(freemove)) newpickspot = mod(pickspot-numpieces, 12); if newpickspot==0, newpickspot=12; end if board(newpickspot) > 1 freemove = mancalamove(newpickspot); end end endendFound 48-marble to win sequence! Columns 1 through 13 8 5 4 2 9 5 7 11 9 5 3 9 5 Columns 14 through 26 1 2 1 12 1 10 1 3 1 8 1 6 1 Columns 27 with 30 4 1 2 1Let the battle of the first Move play itself out!


Alternative preeminence Sets

Some that the various other Mancala rule sets the end there include: no "free" move, no "automatic" move, only picking indigenous the side of the plank you space sitting following to, different number of holes, marbles!

For example, mean "automatic" move and complimentary moves room allowed, but you deserve to only location on your side that the board. Then you still have the right to can win catching a pretty outstanding 42 marbles on your very first move! Tiny change on heat 18 of the driver code (loop 1:6 instead of 1:12) will offer you the succession of theatre to use for this variation!

What"s Next?

What we"d really favor to develop up to below is to usage the game-playing code for cultivate the AI. We newly watched interesting videos favor Deep learning in 11 lines of MATLAB Code, and are passionate to try deep reinforcement finding out for games.