更改方法C+中的结构变量+; 所以我正在为我的AI类制作一个C++类跳棋游戏。我遇到了一个让我发疯的小问题,每个对手的棋子都是一个包含方向的结构:布尔左;布尔右派;当我尝试将其中一个值更改为true时,它似乎没有改变。这似乎是一个范围问题,但我不知道为什么,我一直在使用vs调试工具。下面是一些代码: typedef struct { int pos; int num; bool left; bool right; }opp; int scoreOp(opp o){ if (scoreMoveOp(o.pos + 7) == 10){ o.left = true; return 10; } else if (scoreMoveOp(o.pos + 9) == 10){ o.right = true; return 10; } int scoreOp(opp o){ if (scoreMoveOp(o.pos + 7) == 10){ //scoreMoveOp essentially returns ten. o.left = true; return 10; } else if (scoreMoveOp(o.pos + 9) == 10){ o.right = true; return 10; }
这就是所谓的:更改方法C+中的结构变量+; 所以我正在为我的AI类制作一个C++类跳棋游戏。我遇到了一个让我发疯的小问题,每个对手的棋子都是一个包含方向的结构:布尔左;布尔右派;当我尝试将其中一个值更改为true时,它似乎没有改变。这似乎是一个范围问题,但我不知道为什么,我一直在使用vs调试工具。下面是一些代码: typedef struct { int pos; int num; bool left; bool right; }opp; int scoreOp(opp o){ if (scoreMoveOp(o.pos + 7) == 10){ o.left = true; return 10; } else if (scoreMoveOp(o.pos + 9) == 10){ o.right = true; return 10; } int scoreOp(opp o){ if (scoreMoveOp(o.pos + 7) == 10){ //scoreMoveOp essentially returns ten. o.left = true; return 10; } else if (scoreMoveOp(o.pos + 9) == 10){ o.right = true; return 10; },c++,artificial-intelligence,C++,Artificial Intelligence,这就是所谓的: void checkOPPListForX(){ for (int i = 0; i < O1Moves.size(); i++){ if (X == O1Moves[i].second){ //cout << "O1 has it, and its score is: " << scoreOp(O1) << endl; O1S
void checkOPPListForX(){
for (int i = 0; i < O1Moves.size(); i++){
if (X == O1Moves[i].second){
//cout << "O1 has it, and its score is: " << scoreOp(O1) << endl;
O1Score = scoreOp(O1);
O1Check = true;
}
else O1Check = false;
}
checkOPPListForX();
if (O1Score > O2Score && O1Score > O3Score && O1Score > O4Score){
//move O1;
//O1.pos
if (O1.left)
O1.pos = O1.pos + 7;
else if (O1.right)
O1.pos = O1.pos + 9;
}
void checkOPPListForX(){
对于(int i=0;i
您正在通过值将变量传递给scoreOp
函数,这意味着该函数将获得一个副本。修改副本当然不会修改原始副本
您需要改为通过引用传递参数:
最简单的方法是将函数放入结构中:
typedef struct {
int pos;
int num;
bool left;
bool right;
int scoreOp() {.....}
}opp;
然后你可以用opp.ScoreOp()来称呼它
这个解决方案是,如果您不太习惯通过引用传递,那么您可能正在处理副本,实际上(错误地)希望访问唯一的实例。
typedef struct {
int pos;
int num;
bool left;
bool right;
int scoreOp() {.....}
}opp;