This file takes you through four steps in creating a Mancala program using Java. The first three show examples, but the fourth step only states the rules for Mancala and gives you some basic rules with a few options for more complex rules. There is a fix set of established rules that is stated in section four. However, there are a couple of options that might make the game more interesting. If you choose to have different rules than those that are listed, be sure to describe your altered rules in the comment header of your file in which you implement your code.
The steps are:
- Creating a program to create a Mancala Board and two players.
- Add a stub move method to allow testing of the rest of the program.
- Add the basic set of moves to move the stones around the board.
- Follow all the rules for the complete game of Mancala.
Basic Board with Two Players
To start any game, a usable board must be created. Mancala is created for two players. The board has six pits on either side with two Mancalas on either end for each player.
A simple way to create your own Mancala board is to take an egg carton and cut off the lid. Then cut off the two ends of the lid and attach them to the two ends of the bottom of the egg carton. This will create a playing board similar to the one above. The video below tells how to create your own Mancala board with two players in Java. I label the pits with letters, but some people prefer using numbers, either is OK. After viewing the video, if you think you know a better way to lay out the board in a way that makes sense to you, please do so.
Stub of Move Method
To do any moves, a simple move routine needs to be added. This example has just the parameters needed in the call and the variable type of the value being returned. The reason I include this step is to show you how to create a stub program that works enough to test the other code. This is an important part of building good software. Notice that the return value can be 0 (no turns remaining), 1 (next move belongs to player 1), and 2 (the next move belongs to player 2). I use a random number generator to produce the return value of one of these three numbers.
Complete Movement of Stones among Pits
This one takes care of picking up all the stones in one pit and distributing them one stone at a time in each pit progressing counter-clockwise around the board. Each player drops the stones in all pits including their own Mancala (the end pit) but skipping their opponents Mancala. This does not include any of the special rules for what happens when a stone lands in a specific pit. The one exception is that this code handles the last pit falling in a player’s Mancala.
I am also adding some color to the program through an ANSI (American National Standards Institute) escape sequence file to add color during a System.out print statement.
/* ANSI colors to use in other modules/classes
* Wayne Cook
* Created: 3 April 2021: Basic colors.
*/
public class ANSI_Colors {
// Regular Colors
public static final String RESET = "\u001B[0m";
public static final String BLACK = "\u001B[30m";
public static final String RED = "\u001B[31m";
public static final String GREEN = "\u001B[32m";
public static final String YELLOW = "\u001B[33m";
public static final String BLUE = "\u001B[34m";
public static final String PURPLE = "\u001B[35m";
public static final String CYAN = "\u001B[36m";
public static final String WHITE = "\u001B[37m";
// Bold
public static final String BLACK_BOLD = "\033[1;30m"; // BLACK
public static final String RED_BOLD = "\033[1;31m"; // RED
public static final String GREEN_BOLD = "\033[1;32m"; // GREEN
public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW
public static final String BLUE_BOLD = "\033[1;34m"; // BLUE
public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE
public static final String CYAN_BOLD = "\033[1;36m"; // CYAN
public static final String WHITE_BOLD = "\033[1;37m"; // WHITE
// Underline
public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK
public static final String RED_UNDERLINED = "\033[4;31m"; // RED
public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN
public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW
public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE
public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE
public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN
public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE
// Background Colors
public static final String ANSI_BLACK_BACKGROUND = "\u001B[40m";
public static final String ANSI_RED_BACKGROUND = "\u001B[41m";
public static final String ANSI_GREEN_BACKGROUND = "\u001B[42m";
public static final String ANSI_YELLOW_BACKGROUND = "\u001B[43m";
public static final String ANSI_BLUE_BACKGROUND = "\u001B[44m";
public static final String ANSI_PURPLE_BACKGROUND = "\u001B[45m";
public static final String ANSI_CYAN_BACKGROUND = "\u001B[46m";
public static final String ANSI_WHITE_BACKGROUND = "\u001B[47m";
}
Wrapping the Game Up
This section has no code example. It is left to the user to complete the Mancala game. Document your decisions on any changes to the rules so that I know what to do when I test the code.
/* Rules for Mancala play.
* The board is designed for two players. There are six pits on each
* players side and a Mancala (large pit) on either side.
* To start place four stones in each pit, leaving the Mancala empty.
* Decide who goes first.
* One player picks up all the stones in one of their own pits.
* Starting with the next pit over (Counter-clockwise), drop one stone in
* each pit including your own Mancala and skipping your opponents
* Mancala. If the last stone is dropped in your own Mancala, you get to
* play again. If the last stone is dropped into an empty pit on your own
* side and there are stones in the pit opposite the pit where you just
* dropped the last stone, then you get to capture your own stone and all
* the stones in the opposite pit and place them all in your own Mancala.
* Otherwise, it is the other players turn.
* When all the pits on one side are empty, the other player gets to take
* all of the stones on their own side and place those stones into their
* own Mancala.
* At the end, whoever has the most stones in their Mancala wins.
*/
These are the basic rules. However, there are some changes that other students have made. The most common one is who receives the stones left in the opponent’s pits when one player empties all of their own stones. The common rule is that the opponent is able to remove their own stones and move them to their Mancala. However, the game becomes much more interesting if the first person able to empty all the stones from the pits on their own side is able to grab all of the stones in the opponent’s pits. This increases the chances of second player to play in the game to actually win the game.