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?
int * funct(){
if(condition1){
return{1,1}
}else{
return{1,-1}
}
}
I need this return array structure for implementing my minimax algorithm. 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 valuetypedef struct
{
int data[2];
}mystruct;
mystruct funct(int condition)
{
if(condition)
{
return (mystruct){{1,-1}};
}else
{
(mystruct){{1, -1}};
}
}