Mancala, also known as the Kalaha, is a game invented in the United States by William Julius Champion, Jr. in 1940. For most of its variations, Mancala is a solved game with a first-player win if both players play perfect games. In this project, I mainly use multiway graphs to represent the first two steps of Mancala, trying to find some patterns from the graphs.
Introduction to Mancala
Introduction to Mancala
Mancala is a game played by two opponents. The picture of the Mancala board is shown below.
We can visualize the Mancala by using Wolfram Language.
In[]:=
Board={{4,4,4,4,4,4,0},{4,4,4,4,4,4,0}}View[x_List]:={Last[First[x]],Column[{Framed/@Most[First[x]],Most[Framed/@Last[x]]}],Last[Last[x]]}View[Board]
Out[]=
{{4,4,4,4,4,4,0},{4,4,4,4,4,4,0}}
Out[]=
0,
,0
|
|
Here I listed the rules for classical Mancala games.
◼
As we start to play the game, the board and seeds are initialized as shown in the picture above.
◼
Each of the two players possesses one of the 2 empty stores and 6 pits with 4 seeds in them respectively. The player's score is the number of seeds in the store to their right.
◼
Players take turns moving their seeds. On each turn, the player removes all seeds from one of the pits under their control. The player drops one seed in each pit in turn, including the player’s store but not their opponent’s. The game is played counterclockwise.
◼
If the last sown seed lands in an empty pit owned by the player, and the opposite pit contains seeds, both the last seed and the opposite seeds are captured and placed into the player's store.
◼
If the last sown seed lands in the player's store, the player gets an additional move. There is no limit on the number of moves a player can make in their turn.
◼
When one player no longer has any seeds in any of their pits, the game ends. The other player moves all remaining seeds to their store, and the player with the most seeds in their store wins.
Building Mancala Rules
Building Mancala Rules
First, we visualize our board.
In[]:=
Board={{4,4,4,4,4,4,0},{4,4,4,4,4,4,0}}
Out[]=
{{4,4,4,4,4,4,0},{4,4,4,4,4,4,0}}
The function below actually takes the first list and the second list to visualize every position of the Mancala.
In[]:=
View[x_List]:={Last[First[x]],Column[{Framed/@Most[First[x]],Most[Framed/@Last[x]]}],Last[Last[x]]}
In[]:=
View[Board]
Out[]=
0,
,0
|
|
When we are building the rules from Mancala into Wolfram languages, we build them step by step.
The very first step is to choose a value from a board and get both its index and the “seeds” inside the pit.
The very first step is to choose a value from a board and get both its index and the “seeds” inside the pit.
In[]:=
PickVal[y_List,a_Integer,b_Integer]:=y[[a]][[b]]
To pick the value in the first board and the second pit, we apply the previous function like this.
In[]:=
PickVal[Board,1,2]
Out[]=
4
Since Mancala is played by two players one by one, we can create the first person’s moving function, or so-called transition function, first. Then, we can imitate what we have done in the first section to create the second transition function.Here are lots of mathematical computations.1. We consider three different situations --- the value we choose to move in the pit may exceed the pits left in the first list, the value we choose to move is just as same as the number of the pits in the first list, and the value may smaller than the pits left in the first list.To illustrate, let’s see these three simple situations. 1. 2. 3.
0,
,0
|
|
1,
,0
|
|
1,
,0
|
|
2. Then we make the rules (4) and (5) mentioned in the introduction in this huge function.
3. We use the function EndQ to find whether the game is end according to rule (6).
3. We use the function EndQ to find whether the game is end according to rule (6).
In[]:=
EndQ[l_List]:=Module[{x,y},If[Total[l[[1]]]-l[[1]][[7]]===0||Total[l[[2]]]-l[[2]][[7]]===0,x=Total[l[[1]]];y=Total[l[[2]]];View[{{0,0,0,0,0,0,x},{0,0,0,0,0,0,y}}],l]]
In[]:=
AddSeeds[a_List,i_Integer]:= Module[{x,y,z,rec},Which[i-PickVal[a,1,i]>0,(x={(a[[1]]+Join[Table[0,i-PickVal[a,1,i]-1],Table[1,PickVal[a,1,i]],Table[0,7-i+1]]),a[[2]]};x[[1]][[i]]=0;If[x[[1]][[i-PickVal[a,1,i]]]-1===0&&x[[2]][[i-PickVal[a,1,i]]]!=0,x[[1]][[7]]=x[[1]][[7]]+x[[1]][[i-PickVal[a,1,i]]]+x[[2]][[i-PickVal[a,1,i]]];x[[1]][[i-PickVal[a,1,i]]]=0;x[[2]][[i-PickVal[a,1,i]]]=0]),i===PickVal[a,1,i],x={(a[[1]]+Join[Table[1,i-1],Table[0,7-i],{1}]),a[[2]]};x[[1]][[i]]=0;rec=Table[AddSeeds[{x[[1]],a[[2]]},j],{j,6}],i<PickVal[a,1,i],x={(a[[1]]+Join[Table[1,i-1],Table[0,7-i],{1}]),a[[2]]};y=PickVal[x,1,i]-i;x[[1]][[i]]=0;z=QuotientRemainder[y,13];(x[[1]]=x[[1]]+z[[1]];x[[2]]=x[[2]]+z[[1]];x[[2]][[7]]=x[[2]][[7]]-z[[1]]);If[z[[2]]===0&&z[[1]]!=0,Table[AddSeeds[{x[[1]],x[[2]]},j],{j,6}],Nothing];If[z[[2]]<= 6,x[[2]]=x[[2]]+Join[Table[1,z[[2]]],Table[0,7-z[[2]]]],x[[2]]=x[[2]]+Join[Table[1,6],Table[0,1]];x[[1]]=x[[1]]+Join[Table[0,13-z[[2]]-1],Table[1,z[[2]]-6],{0}];If[x[[1]][[13-z[[2]]]]-1===0,x[[1]][[7]]=x[[1]][[7]]+x[[1]][[13-z[[2]]]]+x[[2]][[13-z[[2]]]];x[[1]][[13-z[[2]]]]=0;x[[2]][[13-z[[2]]]]=0]]];new={x[[1]],x[[2]]};AppendTo[Boardx,new]]
After we have finished writing the function for the first player, we are ready to go for the second player.
This time, the rules and ways we think to build the function are as same as before. However, we must be careful that the seeds are always distributed in counterclockwise. This means we need to make a slight adjustment to our calculations.
The function for the second player to move the seeds is shown below.
This time, the rules and ways we think to build the function are as same as before. However, we must be careful that the seeds are always distributed in counterclockwise. This means we need to make a slight adjustment to our calculations.
The function for the second player to move the seeds is shown below.
In[]:=
AddSeeds2[b_List,j_Integer]:=Module[{x,y,z,rec},Which[7-j>PickVal[b,2,j],(x={b[[1]],(b[[2]]+Join[Table[0,j],Table[1,PickVal[b,2,j]],Table[0,7-j-PickVal[b,2,j]]])};x[[2]][[j]]=0;If[x[[2]][[PickVal[b,2,j]+j]]-1===0&&x[[1]][[PickVal[b,2,j]+j]]!=0,x[[2]][[7]]=x[[2]][[7]]+x[[2]][[PickVal[b,2,j]+j]]+x[[1]][[PickVal[b,2,j]+j]];x[[1]][[PickVal[b,2,j]+j]]=0;x[[2]][[PickVal[b,2,j]+j]]=0;]),7-j===PickVal[b,2,j],(x={b[[1]],(b[[2]]+Join[Table[0,j],Table[1,PickVal[b,2,j]]])};x[[2]][[j]]=0;rec=Table[AddSeeds2[{b[[1]],x[[2]]},i],{i,6}]),7-j<PickVal[b,2,j],x={b[[1]],(b[[2]]+Join[Table[0,j],Table[1,7-j]])};y=PickVal[x,2,j]-7+j;x[[2]][[j]]=0;z=QuotientRemainder[y,13];(x[[1]]=x[[1]]+z[[1]];x[[2]]=x[[2]]+z[[1]];x[[1]][[7]]=x[[1]][[7]]-z[[1]]);If[z[[2]]===0&&z[[1]]!=0,Table[AddSeeds2[{x[[1]],x[[2]]},i],{i,6}],Nothing];If[z[[2]]<=6,x[[1]]=x[[1]]+Join[Table[1,z[[2]]],Table[0,7-z[[2]]]],x[[1]]=x[[1]]+Join[Table[1,6],Table[0,1]];x[[2]]=x[[2]]+Join[Table[1,z[[2]]-6],Table[0,7-z[[2]]+6]];If[x[[2]][[z[[2]]-6]]-1===0&&x[[1]][[z[[2]]-6]]!=0,x[[2]][[7]]=x[[2]][[7]]+x[[2]][[z[[2]]-6]]+x[[1]][[z[[2]]-6]];x[[2]][[z[[2]]-6]]=0;x[[1]][[z[[2]]-6]]=0]]];new={x[[1]],x[[2]]};AppendTo[Boardy,new]]
We have successfully created the two functions. The two functions listed below are used to generate data. They can choose the pits from 1-6 from each side and return the results of all possible choices.
In[]:=
Step2:=Table[AddSeeds2[Board,i],{i,6}]
In[]:=
Step1:=Table[AddSeeds[Board,i],{i,6}]
Sample Play of Mancala
Sample Play of Mancala
To better illustrate how Mancala is played, here I will present a process of playing it. Check out if you are unfamiliar with Mancala.
◼
Initializing the game: The classic Mancala game is initialized with 0 seeds in each store and 4 seeds in each pit.
In[]:=
Board={{4,4,4,4,4,4,0},{4,4,4,4,4,4,0}}
Out[]=
{{4,4,4,4,4,4,0},{4,4,4,4,4,4,0}}
In[]:=
View[Board]
Out[]=
0,
,0
|
|
◼
Now let's begin to choose one of the pits and move all the seeds in the pit according to the rules stated above.
The cell below is different from the initial cell since it is not used to generate a large quantity of data. Therefore, please run the two cells below before starting your game. AddSeedsOne represents the player one will play and AddSeedsTwo represents the player two will play.
Step11 represents the first move of player one and Step12 represents the player two moves from the position of Step11.
You may try this process for several times to get familiar to Mancala.
◼
Here are how the rules 4, 5, and 6 works.
◼
Rule4: Assume the board is initialized as {{4,4,4,4,0,4,0},{4,4,4,4,4,4,0}} and player 1 moves first. If we move the first seed of player one, the last seed in that pit will stay in the fifth pit, which contains 0 seed initially. In addition, player 2’s fifth pit contains four seeds. Therefore, player 1 will take player 2’s seeds in the fifth pit along with the last seed, getting 5 seeds in total to the store.
◼
Rule5: Assume the board is {{4,4,4,4,4,4,0},{4,4,4,4,4,4,0}} and player 1 moves first. If player one moves the seeds in the fourth pits, player one gets a bonus move as the last seed ended up in the store.
◼
Rule6: Assume the board is {{1,2,3,4,5,6,0},{0,0,0,0,0,0,10}}. This is the end of the game. Player one will collect all the seeds.
Above all, I have explained all the rules that might be confusing. You may explore this game by playing it several times.
MultiwayGraph
MultiwayGraph
Let’s begin to create the multiway graph of all the outputs we have got so far.
First, we generate data and put them into lists.
First, we generate data and put them into lists.
The function below is used to create associations between values, helping us to create the graph.
Graph 1
Graph 1
I attached the data for this section of the graph below in the “Data” section, under the “Graph1” subtitle.
The graph I created here represents that the initial position will have 6 different inputs which will generate all the possible results below. This graph consists of all possible positions the Mancala will end after the two players play for the first round.
The first graph illustrates that game four will result in lots of possible outcomes compared to other kinds of games. This is because when player one moves the fourth pit which is initialized with four seeds, the last seed will end up in the store. Therefore, player one gets one free move. As a result, after the play of player two, there should be many more different situations compared to other choices.
◼
Click to view the edge color definition
To make the graph looks better, I made the first player’s move in red color. From this graph, we notice that node 5, which represents the fourth choice of the first player emits large branches compared to other branches.
Graph 2
Graph 2
Now, we are getting ready to create the second graph, in which the players play the game twice. I attached the data for this section of the graph below in the “Data” section, under the “Graph2” subtitle.
When I was trying to add all of the information of the first player's moves into the graph, the graph cannot be shown due to the large quantity of data. Therefore, I chose part of the data and plot them in the graph below.
Therefore, this graph only represents all the possible positions after player one moves twice and player two moves once.
From this graph, we observe that after the first move of both players and the second movement of the first player, most of the games concentrate on the side which is previously derived from the fourth choice among the six initial choices. This shows how a slightly different strategy can lead to great variations in games.
I also added some red edges to make the graph looks nicer.
From the graph below, we can see the pattern that the branches of this game do not interconnect with each other frequently. I suppose this is consistent with the property of Mancala since each time one seed goes into the store, the game should be entirely different from others, resulting in a few situations that are derived from two or more different situations.
Conclusion
Conclusion
Above all, I have made the game Mancala and also analyzed the graph I created to represent the development of the game. I have successfully embedded all the classical Mancala rules into the game. From the game, users can play Mancala by using the function AddSeedsOne and AddSeedsTwo alternatively. By using the function EndQ, users can find out whether the game is ended.
The analysis of the two graphs reflects that if player one chooses to move seeds in the fourth pit, there will be more possible outcomes than other choices. Besides, the graphs I drew are similar to the tree graph as not too many games will result in the same outcome. Therefore, the branches of the graphs are more discrete instead of interconnected.
Future work will be concentrated on creating graphs of the Mancala with more depth. To achieve this goal, several steps can be done.
1. Making the Mancala rules into the repository function Multiway Graph.
2. To make the algorithm more efficient to generate data more quickly.
3. To arrange data in a more organized way.
Besides creating Multiway Graph, I can also use the result of the Graph with more depth to analyze whether player one or two has an advantage while playing.
The analysis of the two graphs reflects that if player one chooses to move seeds in the fourth pit, there will be more possible outcomes than other choices. Besides, the graphs I drew are similar to the tree graph as not too many games will result in the same outcome. Therefore, the branches of the graphs are more discrete instead of interconnected.
Future work will be concentrated on creating graphs of the Mancala with more depth. To achieve this goal, several steps can be done.
1. Making the Mancala rules into the repository function Multiway Graph.
2. To make the algorithm more efficient to generate data more quickly.
3. To arrange data in a more organized way.
Besides creating Multiway Graph, I can also use the result of the Graph with more depth to analyze whether player one or two has an advantage while playing.
Data Generating (Unnecessary to View)
Data Generating (Unnecessary to View)
◼
It is unnecessary to view the data. However, if you would like to see them, you can access them by clicking Graph1 or Graph2.
References
References
1. Wikipedia contributors. (2022, July 18). Kalah. Wikipedia. https://en.wikipedia.org/wiki/Kalah
2. How to play Mancala.(2022, July 18). Alycia Zimmerman https://harriscenter.org/wp-content/uploads/2020/03/mancala_rules.pdf
2. How to play Mancala.(2022, July 18). Alycia Zimmerman https://harriscenter.org/wp-content/uploads/2020/03/mancala_rules.pdf