调用以下函数时,我不断收到错误消息。错误是在调用searchTree.m期间未分配输出参数bestboard
(可能还有其他参数)。
function [alpha,bestboard] = searchTree(board,depth,alpha,beta,turn)
if(depth<=0)
alpha=calculateHeuristic(board,changeTurn(turn));
bestboard=board;
return
else
child=GenerateChild(board,turn);
a=size(child,3);
if(turn==2)
turn=changeTurn(turn);
for i=1:a
[score,aboard]=searchTree(child(:,:,i),depth-1,alpha,beta,turn);
if(score>alpha)
alpha=score;
bestboard=child(:,:,i);
end
end
return
else
for j=1:a
turn=changeTurn(turn);
[scor,bboard]=searchTree(child(:,:,j),depth-1,alpha,beta,turn);
if(scor<beta)
beta=scor;
bestboard=child(:,:,j);
alpha=beta;
end
end
return
end
end
答案 0 :(得分:3)
这是因为bestboard
分配了if
而没有其他内容。在您的函数中,如果turn ~= 2
和scor >= beta
以及depth > 0
则不会分配bestboard
。考虑在一开始就给它分配一个默认值吗?