我意識到我需要我的 C 函式回傳 2 個值而不僅僅是一個值,那么以這種方式回傳硬編碼陣列是否正確?
int * funct(){
if(condition1){
return{1,1}
}else{
return{1,-1}
}
}
我需要這個回傳陣列結構來實作我的 minimax 演算法。這是背景關系的一些代碼,但這不是必需的(上面的示例應該足以傳達這個想法)。
//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};
}
}
}
uj5u.com熱心網友回復:
- C語言不能回傳自動存盤期陣列。
return {-1, boardScore(AI)};這個語法是錯誤的- 您可以使用
structs 作為傳遞 bt 值的結構 - 您可以使用復合文字
typedef struct
{
int data[2];
}mystruct;
mystruct funct(int condition)
{
if(condition)
{
return (mystruct){{1,-1}};
}else
{
(mystruct){{1, -1}};
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/533951.html
上一篇:C中的回溯迷宮,有障礙物
下一篇:遞回函式回傳有序向量
