[类中的c ++ int被设置为值,似乎无处不在

问题描述 投票:0回答:1

int winner应该在某些条件下设置为2,但是以某种方式将其设置为各种更高的值,最常见的是6。我不知道这是如何发生的,因为我的课程中没有其他函数会影响winner,并且在程序中的其他任何地方都没有提到该变量。最让我感到困惑的是,我有一个几乎相同的函数(P2Move()),其在将winner变量设置为P1Move()的方式上完全相同,并且该函数运行得很好。

一些信息:该类的一部分称为Board,它充当由Square类对象组成的棋盘阵列。

下面是导致问题的函数。在底部附近,语句else if((canTake.size()==0)&&(canMove.size()==0)) {Board::winner = 2;}引起了问题。当我从功能中删除有问题的部分时,其他所有内容似乎都可以正常工作,但是我需要该部分才能工作以提交最终项目。

void Board::P1Move()
{
    P1pieces = 0;
    std::vector <Move> canMove;
    std::vector <Move> canTake;

    for(int j = 0; j < bSize; j++)
    { //Start of j loop.
        for(int i = 0; i < bSize; i++)
        { //Start of i loop.
            Square sq = board[i][j];
            bool cTakeL = canTakeL(i,j);
            bool cTakeR = canTakeR(i,j);
            bool cMoveL = canMoveL(i,j);
            bool cMoveR = canMoveR(i,j);

            if(board[i][j].getPl() == P1)
            {
                P1pieces++;
                if(cTakeL)
                {
                    Move a = Move(sq.getIndex(),board[i-2][j+2].getIndex(),board[i-1][j+1].getIndex(),0);
                    canTake.push_back(a);
                }
                if(cTakeR)
                {
                    Move b = Move(sq.getIndex(),board[i+2][j+2].getIndex(),board[i+1][j+1].getIndex(),0);
                    canTake.push_back(b);
                }
                if(cMoveL)
                {
                    Move c = Move(sq.getIndex(),board[i-1][j+1].getIndex(),0,0);
                    canMove.push_back(c);
                }
                if(cMoveR)
                {
                    Move d = Move(sq.getIndex(),board[i+1][j+1].getIndex(),0,0);
                    setWinner(d.getSpos());
                    canMove.push_back(d);
                }
            }
        } //End of i loop.
    } //End of j loop.

    if(canTake.size()!=0)
    {
        time_t t;
        time(&t);
        srand(t);
        int moveNum = rand()%canTake.size();
        std::string output = "p1 ";
        Move out = canTake.at(moveNum);
        int i = 0;
        int j = 0;
        for(int y = 0; y < bSize; y++)
        {
            for(int x = 0; x < bSize; x++)
            {
                if(board[x][y].getIndex()==out.getSpos())
                {
                    i = x;
                    j = y;
                }
            }
        }
        if(board[i-2][j+2].getIndex()==out.getEndPos())
        {
            board[i-2][j+2].setOcc(true);
            board[i-2][j+2].setPl(P1);
            board[i-1][j+1].setOcc(false);
            board[i-1][j+1].setPl(NA);
        }
        else if(board[i+2][j+2].getIndex()==out.getEndPos())
        {
            board[i+2][j+2].setOcc(true);
            board[i+2][j+2].setPl(P1);
            board[i+1][j+1].setOcc(false);
            board[i+1][j+1].setPl(NA);
        }
        output = output + out.toString();
        setCmove(output);
        board[i][j].setOcc(false);
        board[i][j].setPl(NA);
    }
    else if(canMove.size()!=0)
    {
        time_t t;
        time(&t);
        srand(t);
        int moveNum = rand()%canMove.size();
        std::string output = "p1 ";
        Move out = canMove.at(moveNum);
        int i = 0;
        int j = 0;
        for(int y = 0; y < bSize; y++)
        {
            for(int x = 0; x < bSize; x++)
            {
                if(board[x][y].getIndex()==out.getSpos())
                {
                    i = x;
                    j = y;
                }
            }
        }
        if(board[i-1][j+1].getIndex()==out.getEndPos())
        {
            board[i-1][j+1].setOcc(true);
            board[i-1][j+1].setPl(P1);
        }
        else if(board[i+1][j+1].getIndex()==out.getEndPos())
        {
            board[i+1][j+1].setOcc(true);
            board[i+1][j+1].setPl(P1);
        }
        output = output + out.toString();
        setCmove(output);
        board[i][j].setOcc(false);
        board[i][j].setPl(NA);
    }
    else if((canTake.size()==0)&&(canMove.size()==0))
    {
        Board::winner = 2;
    }
    P1pieces = canTake.size() + canMove.size();
}

c++ function int
1个回答
0
投票

您正在使用std::vector,这是一件好事,并提供了一种非常简单的方法来找出您是否以及在何处可以进行越界访问(如注释中所建议)。

© www.soinside.com 2019 - 2024. All rights reserved.