Mancala Game (with AI)

Version 1.0.0 (25.3 KB) by Benjamin
The classic game, Mancala. Includes a simplistic (and deterministic) AI to help you practice.
14 Downloads
Updated 9 Jun 2022

View License

Ensure you have both MancalaAI.m and MancalaAI.fig installed.
Setup
To start the game, enter MancalaAI into the Command Window. This will bring up the board's GUI.
The GUI has several elements: The board, a "Pass Turn" button, a current player indicator, a "No more moves?" button, a reset button, and the AI's interface.
Moving Marbles
In the middle of the board are six buttons labeled "Here". Upon pressing the button, the GUI will automatically deposit the proper amount of marbles (represented as simply numbers) into the following pits/score areas depending on the current player's turn:
  • If the "Current Player" section reads "Red", then it will take the number from BELOW the button and place them COUNTERCLOCKWISE, also depositing a single marble in the "Red's Score" area. If the "Current Player" section reads "Blue",
  • If the "Current Player" section reads "Blue", then it will take the number from ABOVE the button and place them COUNTERCLOCKWISE, also depositing a single marble in the "Blue's Score" area.
Capturing
Capturing is also fully functional within the system, and the AI will also account for a variety of capture situations.
  • A capture occurs when your final marble lands on an empty pit on your side of the board. If the pit opposite of this pit has marbles inside of it, then you take that marble and all marbles on the opposite side, adding them to your score.
  • Capturing does not grant you an extra turn.
Extra Turns
  • A player or AI earns an extra turn whenever they deposit the last marble of their chosen pit into their score basin. For example, taking 4 marbles in column 3 (explained below) will result in the 4th marble being placed directly into the score basin. This will allow you to take an extra turn. To take an extra turn, simply press another "Here" button that contains marbles on your side.
How to Play
The game always starts with the Red player going first. You can change this to be the Blue player instead by pressing the "Pass Turn" button. When a player makes a turn that does not result in them receiving an extra turn, they should press the "Pass Turn" button such that the next player can make their move.
In the case of facing the AI, the AI will ALWAYS be the Blue player. On the AI's turn, press the "Pass to AI" button located on the top right of the GUI. The AI will interpret the current state of the board and present which column it chooses to move in the panel below the button. It will read "Best move is pit#:" followed by the chosen column. Columns are numbered 1 through 6, starting with the leftmost "Here" button. Once the AI has made its choice, press the chosen column's "Here" button while the "Current Player" section reads "Blue". If the AI were to receive an extra turn for this action, press the "Pass to AI" button again, and repeat until it makes a move that no longer grants an extra turn. When this occurs, press the "Pass Turn" button and make your move(s).
Endgame
The game ends once there are no marbles left anywhere that isn't the score basins. When this occurs, press the "No More Moves?" button in the bottom right of the GUI to calculate any final scores and declare the winner.
  • IMPORTANT: If a player's move were to leave them with NO MARBLES ANYWHERE on their side of the board (indicated by their color), then the player must press the "No More Moves?" button, immediately ending the game. All remaining Marbles on the opponent's side will be collected and added to the opponent's final score.
Resetting/New Games
The game can be reset back to its default state by pressing the "Reset Game" button just above the main Mancala board. This will reset all scores to 0, all pits to 4, and change the current player back to Red. Since the AI is deterministic, it does not get reset by this button, and continues on as normal.
Extra Notes
  • The AI functions by first viewing its own side of the board, and logging all values it sees. It then does the same for the opponent's side. It then follows a strict priority of behavior (which you can exploit if you have trouble beating it!):
  1. If a move can be made that would grant an extra turn to the AI, it will always choose the column closest to its score basin (prioritizes column 1)
  2. If there is no move that would grant an extra turn, the AI then does an "easyCapture" check. If ANY of its columns contain exactly 13 marbles, it will choose the column closest to its score basin (prioritizes column 1). This guarantees at least +2 marbles to the AI's score.
  3. If there are no moves that grant extra turns and there are no easy captures identified, then the AI checks if ANY of their pits have 0 marbles. If it does, then it does a more complicated check of their own and opponent's pits to determine if a capture is possible. If it is, then the AI will choose the column that results in a capture, with varying column priority. Usually, it will prioritize columns closest to its score basin (prioritizes column 1).
  4. If there are no moves that grant extra turns and no captures are possible, then the AI will move the smallest pit of marbles on its side. If there are multiple pits with the same amount of smallest marbles, then the AI will prioritize the column closest to its score basin (prioritizes column 1).
Game and AI developed by Benjamin Michael Butter, Undergraduate Biomedical Engineering student at Louisiana Tech University while working at the University of Arkansas in Fayetteville under Dr. Kyle P. Quinn.

Cite As

Benjamin (2024). Mancala Game (with AI) (https://www.mathworks.com/matlabcentral/fileexchange/112950-mancala-game-with-ai), MATLAB Central File Exchange. Retrieved .

Butter. B.M., Mancala Game (with AI) https://www.mathworks.com/matlabcentral/fileexchange/<...>), Jun 2022, MATLAB Central File Exchange. Retrieved mmm dd, yyyy.

MATLAB Release Compatibility
Created with R2022a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0