Creating Mancala in C++

Mancala is a board game first developed in Egypt, according to its history as found in Ultra Board Games. The game has many names and was first introduced in the United States as Chuba. Mancala is an Arabic name that is based on the movement of the stones. It is a good game to develop in a computer language as it forces the user to use many important features of each language in order to efficiently program the game. In my C++ classes, I break the develop of this code into multiple sections. The first covers writing functions to print a varying length solid line and to print a varying length dotted lines using the characters ‘ ‘ and ‘*’. The second section adds the pit numbers and the stones in each pit. The board has six indentations on either player side and two larger indentations at the end, called Mancalas. The general purpose is to trap as many stones in your own Mancala as possible. The names for the indentations and playing pieces vary quite a bit. For this discussion and my example, I use “pit” for each indentation and “stone” for each playing piece. All of the code is shown in the following videos. I decided to not put in any of the code here (besides the array definition, because students learn so much more by typing the code themselves. The array line for the board’s pits is:

int board[] = { 4,4,4,4,4,4,0,4,4,4,4,4,4,0 };

Each player’s pits are initially filled with four stones each. The two Mancalas are left empty. The pits 0-5 are player 1’s side and pit 6 is player 1’s Mancala. The pits 7-12 are player 2’s side and pit 13 is player 2’s Mancala.

This will create the board. The next code that needs to be written is to create the rules for two players to play the game. The rules are fairly simple after the board is set up with four stones in each of the payers’ six pits.

Adding Moves to the Mancala Game

  1. The two players decide who goes first.
  2. The first player picks up all of the stones in one of their six pits.
  3. They place the stones one at a time in each of the subsequent pits (going counterclockwise) until they run out of stones.
  4. If the last stone lands in their own Mancala, they are able to go again.
  5. Once all the pits are empty on one side, the other player removes all the stones from their pits and puts them into their own Mancala.
  6. The winner has the most stones.

There are a few additional rules, but these are the rules that are included in this demonstrations.

The game still needs to have a capture function written as well as a quick check to see who won. Thank you for looking at this page.

Add Capture of Stones Rule

If you have enough stones to go completely around the board and land the last stone in an empty pit, you are able to take your own stone and all the stones in the opposite pit on your opponent’s side and place all of them in your own Mancala. The interesting fact is that all opposing pits when added to the current player’s landing pit add up to twelve. This makes calculating the opponent’s pit easy. Since this is an easy calculation, I am also adding looping to this example so the users can play multiple games instead of having to restart the process for each game. Because multiple games are now allowed, I am also statistics to the program.

Adding File Input/Output

Once the game has been completed, it is time to start saving the scores to a file. This is done with the include files <fstream> and <sstream>. Both must be added to your include section of the program:

#include <fstream>
#include <sstream>

To read or write to a file, the file must be opened. To prevent memory leaks, be sure to close the file after you have completed your use of the file. Memory leaks are where a connection to a file is kept open and never closed. If the code does this repeatedly, new connections with that file will continue to be opened and none closed. It may be slow, but over time the size of the executable file will continue to grow until it eventually will run out of allocated space. A read from a file would be something like:

ifstream inputFile;
string fileName = "mancala.txt", userName;
int games, wins, losses, ties;
// read the information from the file.
inputFile.open(fileName);        // Try to open
if(inputFile) {                  // If the file was able to be opened, read
    input >> userName >> games >> wins >> losses >> ties;
}

I would enjoy hearing from you.

This site uses Akismet to reduce spam. Learn how your comment data is processed.