Search code examples
javatic-tac-toe

Making my AI player in tic tac toe


I'm working on a project where im given a piece of code for 4x4 tic tac toe but have to implement my own AI in it that can beat the preinstalled AI. The 2 AI's im facing is simple and random. Random just randomly inserts X's on a square and Simple player starts from the top left square and iterates right 1 square. So to intercept simple player i have put my first O on the first row and basically do a vertical line down until there are 4 in a row. However, random player can intercept my line and then after that my computer player randomly places O's in empty squares to draw. However, this doesn't work properly since my player stops taking turn maybe because it doesn't know where to go. So i would appreciate if anyone could correct my concepts.

THIS IS ONLY A PART OF MY CODE

package noughtsAndCrossesV3;

import ncErrors.outOfRangeError;
import java.util.ArrayList;
import java.util.Random;

public class MyCompPlayer extends GenericPlayer implements NCPlayer {

    Random theGenerator;

    public MyCompPlayer()
    {
        super();        // no further initialisation required
        theGenerator = new Random();
    }

   // NCGrid is the grid the class that displays the grid and rules to win

    @Override
    public GridCoordinate getNextMove(NCGridV3 currentGrid) {
        int Row;
        int Col;
        GridCoordinate theSquare = null;
        int randomSelection;
        ArrayList<GridCoordinate> freeSquares = new ArrayList<GridCoordinate>(); // array finding free squares

        //iterates through row and column
        for (Row = 0; (theSquare == null) && (Row < currentGrid.getGridRowDimension()); Row++){
            for (Col = 0; (theSquare == null) && (Col < currentGrid.getGridColDimension()); Col++){


                try{

                    //If last column is empty, then draw a row of O's downwards in a straight line.

                    if(currentGrid.getSquareStatus(Row,3)==NCGridV3.SquareStatus.EMPTY){
                    theSquare = new GridCoordinate(Row,3);
                    return theSquare;
                }
                //If there is a nought then randomize movement. This doesnt work yet.
                else if(currentGrid.getSquareStatus(Row,3)==NCGridV3.SquareStatus.NOUGHT)
                    freeSquares.add(new GridCoordinate(Row, Col));
                // adds free sqaures to array and plots coordinate there but doesnt work.

                }

                catch (outOfRangeError e)
                {

                }

            }

        }

    randomSelection = theGenerator.nextInt(freeSquares.size());


    return freeSquares.get(randomSelection);
 }

}


Solution

  • At the end of the day there is always something the random AI might luckily stumble upon to ruin your day if you use a simple counter AI. I think the idea is to create an algorithm that minimizes the chance of that happening to negligible size. Maybe instead of just going down in a column you keep going in a different direction and always prioritize the direction you last took unless it is not possible.

    At the end of the day the game is deterministic and the amount of options isn't crazy like in chess, you could write an algorithm that wins all the time given enough run time. I suggest you take look at the minimax approach, it's the classic way to write your first AI for a game like chess, checkers or tic tac toe.