This site requires JavaScript, please enable it in your browser!
Greenfoot back
JasonZhu
JasonZhu wrote ...

2014/5/12

Trouble with Tic Tac Toe AI

JasonZhu JasonZhu

2014/5/12

#
I've coded support methods for the computer's AI. I'm not getting the result I want from my code. I believe the problem lies in one of the support methods. I can't seem to figure out what though. Help on this matter is greatly appreciated. Thanks in advance! Here is my code for my computer AI:
   private void computerTurn()  // the main AI
    {
        int row = 1;
        int col = 1;
        int pos = 4;
        if(!board[row][col].isAvailable()){
            pos = checkForTwo(2);
            if(pos==-1){
                pos = checkForTwo(1);
                if(pos==-1){
                    pos = openCorner();
                    if(pos==-1){
                        pos = randomPlay();
                    }
                }
            }
        }
        row = pos/3;
        col = pos%3;
        board[row][col].computer();
    }

    private int checkForTwo(int player)  // checks for a line with two X or O and returns the position of the empty cell
    {        
        int pos = -1; int counter = 0;
        for(int row=0;row<3;row++){ // left to right rows
            counter = 0;
            pos = -1;
            for(int col=0;col<3;col++){
                if(board[row][col].getPlayer()==player){
                    counter++;
                }else{
                    if(board[row][col].isAvailable()){
                        pos = row*3 + col;
                    }else{
                        counter = 3;
                    }
                }
            }
            if(counter==2){
                return pos;
            }
        }

        for(int row=0;row<3;row++){ // top to bottom columns
            counter = 0;
            pos = -1;
            for(int col=0;col<3;col++){
                if(board[col][row].getPlayer()==player){
                    counter++;
                }else{
                    if(board[col][row].isAvailable()){
                        pos = row*3 + col;
                    }else{
                        counter = 3;
                    }
                }
            }
            if(counter==2){
                return pos;
            }
        }

        for(int row=0;row<3;row++){ // top left to bottom right
            counter = 0;
            pos = -1;
            if(board[row][row].getPlayer()==player){
                counter++;
            }else{
                if(board[row][row].isAvailable()){
                    pos = row*3 + row;
                }else{
                    counter = 3;
                }
            }
        }
        if(counter==2){
            return pos;
        }

        int col = 2;
        for(int row=0;row<3;row++){ // top right to bottom left
            counter = 0;
            pos = -1;
            if(board[row][col].getPlayer()==player){
                counter++;
            }else{
                if(board[row][col].isAvailable()){
                    pos = row*3 + col;
                }else{
                    counter = 3;
                }
            }
            col--;
        }
        if(counter==2){
            return pos;
        }
        return pos;
    }

    private int openCorner() // returns the position of a random open corner
    {
        boolean topLeft = board[0][0].isAvailable();
        boolean topRight = board[0][2].isAvailable();
        boolean bottomLeft = board[2][0].isAvailable();
        boolean bottomRight = board[2][2].isAvailable();

        ArrayList<Integer> pos = new ArrayList<Integer>();
        if(topLeft) pos.add(0);        
        if(topRight) pos.add(2);
        if(bottomLeft) pos.add(6);
        if(bottomRight) pos.add(8);

        Random gen = new Random();
        if(pos.size()==0){
            return -1;
        }else{
            return pos.get(gen.nextInt(pos.size()));
        }
    }

    private int randomPlay() // returns the position of a random open cell
    {
        int row,col;
        Random gen = new Random();
        do{
            row = gen.nextInt(3);
            col = gen.nextInt(3);
        }while(!board[row][col].isAvailable());
        return row*3+col;
    }
JasonZhu JasonZhu

2014/5/13

#
Been looking at it for a while now, still can't see the problem.
JasonZhu JasonZhu

2014/5/13

#
It's most likely in the checkForTwo(), the rest are seemingly functional.
You need to login to post a reply.