Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

java - Creating a Tic-Tac-Toe AI that will override a random selector if it sees a winning move

I have created a working tic-tac-toe engine. So far it supports player vs player, and player vs a very basic AI that selects completely random moves. I would like to add the ability for the AI to continue selecting randomly UNLESS it sees a move that will win the game. I.E. The AI moves randomly until it has 2 in a row with one spot that is empty, and at that point it will choose to play in the winning position.

Here is what I have so far :

for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (board[i][j] == ' ') {
                possibleMove[0] = i;
                possibleMove[1] = j;
            }
            if (board[i][j] == playerAI.moveType) {
                winCount++;
                if (winCount == 2) {
                    myMove = possibleMove;
                    winCount = 0;
                    System.out.println("row area " + Arrays.toString(myMove));
                }
            }
        }
        winCount = 0;
    }

My idea is to check every space on the board ^^ this one checks the rows. I will store any possible move the board finds, and then it will up a counter if we have a move in that row already. If the counter hits 2, then we should go to whatever our stored move is.

I have 4 more functions for columns, diagonals, and anti diagonals, that are the exact same principle.

Can someone please help me find where the flaw in my logic is?

question from:https://stackoverflow.com/questions/65545527/creating-a-tic-tac-toe-ai-that-will-override-a-random-selector-if-it-sees-a-winn

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Another solution is for each cell to check if its two neighbours are full and it is empty:

public class StackOverflow{
    static char[][] board = {{'x', 'x', ' '}, {'x', ' ', ' '}, {' ', ' ', ' '}};
    public static int[] checkRow(){
        for (int i = 0; i<3; i++){
            for (int j = 0; j<3; j++){
                if (board[i][(j+1)%3]=='x' && board[i][(j+1)%3]=='x' && board[i][j]==' '){
                    return new int[]{i, j};
                }
            }
        }
        return null;
    }
    public static void main(String[] args){
        System.out.println(checkRow()[0] + " "+checkRow()[1]);
    }
}

This will iterate through every cell, and check the ones 1 and 2 to the left, mod 3 so that you never check out of bounds. It returns null on a failed attempt. (which will throw because of the print statement, not because of the function itself)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...