# Minimax game theory with mancala

• 12-31-2009, 06:14 AM
jigglywiggly
Minimax game theory with mancala
OOook, so Basically make mancala and create a computer ai... easy right? Not really D:

Ok so Basically, I understand how the minimax game theory works, but I am just flabbergasted on how to get it to work with mancala, I am not even trying to worry about making the algorithim yet, just to populate the Nodes...

Obviously I am not asking you to make this, but just how would I get started on making the ManCalaTreeNode and ManCalaTree? I mean I started with random stuff, except that was really just a binary tree...

(The commented out stuff is the binary tree really)

Code:

```/*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */ /**  *  * @author administrator  */ public class ManCalaTreeNode {     ManCalaTreeNode parent;     ManCalaTreeNode[] children;     int depth;     Board gameState;     /* Object element;     ManCalaTreeNode left, right;     ManCalaTreeNode(Object obj) {         element = obj;         left = null;         right = null;     }     public int numChildren() {         int children=0;         if(left !=null) {             children=1+left.numChildren();         }         if(right!=null) {             children=children+1+right.numChildren();         }             return children;     }     */   }```

And then the Actualy ManCalaTree (same thing commented out stuff is just like a binary tree
Code:

```/*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */ /**  *  * @author administrator  */ public class ManCalaTree {     ManCalaTreeNode root;   /* int count=0;     public ManCalaTree() {         count = 0;         root=null;     }     public ManCalaTree(Object element) {     count=1;     root = new ManCalaTreeNode(element);     }     public ManCalaTree(Object element, ManCalaTree leftSubtree, ManCalaTree rightSubtree)     {     root= new ManCalaTreeNode(element);     count = 1;     if(leftSubtree!=null) {         count = count + leftSubtree.size();         root.left = leftSubtree.root;     }     else {         root.left = null;     }     if(rightSubtree!=null)     {         count = count +rightSubtree.size();         root.right=rightSubtree.root;     }     else {         root.right=null;     }     } ManCalaTreeNode root;     *     */ }```

Then for the actual game, well it works for two players, except for capturing, I don't want to add more complications for the AI yet.

Board (you don't really need to see this if I'm honest):
Code:

```/*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */ /**  *  * @author administrator  */ public class Board {     Storage[] board = new Storage[14];     Storage b = new Storage(4);     int positionreal = 0;     int positionreal2 = 0;     int counter = 2;     int counter2 = 0;     boolean hmm = false;     boolean hmm2 = false;     int counter3 = 0;     int temporary = 0;     int temporary2= 0;     boolean player1hasalot = false;     boolean player2hasalot=false;     public Board() {         board[0] = new Storage(4);         board[1] = new Storage(4);         board[2] = new Storage(4);         board[3] = new Storage(4);         board[4] = new Storage(4);         board[5] = new Storage(4);         board[6] = new Storage(0);         board[7] = new Storage(4);         board[8] = new Storage(4);         board[9] = new Storage(4);         board[10] = new Storage(4);         board[11] = new Storage(4);         board[12] = new Storage(4);         board[13] = new Storage(0);     }     public void movethem(String l) {         positionreal = Integer.parseInt(l);                 if (counter % 2 == 0) {             while (counter % 2 == 0) {             /*    for(int forever=0; forever<board[positionreal].getAmount(); forever++) {                     if(board[positionreal].getAmount()-forever==1) {                     }                 }             */                 if(positionreal+board[positionreal].getAmount() <6) {                       if(board[positionreal+board[positionreal].getAmount()].getAmount()==0) {                           if(positionreal+board[positionreal].getAmount()==0) {                               board[6].setAmount(board[12].getAmount());                               board[12].setAmount(0);                           }                             if(positionreal+board[positionreal].getAmount()==1) {                               board[6].setAmount(board[11].getAmount());                               board[11].setAmount(0);                           }                             if(positionreal+board[positionreal].getAmount()==2) {                               board[6].setAmount(board[10].getAmount());                               board[10].setAmount(0);                           } if(positionreal+board[positionreal].getAmount()==3) {                               board[6].setAmount(board[9].getAmount());                               board[9].setAmount(0);                           } if(positionreal+board[positionreal].getAmount()==4) {                               board[6].setAmount(board[9].getAmount());                               board[9].setAmount(0);                           } if(positionreal+board[positionreal].getAmount()==5) {                               board[6].setAmount(board[8].getAmount());                               board[8].setAmount(0);                           }                                         }                 }                 if(positionreal+board[positionreal].getAmount() > 13 && positionreal+board[positionreal].getAmount() <20) {                       if(board[positionreal+board[positionreal-13].getAmount()].getAmount()==0) {                           if(positionreal+board[positionreal].getAmount()-13==0) {                               board[6].setAmount(board[12].getAmount());                               board[12].setAmount(0);                           }                             if(positionreal+board[positionreal].getAmount()-13==1) {                               board[6].setAmount(board[11].getAmount());                               board[11].setAmount(0);                           }                             if(positionreal+board[positionreal].getAmount()-13==2) {                               board[6].setAmount(board[10].getAmount());                               board[10].setAmount(0);                           } if(positionreal+board[positionreal].getAmount()-13==3) {                               board[6].setAmount(board[9].getAmount());                               board[9].setAmount(0);                           } if(positionreal+board[positionreal].getAmount()-13==4) {                               board[6].setAmount(board[9].getAmount());                               board[9].setAmount(0);                           } if(positionreal+board[positionreal].getAmount()-13==5) {                               board[6].setAmount(board[8].getAmount());                               board[8].setAmount(0);                           }                 }                             if (positionreal > 5 || positionreal < 0) {                     System.out.println("INVALID MOVE PLAYER 1");                     System.exit(0);                 }                 if (positionreal + board[positionreal].getAmount() == 6 || positionreal + board[positionreal].getAmount() == 20) {                     System.out.println("Player 1 go again");                     GoAgainPlayer1(true);                                 }                 for (int totally = 1; totally <= board[positionreal].getAmount(); totally++) {                     if (!(positionreal + board[positionreal].getAmount() == 6 || positionreal + board[positionreal].getAmount() == 20)) {                         GoAgainPlayer1(false);                         System.out.println("Why have i not gone here");                     System.out.print("  "+ hmm);                     }                     if (positionreal + totally == 14) {                         player1hasalot = true;                     }                     if (player1hasalot == true) {                                               board[temporary].setAmount(board[temporary].getAmount() + 1);                         temporary = temporary + 1;                     }                     if (player1hasalot == false) {                         board[positionreal + totally].setAmount(board[positionreal + totally].getAmount() + 1);                     }                 }                 temporary=0;                 board[positionreal].setAmount(0);                             drawBoard();                 System.out.println("Ok I am gettign tired of this" + hmm);               if(hmm==false) {                   counter++;               }                 break;             }         }         }else {             System.out.println("Alright player 2 it's your lovely turn");               if (counter % 2 != 0) {             while (counter % 2 != 0) {                 if (positionreal <6 || positionreal >12 || positionreal==6) {                     System.out.println("INVALID MOVE PLAYER 2");                     System.exit(0);                 }                 if (positionreal + board[positionreal].getAmount() == 13 || positionreal + board[positionreal].getAmount() == 27) {                     System.out.println("Player 2 go again");                     GoAgainPlayer2(true);                     System.out.println("IS IT WORKING!?!?" + hmm2);                 }                 for (int totally = 1; totally <= board[positionreal].getAmount(); totally++) {                     if (!(positionreal + board[positionreal].getAmount() == 13 || positionreal + board[positionreal].getAmount() == 30)) {                         System.out.println("I SHOULDNT HAVE GONE HERE");                         GoAgainPlayer2(false);                                         }                     if (positionreal + totally == 14) {                         player2hasalot = true;                     }                     if (player2hasalot == true) {                                                 board[temporary2].setAmount(board[temporary2].getAmount() + 1);                         temporary2 = temporary2 + 1;                     }                     if (player2hasalot == false) {                         board[positionreal + totally].setAmount(board[positionreal + totally].getAmount() + 1);                     }                 }                 temporary2=0;                 board[positionreal].setAmount(0);                               drawBoard();                 System.out.println("Ok I am gettign tired of this" + hmm2);                                     System.out.println("IS IT WORKING!?!?" + hmm2);               if(hmm2==false) {                   counter++;               }                 break;             }                   }     }     }     public void drawBoard() {         System.out.println("");         System.out.print(board[12].getAmount() + " ");         System.out.print(board[11].getAmount() + " ");         System.out.print(board[10].getAmount() + " ");         System.out.print(board[9].getAmount() + " ");         System.out.print(board[8].getAmount() + " ");         System.out.print(board[7].getAmount() + " ");         System.out.println("");         System.out.print("|" + board[13].getAmount() + "|");         System.out.print("      ");         System.out.print("|" + board[6].getAmount() + "|");         System.out.println("");         System.out.print(board[0].getAmount() + " ");         System.out.print(board[1].getAmount() + " ");         System.out.print(board[2].getAmount() + " ");         System.out.print(board[3].getAmount() + " ");         System.out.print(board[4].getAmount() + " ");         System.out.print(board[5].getAmount() + " ");         System.out.println("");     }     public boolean isOver() {       if(board[0].getAmount()==0 && board[1].getAmount()==0 && board[2].getAmount()==0 && board[3].getAmount()==0 && board[4].getAmount()==0 && board[5].getAmount()==0) {             return true;         }       if(board[7].getAmount()==0 && board[8].getAmount()==0 && board[9].getAmount()==0 && board[10].getAmount()==0 && board[11].getAmount()==0 && board[12].getAmount()==0) {               return true;           }               return false;           }         public boolean GoAgainPlayer1(boolean j) {         if (j == true) {             hmm = true;             return j;         } else if (j==false) {             hmm=false;         }         return hmm;     }     public boolean GoAgainPlayer2(boolean j) {         if (j == true) {             hmm2 = true;             return j;         } else if (j==false) {             hmm2=false;         }         return hmm2;     }     public boolean hmz2() {         return hmm2;     }     public boolean hmz() {         return hmm;     } }```
Each hole in the game board, Storage
Code:

```/*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */ /**  *  * @author administrator  */ public class Storage {     int howmany = 4;     public Storage(int number) {         howmany=number;     }     public int getAmount() {         return howmany;     }     public void setAmount(int amount) {         howmany=amount;     } }```

And The main:

Code:

```/*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */ /**  *  * @author administrator  */ import java.util.Scanner;  // imports just the Scanner class from java.util public class CalaMain {     /**     * @param args the command line arguments     */     public static void main(String[] args) {         int counter = 2;         Board b = new Board();         b.drawBoard();         while (b.isOver() == false) {             Scanner scan = new Scanner(System.in);             if (counter % 2 == 0) {                 System.out.println("Enter a position, player 1");                 String scanned = scan.next();                 b.movethem(scanned);                 while (b.hmz()==true) {                     System.out.println("Enter a position, again player 1");                     System.out.println("HE SHOULD BE GOING AGAIN");                     scanned = scan.next();                     b.movethem(scanned);                   // b.drawBoard();                                     }                 counter = counter + 1;                 System.out.println(b.hmz());             }             if (counter % 2 != 0) {                 System.out.println("Enter a position, player 2");                 String scanned = scan.next();                 b.movethem(scanned);                 while(b.hmz2()==true) {                     System.out.println("Enter a position, again player 2");                     scanned = scan.next();                     b.movethem(scanned);                 }                 counter = counter + 1;             }         }     } }```

I really don't know how to get started out to just populate the trees and stuff. I mean I know how it works, and I see examples online about nim, but mancala is just like different :?
• 01-01-2010, 01:56 AM
jigglywiggly