C ++ Tic-Tac-Toe AI无法正常工作

时间:2017-12-02 11:09:30

标签: c++ tic-tac-toe

这是我第一次在这里提问。如果我在这里犯了任何错误并且让我知道要解决什么,请不要太生气。非常感谢你:D

我正在尝试编写一个C ++代码,您可以针对CPU播放Tic-Tac-Toe。 CPU必须赢或至少打成平手。 CPU先行。作为玩家,您将被要求输入2个数字,每个数字用于行和列。

问题在于,当我执行程序时,它会在第32行开始的for循环的第3个循环中没有任何错误消息而停止。我想知道我做错了什么。

    
#include <iostream>

using namespace std;

void displayBoard(char []);                   //displays current board
bool isValidInput(char [], int, int, int);    //checks the user input
bool isItAScore(char [], char);               //determines if current player won or not
int getValue(char [], char, int);             //get value of the board by player and number marks

int main()
{
    char tBoard[9], computer = 'X', player = 'O', empty = '.';
    int board[3][3], row = -1, col = -1, temp = 0;
    bool didSomeoneWin = false;

    //initializes the board with numbers 0 to 8
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            board[i][j] = temp;
            temp++;
        }
    }


    //initialize the actual board with '.'    
    for(int i= 0; i < 9; i++)
    {
        tBoard[i] = empty;
    }

    //starts the game
    for(int k = 0; k < 9; k++)    //it lasts only 9 turns max
    {
        displayBoard(tBoard);

        if(k % 2 == 1)    //player's turn
        {
            cout << "Player, row and column: ";
            cin >> row >> col;    //takes user input range of 1 to 3

            //decreases each value by 1 to work with the array index 0 ~ 2
            row--;
            col--;

            if(isValidInput(tBoard, row, col, board[row][col]))   //iff the input is valid
            {
                tBoard[board[row][col]] = player;    //puts the mark on the position

                if(k > 4 && isItAScore(tBoard, player))    //iff the current player wins
                {
                    displayBoard(tBoard);
                    cout << "Player wins.";
                    k = 8;    //skips to the end of the loop
                    didSomeoneWin = true;    //no tie
                }
            }
            else    //if the input is invalid
            {
                cout << "Invalid row or column number. Try again.\n";
                k--;    //redo the loop with same player
            }
        }
        else    //computer's turn
        {
            cout << "Computer's move\n";

            if(k == 0)    //first move
            {
                tBoard[5] = computer;
            }
            else
            {
                int value = -100, int position = -1;

                for(int i = 0; i < 9; i++)
                {
                    if(tBoard[i] == empty)
                    {
                        tBoard[i] = computer;

                        if(isItAScore(tBoard, computer))
                        {
                            displayBoard(tBoard);
                            cout << "Computer wins.";
                            i = 8;
                            k = 8;
                            didSomeoneWin = true;
                        }
                        else
                        {
                            int x1 = getValue(tBoard, computer, 1);
                            int x2 = getValue(tBoard, computer, 2);
                            int o1 = getValue(tBoard, player, 1);
                            int o2 = getValue(tBoard, player, 2);

                            if(value < 3 * x2 + x1 - (3 * o2 + o1))
                            {
                                value = 3 * x2 + x1 - (3 * o2 + o1);
                                position = i;
                            }
                        }

                        if(!didSomeoneWin)
                        {
                            tBoard[i] = empty;
                        }
                    }
                }

                tBoard[position] = computer;
            }      
        }
    }

    if(!didSomeoneWin)    //in case of tie
    {
        displayBoard(tBoard);
        cout << "The cat wins";
    }

    return 0;
}

//display the given board
void displayBoard(char brd[])
{
    for(int i = 0; i < 9; i++)
    {
        cout << brd[i] << " ";

        if((i + 1) % 3 == 0)
        {
            cout << endl;
        }
    }
}

//checks the input
bool isValidInput(char brd[], int i, int j, int k)
{
    if(((i >= 0 && i <= 2) && (j >= 0 && j <= 2)) && brd[k] == '.')     //number between 0 and 2, and not taken by any players
    {
        return true;
    }
    else
    {
        return false;
    }
}

//checks if the given player or computer won or not
bool isItAScore(char brd[], char c)
{
    //chekcs rows
    if((brd[0] == c && brd[1] == c && brd[2] == c) || (brd[3] == c && brd[4] == c && brd[5] == c) || (brd[6] == c && brd[7] == c && brd[8] == c))
    {
        return true;
    }
    else
    {
        //checks columns
        if((brd[0] == c && brd[3] == c && brd[6] == c) || (brd[1] == c && brd[4] == c && brd[7] == c) || (brd[2] == c && brd[5] == c && brd[8] == c))
        {
            return true;
        }
        else
        {
            //checks diagonals
            if((brd[0] == c && brd[4] == c && brd[8] == c) || (brd[2] == c && brd[4] == c && brd[6] == c))
            {
                return true;
            }
            //if none of them fails
            else
            {
                return false;
            }
        }
    }
}

int getValue(char brd[], char c, int n)
{
    int temp = 0, mark = 0;
    bool eligible = true;

    for(int i = 0; i < 9; i + 3)    //check rows
    {
        for(int j = i; j < i + 3; j++)
        {
            if(brd[j] != c)
            {
                j = 10;
                eligible = false;
            }
            else
            {
                if(brd[j] == c)
                {
                    temp++;
                }
            }
        }

        if(eligible && temp == n)
        {
            mark++;
        }

        eligible = true;
        temp = 0;
    }

    for(int i = 0; i < 3; i++)    //check columes
    {
        for(int j = i; j < i + 7; j + 3)
        {
            if(brd[j] != c)
            {
                j = 10;
                eligible = false;
            }
            else
            {
                if(brd[j] == c)
                {
                    temp++;
                }
            }
        }

        if(eligible && temp == n)
        {
            mark++;
        }

        eligible = true;
        temp = 0;
    }

    for(int i = 0; i < 9; i + 4)    //check '\' diagonal
    {
        if(brd[i] != c)
        {
            i = 10;
            eligible = false;
        }
        else
        {
            if(brd[i] == c)
            {
                temp++;
            }
        }
    }

    if(eligible && temp == n)
    {
        mark++;
    }

    eligible = true;
    temp = 0;

    for(int i = 2; i < 9; i + 2)    //check '/' diagonal
    {
        if(brd[i] != c)
        {
            i = 10;
            eligible = false;
        }
        else
        {
            if(brd[i] == c)
            {
                temp++;
            }
        }
    }

    if(eligible && temp == n)
    {
        mark++;
    }

    eligible = true;
    temp = 0;

    return mark;
}

我尝试使用名为minimax(公式)的东西。我希望它按预期正常工作。它应该与人类对抗并取得最好的结果(赢或平,不输)。

它会在没有任何错误消息的情况下停止。以下链接是我正在使用的在线编译器:

http://cpp.sh/

1 个答案:

答案 0 :(得分:0)

我发现了很多不寻常的东西。你第一次尝试使用cpp?代码中的第一个是错误:

<router-outlet>

这应该是

int value = -100, int position = -1;

int value = -100;
int position = -1;

计算机的第一步将令牌设置在中间的右侧。我想你想把它设置到中间位置

int value = -100, position = -1;

下一个问题,你有一个无限循环:

tBoard[5] = computer;// should be tBorad[4]

变量我永远不会改变,因为你只读它,而不是写它。这发生了5次。具有已启用警告的编译器将有助于查找此类错误。

接下来,使用中断继续而不是一些疯狂的循环语句。而不是这个,

for(int i = 0; i < 9; i + 3) // right would be i+=3

使用中断

   for(int i = 2; i < 9; i += 2)    //check '/' diagonal
   {
       if(brd[i] != c)
       {
           i = 10;
           eligible = false;
       }
   }

在所有这个错误消失后,该程序似乎正常工作。我认为你应该使用真正的编译器,比如gcc。使用gcc,您可以使用标志来查看警告。