在tic Tac Toe的AI不随机移动

时间:2017-08-04 14:22:35

标签: android random

我将概念改为3x3 Tic Tac Toe为9x9。概念就像TicTacToe你必须赢得水平垂直或对角线3点只有区别是当我赢得一个特定的集合时,我已经将游戏划分为9集,该集合标记为该人的单个点,现在他/她必须赢得下一局赢得主赛。 现在我面临的问题是游戏工作正常AI正在生成随机数,直到人类或计算机赢得第一组,之后AI移动不是随机的,它们在一个序列中。

import java.util.Random;

public class TicTacToeGame {

private char mBoard[];
private final static int BOARD_SIZE = 81;

public static final char PLAYER_ONE = 'X';
public static final char PLAYER_TWO = '0';
public static final char EMPTY_SPACE = ' ';

private Random mRand;


public static int getBOARD_SIZE() {
    // Return the size of the board
    return BOARD_SIZE;
}

public TicTacToeGame(){

    mBoard = new char[BOARD_SIZE];

    for (int i = 0; i < BOARD_SIZE; i++)
        mBoard[i] = EMPTY_SPACE;

    mRand = new Random();
}

// Clear the board of all X's and O's
public void clearBoard()
{
    for (int i = 0; i < BOARD_SIZE; i++)
    {
        mBoard[i] = EMPTY_SPACE;
    }
}

// set the given player at the given location on the game board.
// the location must be available, or the board will not be changed.
public void setMove(char player, int location)
{
    mBoard[location] = player;
}

// Return the best move for the computer to make. You must call setMove()
// to actually make the computer move to that location.
public int getComputerMove()
{
    int move;

    // First see if there's a move O can make to win
    for (int i = 0; i < getBOARD_SIZE(); i++)
    {
        if (mBoard[i] != PLAYER_ONE && mBoard[i] != PLAYER_TWO)
        {
            char curr = mBoard[i];
            mBoard[i] = PLAYER_TWO;
            if (checkForWinner() == 3)
            {
                setMove(PLAYER_TWO, i);
                return i;
            }
            else
                mBoard[i] = curr;
        }
    }

    // See if there's a move O can make to block X from winning
    for (int i = 0; i < getBOARD_SIZE(); i++)
    {
        if (mBoard[i] != PLAYER_ONE && mBoard[i] != PLAYER_TWO)
        {
            char curr = mBoard[i];
            mBoard[i] = PLAYER_ONE;
            if (checkForWinner() == 2)
            {
                setMove(PLAYER_TWO, i);
                return i;
            }
            else
                mBoard[i] = curr;
        }
    }

    // Generate random move
    do
    {
        move = mRand.nextInt(getBOARD_SIZE());
    } while (mBoard[move] == PLAYER_ONE || mBoard[move] == PLAYER_TWO);

    setMove(PLAYER_TWO, move);
    return move;
}

// Check for a winner and return a status value indicating who has won.
// Return 0 if no winner or tie yet, 1 if it's a tie, 2 if X won, or 3
// if O won.
public int checkForWinner()
{
    //set0 win chk
    if ((mBoard[0] == PLAYER_ONE &&
            mBoard[1] == PLAYER_ONE &&
            mBoard[2] == PLAYER_ONE) ||
            (mBoard[9] == PLAYER_ONE &&
                    mBoard[10] == PLAYER_ONE &&
                    mBoard[11] == PLAYER_ONE) ||
            (mBoard[18] == PLAYER_ONE &&
                    mBoard[19] == PLAYER_ONE &&
                    mBoard[20] == PLAYER_ONE) ||
            (mBoard[0] == PLAYER_ONE &&
                    mBoard[9] == PLAYER_ONE &&
                    mBoard[18] == PLAYER_ONE) ||
            (mBoard[1] == PLAYER_ONE &&
                    mBoard[10] == PLAYER_ONE &&
                    mBoard[19] == PLAYER_ONE) ||
            (mBoard[2] == PLAYER_ONE &&
                    mBoard[11] == PLAYER_ONE &&
                    mBoard[20] == PLAYER_ONE) ||
            (mBoard[0] == PLAYER_ONE &&
                    mBoard[10] == PLAYER_ONE &&
                    mBoard[20] == PLAYER_ONE) ||
            (mBoard[2] == PLAYER_ONE &&
                    mBoard[10] == PLAYER_ONE &&
                    mBoard[18] == PLAYER_ONE))
        return 2;

    if ((mBoard[0] == PLAYER_TWO &&
            mBoard[1] == PLAYER_TWO &&
            mBoard[2] == PLAYER_TWO) ||
            (mBoard[9] == PLAYER_TWO &&
                    mBoard[10] == PLAYER_TWO &&
                    mBoard[11] == PLAYER_TWO) ||
            (mBoard[18] == PLAYER_TWO &&
                    mBoard[19] == PLAYER_TWO &&
                    mBoard[20] == PLAYER_TWO) ||
            (mBoard[0] == PLAYER_TWO &&
                    mBoard[9] == PLAYER_TWO &&
                    mBoard[18] == PLAYER_TWO) ||
            (mBoard[1] == PLAYER_TWO &&
                    mBoard[10] == PLAYER_TWO &&
                    mBoard[19] == PLAYER_TWO) ||
            (mBoard[2] == PLAYER_TWO &&
                    mBoard[11] == PLAYER_TWO &&
                    mBoard[20] == PLAYER_TWO) ||
            (mBoard[0] == PLAYER_TWO &&
                    mBoard[10] == PLAYER_TWO &&
                    mBoard[20] == PLAYER_TWO) ||
            (mBoard[2] == PLAYER_TWO &&
                    mBoard[10] == PLAYER_TWO &&
                    mBoard[18] == PLAYER_TWO))
        return 3;

    //set1 win chk
    if ((mBoard[3] == PLAYER_ONE &&
            mBoard[4] == PLAYER_ONE &&
            mBoard[5] == PLAYER_ONE) ||
            (mBoard[12] == PLAYER_ONE &&
                    mBoard[13] == PLAYER_ONE &&
                    mBoard[14] == PLAYER_ONE) ||
            (mBoard[21] == PLAYER_ONE &&
                    mBoard[22] == PLAYER_ONE &&
                    mBoard[23] == PLAYER_ONE) ||
            (mBoard[3] == PLAYER_ONE &&
                    mBoard[12] == PLAYER_ONE &&
                    mBoard[21] == PLAYER_ONE) ||
            (mBoard[4] == PLAYER_ONE &&
                    mBoard[13] == PLAYER_ONE &&
                    mBoard[22] == PLAYER_ONE) ||
            (mBoard[5] == PLAYER_ONE &&
                    mBoard[14] == PLAYER_ONE &&
                    mBoard[23] == PLAYER_ONE) ||
            (mBoard[3] == PLAYER_ONE &&
                    mBoard[13] == PLAYER_ONE &&
                    mBoard[23] == PLAYER_ONE) ||
            (mBoard[5] == PLAYER_ONE &&
                    mBoard[13] == PLAYER_ONE &&
                    mBoard[21] == PLAYER_ONE))
        return 2;

    if ((mBoard[3] == PLAYER_TWO &&
            mBoard[4] == PLAYER_TWO &&
            mBoard[5] == PLAYER_TWO) ||
            (mBoard[12] == PLAYER_TWO &&
                    mBoard[13] == PLAYER_TWO &&
                    mBoard[14] == PLAYER_TWO) ||
            (mBoard[21] == PLAYER_TWO &&
                    mBoard[22] == PLAYER_TWO &&
                    mBoard[23] == PLAYER_TWO) ||
            (mBoard[3] == PLAYER_TWO &&
                    mBoard[12] == PLAYER_TWO &&
                    mBoard[21] == PLAYER_TWO) ||
            (mBoard[4] == PLAYER_TWO &&
                    mBoard[13] == PLAYER_TWO &&
                    mBoard[22] == PLAYER_TWO) ||
            (mBoard[5] == PLAYER_TWO &&
                    mBoard[14] == PLAYER_TWO &&
                    mBoard[23] == PLAYER_TWO) ||
            (mBoard[3] == PLAYER_TWO &&
                    mBoard[13] == PLAYER_TWO &&
                    mBoard[23] == PLAYER_TWO) ||
            (mBoard[5] == PLAYER_TWO &&
                    mBoard[13] == PLAYER_TWO &&
                    mBoard[21] == PLAYER_TWO))
        return 3;

    //set2 win chk
    if ((mBoard[6] == PLAYER_ONE &&
            mBoard[7] == PLAYER_ONE &&
            mBoard[8] == PLAYER_ONE) ||
            (mBoard[15] == PLAYER_ONE &&
                    mBoard[16] == PLAYER_ONE &&
                    mBoard[17] == PLAYER_ONE) ||
            (mBoard[24] == PLAYER_ONE &&
                    mBoard[25] == PLAYER_ONE &&
                    mBoard[26] == PLAYER_ONE) ||
            (mBoard[6] == PLAYER_ONE &&
                    mBoard[15] == PLAYER_ONE &&
                    mBoard[24] == PLAYER_ONE) ||
            (mBoard[7] == PLAYER_ONE &&
                    mBoard[16] == PLAYER_ONE &&
                    mBoard[25] == PLAYER_ONE) ||
            (mBoard[8] == PLAYER_ONE &&
                    mBoard[17] == PLAYER_ONE &&
                    mBoard[26] == PLAYER_ONE) ||
            (mBoard[6] == PLAYER_ONE &&
                    mBoard[16] == PLAYER_ONE &&
                    mBoard[26] == PLAYER_ONE) ||
            (mBoard[8] == PLAYER_ONE &&
                    mBoard[16] == PLAYER_ONE &&
                    mBoard[24] == PLAYER_ONE))
        return 2;

    if ((mBoard[6] == PLAYER_TWO &&
            mBoard[7] == PLAYER_TWO &&
            mBoard[8] == PLAYER_TWO) ||
            (mBoard[15] == PLAYER_TWO &&
                    mBoard[16] == PLAYER_TWO &&
                    mBoard[17] == PLAYER_TWO) ||
            (mBoard[24] == PLAYER_TWO &&
                    mBoard[25] == PLAYER_TWO &&
                    mBoard[26] == PLAYER_TWO) ||
            (mBoard[6] == PLAYER_TWO &&
                    mBoard[15] == PLAYER_TWO &&
                    mBoard[24] == PLAYER_TWO) ||
            (mBoard[7] == PLAYER_TWO &&
                    mBoard[16] == PLAYER_TWO &&
                    mBoard[25] == PLAYER_TWO) ||
            (mBoard[8] == PLAYER_TWO &&
                    mBoard[17] == PLAYER_TWO &&
                    mBoard[26] == PLAYER_TWO) ||
            (mBoard[6] == PLAYER_TWO &&
                    mBoard[16] == PLAYER_TWO &&
                    mBoard[26] == PLAYER_TWO) ||
            (mBoard[8] == PLAYER_TWO &&
                    mBoard[16] == PLAYER_TWO &&
                    mBoard[24] == PLAYER_TWO))
        return 3;

    //set3 win chk
    if ((mBoard[27] == PLAYER_ONE &&
            mBoard[28] == PLAYER_ONE &&
            mBoard[29] == PLAYER_ONE) ||
            (mBoard[36] == PLAYER_ONE &&
                    mBoard[37] == PLAYER_ONE &&
                    mBoard[38] == PLAYER_ONE) ||
            (mBoard[45] == PLAYER_ONE &&
                    mBoard[46] == PLAYER_ONE &&
                    mBoard[47] == PLAYER_ONE) ||
            (mBoard[27] == PLAYER_ONE &&
                    mBoard[36] == PLAYER_ONE &&
                    mBoard[45] == PLAYER_ONE) ||
            (mBoard[28] == PLAYER_ONE &&
                    mBoard[37] == PLAYER_ONE &&
                    mBoard[46] == PLAYER_ONE) ||
            (mBoard[29] == PLAYER_ONE &&
                    mBoard[38] == PLAYER_ONE &&
                    mBoard[47] == PLAYER_ONE) ||
            (mBoard[27] == PLAYER_ONE &&
                    mBoard[37] == PLAYER_ONE &&
                    mBoard[47] == PLAYER_ONE) ||
            (mBoard[29] == PLAYER_ONE &&
                    mBoard[37] == PLAYER_ONE &&
                    mBoard[45] == PLAYER_ONE))
        return 2;

    if ((mBoard[27] == PLAYER_TWO &&
            mBoard[28] == PLAYER_TWO &&
            mBoard[29] == PLAYER_TWO) ||
            (mBoard[36] == PLAYER_TWO &&
                    mBoard[37] == PLAYER_TWO &&
                    mBoard[38] == PLAYER_TWO) ||
            (mBoard[45] == PLAYER_TWO &&
                    mBoard[46] == PLAYER_TWO &&
                    mBoard[47] == PLAYER_TWO) ||
            (mBoard[27] == PLAYER_TWO &&
                    mBoard[36] == PLAYER_TWO &&
                    mBoard[45] == PLAYER_TWO) ||
            (mBoard[28] == PLAYER_TWO &&
                    mBoard[37] == PLAYER_TWO &&
                    mBoard[46] == PLAYER_TWO) ||
            (mBoard[29] == PLAYER_TWO &&
                    mBoard[38] == PLAYER_TWO &&
                    mBoard[47] == PLAYER_TWO) ||
            (mBoard[27] == PLAYER_TWO &&
                    mBoard[37] == PLAYER_TWO &&
                    mBoard[47] == PLAYER_TWO) ||
            (mBoard[29] == PLAYER_TWO &&
                    mBoard[37] == PLAYER_TWO &&
                    mBoard[45] == PLAYER_TWO))
        return 3;

    //set4 win chk
    if ((mBoard[30] == PLAYER_ONE &&
            mBoard[31] == PLAYER_ONE &&
            mBoard[32] == PLAYER_ONE) ||
            (mBoard[39] == PLAYER_ONE &&
                    mBoard[40] == PLAYER_ONE &&
                    mBoard[41] == PLAYER_ONE) ||
            (mBoard[48] == PLAYER_ONE &&
                    mBoard[49] == PLAYER_ONE &&
                    mBoard[50] == PLAYER_ONE) ||
            (mBoard[30] == PLAYER_ONE &&
                    mBoard[39] == PLAYER_ONE &&
                    mBoard[48] == PLAYER_ONE) ||
            (mBoard[31] == PLAYER_ONE &&
                    mBoard[40] == PLAYER_ONE &&
                    mBoard[49] == PLAYER_ONE) ||
            (mBoard[32] == PLAYER_ONE &&
                    mBoard[41] == PLAYER_ONE &&
                    mBoard[50] == PLAYER_ONE) ||
            (mBoard[30] == PLAYER_ONE &&
                    mBoard[40] == PLAYER_ONE &&
                    mBoard[50] == PLAYER_ONE) ||
            (mBoard[32] == PLAYER_ONE &&
                    mBoard[40] == PLAYER_ONE &&
                    mBoard[48] == PLAYER_ONE))
        return 2;

    if ((mBoard[30] == PLAYER_TWO &&
            mBoard[31] == PLAYER_TWO &&
            mBoard[32] == PLAYER_TWO) ||
            (mBoard[39] == PLAYER_TWO &&
                    mBoard[40] == PLAYER_TWO &&
                    mBoard[41] == PLAYER_TWO) ||
            (mBoard[48] == PLAYER_TWO &&
                    mBoard[49] == PLAYER_TWO &&
                    mBoard[50] == PLAYER_TWO) ||
            (mBoard[30] == PLAYER_TWO &&
                    mBoard[39] == PLAYER_TWO &&
                    mBoard[48] == PLAYER_TWO) ||
            (mBoard[31] == PLAYER_TWO &&
                    mBoard[40] == PLAYER_TWO &&
                    mBoard[49] == PLAYER_TWO) ||
            (mBoard[32] == PLAYER_TWO &&
                    mBoard[41] == PLAYER_TWO &&
                    mBoard[50] == PLAYER_TWO) ||
            (mBoard[30] == PLAYER_TWO &&
                    mBoard[40] == PLAYER_TWO &&
                    mBoard[50] == PLAYER_TWO) ||
            (mBoard[32] == PLAYER_TWO &&
                    mBoard[40] == PLAYER_TWO &&
                    mBoard[48] == PLAYER_TWO))
        return 3;

    //set5 win chk
    if ((mBoard[33] == PLAYER_ONE &&
            mBoard[34] == PLAYER_ONE &&
            mBoard[35] == PLAYER_ONE) ||
            (mBoard[42] == PLAYER_ONE &&
                    mBoard[43] == PLAYER_ONE &&
                    mBoard[44] == PLAYER_ONE) ||
            (mBoard[51] == PLAYER_ONE &&
                    mBoard[52] == PLAYER_ONE &&
                    mBoard[53] == PLAYER_ONE) ||
            (mBoard[33] == PLAYER_ONE &&
                    mBoard[48] == PLAYER_ONE &&
                    mBoard[51] == PLAYER_ONE) ||
            (mBoard[34] == PLAYER_ONE &&
                    mBoard[43] == PLAYER_ONE &&
                    mBoard[52] == PLAYER_ONE) ||
            (mBoard[35] == PLAYER_ONE &&
                    mBoard[44] == PLAYER_ONE &&
                    mBoard[53] == PLAYER_ONE) ||
            (mBoard[33] == PLAYER_ONE &&
                    mBoard[43] == PLAYER_ONE &&
                    mBoard[53] == PLAYER_ONE) ||
            (mBoard[53] == PLAYER_ONE &&
                    mBoard[43] == PLAYER_ONE &&
                    mBoard[51] == PLAYER_ONE))
        return 2;

    if ((mBoard[33] == PLAYER_TWO &&
            mBoard[34] == PLAYER_TWO &&
            mBoard[35] == PLAYER_TWO) ||
            (mBoard[42] == PLAYER_TWO &&
                    mBoard[43] == PLAYER_TWO &&
                    mBoard[44] == PLAYER_TWO) ||
            (mBoard[51] == PLAYER_TWO &&
                    mBoard[52] == PLAYER_TWO &&
                    mBoard[53] == PLAYER_TWO) ||
            (mBoard[33] == PLAYER_TWO &&
                    mBoard[48] == PLAYER_TWO &&
                    mBoard[51] == PLAYER_TWO) ||
            (mBoard[34] == PLAYER_TWO &&
                    mBoard[43] == PLAYER_TWO &&
                    mBoard[52] == PLAYER_TWO) ||
            (mBoard[35] == PLAYER_TWO &&
                    mBoard[44] == PLAYER_TWO &&
                    mBoard[53] == PLAYER_TWO) ||
            (mBoard[33] == PLAYER_TWO &&
                    mBoard[43] == PLAYER_TWO &&
                    mBoard[53] == PLAYER_TWO) ||
            (mBoard[53] == PLAYER_TWO &&
                    mBoard[43] == PLAYER_TWO &&
                    mBoard[51] == PLAYER_TWO))
        return 3;

    //set6 win chk
    if ((mBoard[54] == PLAYER_ONE &&
            mBoard[55] == PLAYER_ONE &&
            mBoard[56] == PLAYER_ONE) ||
            (mBoard[63] == PLAYER_ONE &&
                    mBoard[64] == PLAYER_ONE &&
                    mBoard[65] == PLAYER_ONE) ||
            (mBoard[72] == PLAYER_ONE &&
                    mBoard[73] == PLAYER_ONE &&
                    mBoard[74] == PLAYER_ONE) ||
            (mBoard[54] == PLAYER_ONE &&
                    mBoard[63] == PLAYER_ONE &&
                    mBoard[72] == PLAYER_ONE) ||
            (mBoard[55] == PLAYER_ONE &&
                    mBoard[64] == PLAYER_ONE &&
                    mBoard[73] == PLAYER_ONE) ||
            (mBoard[56] == PLAYER_ONE &&
                    mBoard[65] == PLAYER_ONE &&
                    mBoard[74] == PLAYER_ONE) ||
            (mBoard[54] == PLAYER_ONE &&
                    mBoard[64] == PLAYER_ONE &&
                    mBoard[74] == PLAYER_ONE) ||
            (mBoard[56] == PLAYER_ONE &&
                    mBoard[64] == PLAYER_ONE &&
                    mBoard[72] == PLAYER_ONE))
        return 2;

    if ((mBoard[54] == PLAYER_TWO &&
            mBoard[55] == PLAYER_TWO &&
            mBoard[56] == PLAYER_TWO) ||
            (mBoard[63] == PLAYER_TWO &&
                    mBoard[64] == PLAYER_TWO &&
                    mBoard[65] == PLAYER_TWO) ||
            (mBoard[72] == PLAYER_TWO &&
                    mBoard[73] == PLAYER_TWO &&
                    mBoard[74] == PLAYER_TWO) ||
            (mBoard[54] == PLAYER_TWO &&
                    mBoard[63] == PLAYER_TWO &&
                    mBoard[72] == PLAYER_TWO) ||
            (mBoard[55] == PLAYER_TWO &&
                    mBoard[64] == PLAYER_TWO &&
                    mBoard[73] == PLAYER_TWO) ||
            (mBoard[56] == PLAYER_TWO &&
                    mBoard[65] == PLAYER_TWO &&
                    mBoard[74] == PLAYER_TWO) ||
            (mBoard[54] == PLAYER_TWO &&
                    mBoard[64] == PLAYER_TWO &&
                    mBoard[74] == PLAYER_TWO) ||
            (mBoard[56] == PLAYER_TWO &&
                    mBoard[64] == PLAYER_TWO &&
                    mBoard[72] == PLAYER_TWO))
        return 3;

    //set7 win chk
    if ((mBoard[57] == PLAYER_ONE &&
            mBoard[58] == PLAYER_ONE &&
            mBoard[59] == PLAYER_ONE) ||
            (mBoard[66] == PLAYER_ONE &&
                    mBoard[67] == PLAYER_ONE &&
                    mBoard[68] == PLAYER_ONE) ||
            (mBoard[75] == PLAYER_ONE &&
                    mBoard[76] == PLAYER_ONE &&
                    mBoard[77] == PLAYER_ONE) ||
            (mBoard[57] == PLAYER_ONE &&
                    mBoard[66] == PLAYER_ONE &&
                    mBoard[75] == PLAYER_ONE) ||
            (mBoard[58] == PLAYER_ONE &&
                    mBoard[67] == PLAYER_ONE &&
                    mBoard[76] == PLAYER_ONE) ||
            (mBoard[59] == PLAYER_ONE &&
                    mBoard[68] == PLAYER_ONE &&
                    mBoard[77] == PLAYER_ONE) ||
            (mBoard[57] == PLAYER_ONE &&
                    mBoard[67] == PLAYER_ONE &&
                    mBoard[77] == PLAYER_ONE) ||
            (mBoard[59] == PLAYER_ONE &&
                    mBoard[67] == PLAYER_ONE &&
                    mBoard[75] == PLAYER_ONE))
        return 2;

    if ((mBoard[57] == PLAYER_TWO &&
            mBoard[58] == PLAYER_TWO &&
            mBoard[59] == PLAYER_TWO) ||
            (mBoard[66] == PLAYER_TWO &&
                    mBoard[67] == PLAYER_TWO &&
                    mBoard[68] == PLAYER_TWO) ||
            (mBoard[75] == PLAYER_TWO &&
                    mBoard[76] == PLAYER_TWO &&
                    mBoard[77] == PLAYER_TWO) ||
            (mBoard[57] == PLAYER_TWO &&
                    mBoard[66] == PLAYER_TWO &&
                    mBoard[75] == PLAYER_TWO) ||
            (mBoard[58] == PLAYER_TWO &&
                    mBoard[67] == PLAYER_TWO &&
                    mBoard[76] == PLAYER_TWO) ||
            (mBoard[59] == PLAYER_TWO &&
                    mBoard[68] == PLAYER_TWO &&
                    mBoard[77] == PLAYER_TWO) ||
            (mBoard[57] == PLAYER_TWO &&
                    mBoard[67] == PLAYER_TWO &&
                    mBoard[77] == PLAYER_TWO) ||
            (mBoard[59] == PLAYER_TWO &&
                    mBoard[67] == PLAYER_TWO &&
                    mBoard[75] == PLAYER_TWO))
        return 3;

    //set8 win chk
    if ((mBoard[60] == PLAYER_ONE &&
            mBoard[61] == PLAYER_ONE &&
            mBoard[62] == PLAYER_ONE) ||
            (mBoard[69] == PLAYER_ONE &&
                    mBoard[70] == PLAYER_ONE &&
                    mBoard[71] == PLAYER_ONE) ||
            (mBoard[78] == PLAYER_ONE &&
                    mBoard[79] == PLAYER_ONE &&
                    mBoard[80] == PLAYER_ONE) ||
            (mBoard[60] == PLAYER_ONE &&
                    mBoard[69] == PLAYER_ONE &&
                    mBoard[78] == PLAYER_ONE) ||
            (mBoard[61] == PLAYER_ONE &&
                    mBoard[70] == PLAYER_ONE &&
                    mBoard[79] == PLAYER_ONE) ||
            (mBoard[62] == PLAYER_ONE &&
                    mBoard[71] == PLAYER_ONE &&
                    mBoard[80] == PLAYER_ONE) ||
            (mBoard[60] == PLAYER_ONE &&
                    mBoard[70] == PLAYER_ONE &&
                    mBoard[80] == PLAYER_ONE) ||
            (mBoard[62] == PLAYER_ONE &&
                    mBoard[70] == PLAYER_ONE &&
                    mBoard[78] == PLAYER_ONE))
        return 2;

    if ((mBoard[60] == PLAYER_TWO &&
            mBoard[61] == PLAYER_TWO &&
            mBoard[62] == PLAYER_TWO) ||
            (mBoard[69] == PLAYER_TWO &&
                    mBoard[70] == PLAYER_TWO &&
                    mBoard[71] == PLAYER_TWO) ||
            (mBoard[78] == PLAYER_TWO &&
                    mBoard[79] == PLAYER_TWO &&
                    mBoard[80] == PLAYER_TWO) ||
            (mBoard[60] == PLAYER_TWO &&
                    mBoard[69] == PLAYER_TWO &&
                    mBoard[78] == PLAYER_TWO) ||
            (mBoard[61] == PLAYER_TWO &&
                    mBoard[70] == PLAYER_TWO &&
                    mBoard[79] == PLAYER_TWO) ||
            (mBoard[62] == PLAYER_TWO &&
                    mBoard[71] == PLAYER_TWO &&
                    mBoard[80] == PLAYER_TWO) ||
            (mBoard[60] == PLAYER_TWO &&
                    mBoard[70] == PLAYER_TWO &&
                    mBoard[80] == PLAYER_TWO) ||
            (mBoard[62] == PLAYER_TWO &&
                    mBoard[70] == PLAYER_TWO &&
                    mBoard[78] == PLAYER_TWO))
        return 3;

    // Check for a tie
    for (int i = 0; i < getBOARD_SIZE(); i++)
    {
        // if we find a number, then no one has won yet
        if (mBoard[i] != PLAYER_ONE && mBoard[i] != PLAYER_TWO)
            return 0;
    }

    // If we make it through the previous loop, all places are taken, so it's a tie
    return 1;
}
}

附上图片以显示正在发生的事情

Showing the steps

MainActivity.java很大,可以放在这里工作正常,因为当我从TicTacToeGame.java文件中删除函数getComputerMove()中的以下代码时,该文件在上面共享,随机数正常。但是人工智能永远不会试图赢或阻止我们获胜。

 // First see if there's a move O can make to win
 for (int i = 0; i < getBOARD_SIZE(); i++)
 {
    if (mBoard[i] != PLAYER_ONE && mBoard[i] != PLAYER_TWO)
    {
        char curr = mBoard[i];
        mBoard[i] = PLAYER_TWO;
        if (checkForWinner() == 3)
        {
            setMove(PLAYER_TWO, i);
            return i;
        }
        else
            mBoard[i] = curr;
    }
}

// See if there's a move O can make to block X from winning
for (int i = 0; i < getBOARD_SIZE(); i++)
{
    if (mBoard[i] != PLAYER_ONE && mBoard[i] != PLAYER_TWO)
    {
        char curr = mBoard[i];
        mBoard[i] = PLAYER_ONE;
        if (checkForWinner() == 2)
        {
            setMove(PLAYER_TWO, i);
            return i;
        }
        else
            mBoard[i] = curr;
    }
}

1 个答案:

答案 0 :(得分:1)

我相信AI会因为你检查获胜动作而按顺序行动。

// First see if there's a move O can make to win
for (int i = 0; i < getBOARD_SIZE(); i++)
{
    if (mBoard[i] != PLAYER_ONE && mBoard[i] != PLAYER_TWO)
    {
        char curr = mBoard[i];
        mBoard[i] = PLAYER_TWO;
        if (checkForWinner() == 3)
        {
            setMove(PLAYER_TWO, i);
            return i;
        }
        else
            mBoard[i] = curr;
    }
}

Your AI saw this as a winning move 因此人工智能将继续按顺序进行这些水平移动,因为它们都算作获胜动作。

您可能需要考虑使用多个一维或二维数组来表示每个迷你Tic-Tac-Toe游戏