Results 1 to 3 of 3
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default Minimax ai, oh lordy

    So basically, my professor, just told us to make Mancala with AI, and not really much further instruction other than: Use minimax, and use these 2 classes:

    Java 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;
    
    }
    and:


    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author administrator
     */
    public class ManCalaTree {
    ManCalaTreeNode root;
    }


    I am on my netbook, and I totally forget to update the code from my computer, but just assume my code for 2 players works:


    Java 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();
    
    
            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);
                   counter=counter+1;
                }
    
                if (counter % 2 != 0) {
    
                    System.out.println("Enter a position, player 2");
    
                    String scanned = scan.next();
    
    
    
                    b.movethem(scanned);
                    counter=counter+1;
                }
    
    
    
            }
    
        }
    }
    Java 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:

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author administrator
     */
    public class Board {
    
        Storage[] board = new Storage[12];
        Storage b = new Storage(4);
        int positionreal = 0;
        int left = 0;
        int right = 0;
        int counter = 2;
        int counter2 = 0;
    
        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(4);
            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);
    
    
        }
    
        public void movethem(String l) {
    
            positionreal = Integer.parseInt(l);
    
            if (counter % 2 == 0) {
    
    
    
                while (counter % 2 == 0) {
    
    
                    if (positionreal > 6 && positionreal >= 0) {
                        System.out.println("INVALID MOVE PLAYER 1");
                        System.exit(0);
                    }
                    if (positionreal + board[positionreal].getAmount() == 6) {
                        System.out.println("Player 1 go again");
    
                        for (int totally = positionreal; totally <= board[positionreal].getAmount(); totally++) {
                            if (counter2 < 5 - positionreal && counter2 >= 0) {
                                counter2 = counter2 + 1;
    
                            }
    
                            board[positionreal + counter2].setAmount(board[positionreal + counter2].getAmount() + 1);
                        }
                        right = right + 1;
                        board[positionreal].setAmount(0);
                        drawBoard();
                        System.exit(0);
    
                    }
    
    
                    if (!(positionreal + board[positionreal].getAmount() == 6)) {
                        
                        for (int vrz = 0; vrz <= board[positionreal].getAmount(); vrz++) {
    
    
                            if (positionreal + board[positionreal].getAmount() >= 5 && board[positionreal].getAmount() - vrz > 0) {
    
                                right = right + 1;
                               
                                board[positionreal+vrz].setAmount(board[positionreal+vrz].getAmount() + 1);
                                System.out.println("I GOT HERE");
    
                            } else {
                              
                                board[positionreal + vrz].setAmount(board[positionreal + vrz].getAmount() + 1);
                                System.out.println("I GOTS HERE");
                            }
    
    
    
                            
                        }
                         board[positionreal].setAmount(0);
                         counter = counter + 1;
    
    
                        
                        drawBoard();
                    }
    
    
    
                }
    
    
    
            } else {
                if (positionreal == 11 && board[positionreal].getAmount() == 1) {
                    left = left + 1;
                    board[positionreal].setAmount(0);
    
    
                    System.out.println("Player 2 go again");
    
    
                }
                while (counter % 2 != 0) {
                    positionreal = Integer.parseInt(l);
                    if (positionreal < 6 && positionreal <= 11) {
                        System.out.println("INVALID MOVE PLAYER 2");
                        System.exit(0);
                    }
    
    
    
    
                    for (int i = 0; i < board[positionreal].getAmount(); i++) {
    
    
                        if (positionreal + i == 11 && board[positionreal].getAmount() - i > 0) {
                            i = i + 1;
                            left = left + 1;
                            positionreal = 0;
    
                            board[positionreal + i].setAmount(board[positionreal].getAmount() + 1);
    
    
    
    
                        }
                    }
                }
                System.out.println(right);
                board[positionreal].setAmount(0);
                counter = counter + 1;
            }
        }
    
        public void drawBoard() {
    
            System.out.println("");
            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.print(board[6].getAmount() + " ");
            System.out.println("");
            System.out.print("|" + left + "|");
            System.out.print("      ");
            System.out.print("|" + right + "|");
            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() {
            return false;
        }
    }

    Now I am obviously not asking anyone to do this, I just want advice on how to go about doing this, since I literally got no instruction.

    (This is my first ai ever)

  2. #2
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Help please?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jigglywiggly View Post
    Help please?
    Do you know how a minimax evaluation works? Basically it boils down to a small, nifty, recursive method. There are quite some links available, here's wikipedia's explanation.

    kind regards,

    Jos

Similar Threads

  1. Creating the evaluation function for Minimax
    By matzahboy in forum New To Java
    Replies: 7
    Last Post: 11-05-2009, 03:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •