我有一个Qd对象的数组,我已经用值来播种了。如何访问数组中特定对象的值并进行更改?

时间:2017-06-19 05:20:06

标签: c++ arrays qt

我的tiles.cpp文件是我正在尝试访问rect [someint] [someint]的对象,以便我可以使用我的ai来更改棋盘上的棋子。现在,通过点击特定的磁贴并传递该磁贴信息来验证移动是否正确。我正在做的事情是弄清楚如何更换瓷砖而无法通过点击访问它。

当我尝试更改rect的值如rect [someint] [someint] - > piece = 0时,我得到一个无效类型未解析的重载函数类型[int],用于数组subscipt rect [tempy] [tempx]错误。

我的一些主要文件主要功能:

Tile * rect[8][8];

void chessBoard(QWidget *baseWidget, Tile *rect[8][8]){

    for(int i = 0; i < 8; i++){
    boardX = 350;
    for(int j = 0; j < 8; j++){
        //seed tile array with tile objects
        rect[i][j] = new Tile(baseWidget);
        rect[i][j]->tileColor=(i+j)%2;
        rect[i][j]->piece=0;
        rect[i][j]->row=i;
        rect[i][j]->col=j;
        rect[i][j]->tileNum=k++;
        rect[i][j]->tileDisplay();
        rect[i][j]->setGeometry(boardX,boardY,64,64);

        //rect[i][j]->setScaledContents(true);

        boardX += 64;
    }
    boardY += 64;
}    

Tile.cpp的一小部分:

Pieces *isValid = new Pieces();
extern Tile * rect[8][8];
extern QWidget *myWidget;

Tile *click1;

void Tile::mousePressEvent(QMouseEvent * event){
    moveChecking(this, ++count);
}

Tile::moveChecking(Tile *temp, int countC){


if(countC==1){
    if(temp->piece && (temp->pieceColor==turns%2)){


        if(temp->pieceName != " "){
            tempx = temp->col;
            tempy = temp->row;
            click1 = new Tile();

            temp->setStyleSheet("QLabel {background-color: green;}");

            click1=temp;
        } else {
            count = 0;
        }
    } else {
        count = 0;
    }

} else {

    if(temp->tileNum==click1->tileNum){
        click1->tileDisplay();
        count = 0;

    } else {
        //ready coordinates to give to Pieces
        tempx = click1->col;
        tempy = click1->row;
        tempx2 = temp->col;
        tempy2 = temp->row;
        //give coordinates of piece origin and possible landing
        isValid->coordinates(tempx, tempy, tempx2, tempy2);

        //check if input coordinates are a valid move for piece and player
        if(isValid->whichPiece() == true){

            //switch Qwidget values on origin and spot piece landed
            click1->piece=0;
            temp->piece=1;

            //give moved piece same attributes
            temp->pieceColor=click1->pieceColor;
            temp->pieceName=click1->pieceName;


            click1->display(click1->pieceName);
            temp->display(click1->pieceName);

            click1->tileDisplay();
            temp->tileDisplay();



            turns++;
            count = 0;

            if(aiOn == 1){
                aiTurn(temp);
            }


        } else {

            count = 1;
        }

    }

  }

}

在转弯处,我完全陷入困境,试图弄清楚如何在瓷砖对象数组中的坐标y,x处访问给定的图块。感谢您的帮助!

Ai功能。在改变Gui板上的碎片的那一刻,没有做我需要的东西。我提到的错误出现在@@@ signs。

void Tile::aiTurn(Tile *temp){
    int aiIsThinking = 1;

    Ai_Logic *newMove = new Ai_Logic;

    while(aiIsThinking == 1){
        newMove->calculateBestMove();


        //ready coordinates to give to Pieces
        tempx = aiX;
        tempy = aiY;
        tempx2 = aiX1;
        tempy2 = aiY1;
        //give coordinates of piece origin and possible landing
        isValid->coordinates(tempx, tempy, tempx2, tempy2);

        //check if input coordinates are a valid move for piece and player
        if(isValid->whichPiece() == true){


            aiClick = new Tile();
            //aiClick = &rect[tempy][tempx];
            aiClick1 = new Tile();

            rect[tempx][tempy]->pieceName = "P"; //@@@

            //simulating ai clicking start piece
            aiClick->row = tempx;
            aiClick->col = tempy;
            //aiClick->pieceColor = rect[tempx][tempy].pieceColor;

            //simulating ai clicking landing spot
            aiClick1->row = tempx2;
            aiClick1->col = tempy2;

            //tile color corrections
            aiClick -> tileColor=(tempx+tempy)%2;
            aiClick1 -> tileColor=(tempx2+tempx2)%2;


            //switch Qwidget values on origin and spot piece landed
            aiClick->piece=0;
            aiClick->pieceName = " ";
            aiClick1->piece=1;

            //give moved piece same color at landing
            for(int k = 0; k < 8; k++){
                if(boardArr[tempy2][tempx2] == whitePieces[k]){
                    aiClick1->pieceColor=0;

                } else if (boardArr[tempy2][tempx2] == blackPieces[k]){
                    aiClick1->pieceColor=1;
                }
            }


           // aiClick1->pieceColor=aiClick->pieceColor;
            aiClick1->pieceName=boardArr[tempy2][tempx2];

            //display piece having moved
            aiClick->display(aiClick->pieceName);
            aiClick1->display(aiClick->pieceName);

            //make sure tile color is correct
            aiClick->tileDisplay();
            aiClick1->tileDisplay();

            turns++;
            count = 0;
            aiIsThinking = 0;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我在解释我的帖子中的问题方面表现不佳。简单地说,我创建了一个名为rect [8] [8]的外部qt小部件对象数组。这在我的tiles.cpp文件中定义为extern,并在我的main.cpp中定义为Tiles * rect [8] [8]。我无法通过rect [y] [x] - >某些属性访问它。但是使用:: rect [y] [x] - &gt;一些财产工作得很好。