I am trying to create a simple AI (for four in a line). I have looked online and found a few complicated AI's but they were to complex for me. I am trying to use recursion and maxDepth to create an AI to play two moves ahead.

public class AIMove {


    //holds a score, column pair. 
    int Score;
    int col;

    AIMove(int s,int c)
    {
        Score=s;
        col=c;
    }
    AIMove()
    {
        Score=col=0;
    }
    class Connect4
    {
        byte gameBoard[][]=new byte[7][6];
        //Circle[][] gameBoard = new Circle[7][6];
        //the connect 4 board. 0 means empty

        AIMove recursiveMinMax(byte placement, int depth, int maxDepth)
        {//Find the best move for the placement. Returns column and score pair 
            //depth specifies current level of depth (should be passed as 0)
            //maxDepth specifies the maximum depth to search

            AIMove goodness = new AIMove(-120000,0);


            if(depth%2==1)goodness.Score*=-1;
/*
 * 
 *  for i = 1 to 7
        if column i is not full
        {
            drop coin into column i
            value = -Goodness(-player,depth-1) / 2
            remove coin from column i
            if value > max
                max = value
        }
    return max
}
 */
            AIMove value=new AIMove();
            for (int i=0;i<7;i++) 
            { //try putting a piece at each of 7 columns.
                value.col=i;
                if(gameBoard[0][i]!=0)
                    continue; //if column is full ignore it.

                //find the row to put this piece. x is the row
                int x=0;

Any help or examples will be greatly appreciated. Thanks!

I am confused about your approach to the minmax method. Are you trying to evaluate from each slot or from each column? Also, how do you come up with scoring (line 31)? The way you score right now always gives higher score to any odd columns. That's also the problem when you try to evaluate each column instead of each slot because there is not much to evaluate plus it would be very inaccurate...

The evaluation should go from the current available slot in the given column (placement). Evaluate it with near by columns and its own. For example, give higher score to a column that could make a connection of 3 than any others. Give even higher score that can make 3 connection in multiple direction. Then lower the score for less connection and such. Don't forget to give the highest score if it can make 4 connection (win the game).

Lastly, you also need to check where the depth of the search is. Ensure that it won't go too deep.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.