[英]Is it correct to return a hard-coded array needed for recursion in C?
I realized that I need my C function to return 2 values and not just one, so is it correct to return a hard-coded array in this way?我意识到我需要我的 C function 返回 2 个值而不仅仅是一个值,所以以这种方式返回硬编码数组是否正确?
int * funct(){
if(condition1){
return{1,1}
}else{
return{1,-1}
}
}
I need this return array structure for implementing my minimax algorithm.我需要这个返回数组结构来实现我的 minimax 算法。 Here's some code for context, but it's not necessary (the example above should be enough to deliver the idea).
这是上下文的一些代码,但这不是必需的(上面的示例应该足以传达这个想法)。
//Requires: board, depth & true if we're playing max player/ false if min player
//Effects: returns best score & col
int * minimax(int ** board, int depth, bool maxPlayer){
int newscore;
int * validLocations= getValidLocations(board);
bool gameOver= gameOver(board);
if (depth==0 || gameOver){
if (gameOver){
if(isWin(board, COMPUTER)){
return {-1, +10000};
}
else if(isWin(board,PLAYER)){
return {-1, -10000};
}
else{
return {-1, 0};; //tie
}
}
else{ //depth is 0
return {-1, boardScore(AI)};
}
}
if(maxPlayer){
int val= INT_MIN;
int bestCol= validLocations[0];
int validLocationsIndex=0;
int col= validLocations[0];
while (col!=-1 && validLocationsIndex<7){
int ** possibleBoard= copyBoard(board);
insert(possibleBoard, col, COMPUTER);
newscore= minimax(possibleBoard, depth-1, false)[1];
freeBoard(possibleBoard);
if (newscore>val){
val= newscore;
bestCol= col;
}
validLocationsIndex++;
col= validLocations[validLocationsIndex];
return {bestCol, newscore};
}
}
else{
int val= INT_MAX;
int validLocationsIndex=0;
int col= validLocations[0];
while (col!=-1 && validLocationsIndex<7){
int ** possibleBoard= copyBoard(board);
insert(possibleBoard, col, PLAYER);
newscore= minimax(possibleBoard, depth-1, true)[1];
freeBoard(possibleBoard);
if (newscore<val){
val= newscore;
bestCol= col;
}
validLocationsIndex++;
col= validLocations[validLocationsIndex];
return {bestCol, newscore};
}
}
}
return {-1, boardScore(AI)};
this syntax is wrongstruct
s as structs are passed bt valuestruct
s 作为结构传递 bt 值typedef struct
{
int data[2];
}mystruct;
mystruct funct(int condition)
{
if(condition)
{
return (mystruct){{1,-1}};
}else
{
(mystruct){{1, -1}};
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.