我有一个 Qt 对象的 2d 数组,我已经用值播种了这些对象。如何访问数组中特定对象的值并更改它们?
I have an 2d array of Qt objects that I've seeded with values. how do I access the values of a particular object in the array and change them?
我的瓷砖.cpp文件是我尝试在rect[someint][someint]访问对象的地方,以便我可以使用我的ai来更改板上的棋子。现在,它是通过单击特定磁贴并传递该磁贴信息以验证移动是否正确来完成的。我遇到麻烦的是弄清楚如何更改磁贴,而无需通过单击访问它。
当我尝试更改 rect 的值时,例如 rect[someint][someint]->piece=0,我得到一个无效的类型未解析的重载函数类型 [int] 数组子矩形[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;
}
瓷砖的一小部分.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;
}
}
}
}
在 ai 转弯处,我完全陷入了试图弄清楚如何在磁贴对象数组中的坐标 y、x 处访问给定图块的地方。感谢您的任何帮助!
人工智能功能。目前没有做我需要它做的事情,即更改 Gui 板上的棋子。我提到的错误出现在@@@符号上。
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;
}
}
}
我在帖子中解释我的问题方面做得很差。 简单地说,我创建了一个名为 rect[8][8] 的 qt widget 对象的 extern 数组。这在我的 tiles.cpp 文件中被定义为 extern,在我的 main 文件中再次定义为 .cpp Tiles * rect[8][8]。我无法通过 rect[y][x]->某些属性访问它。但是使用 ::rect[y][x]-> 某些属性运行良好。
相关文章:
- 将对象数组的引用传递给函数
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 类对象数组的问题会导致崩溃
- Arduino C++在构造函数中用参数声明对象数组
- 使用对象数组对 SFML 进行动画处理
- C++ 对象数组堆栈溢出
- 如何将对象数组作为参数传递给模板
- 使用向量初始化参数化构造函数的对象数组
- 如何初始化对象数组?
- C++创建对象数组
- 在对象数组中搜索字符串并返回相应值的函数
- 有没有办法使用该类的构造函数初始化另一个类的私有部分内的对象数组?
- 如何在运行时在对象数组中动态追加新对象C++并打印它们
- 为什么我在声明对象数组时不能使用 -> 运算符?
- 编译时生成应在构造函数中创建的非 constexpr 对象数组
- 使用 C++ 创建对象数组
- 对象数组打印空白字符串
- 重载运算符 [] 用于从对象数组中给出特定索引
- 如何在 C++ 中使用提升属性树从 JSON 文件中读取对象数组