1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
int aivsai(char temp[1000][1000],int n,char curai,pair<int,int>&bestmove,int deep)
{
vector<pair<int,int>>emptypos;
findempty(temp,n,emptypos);
int bestscore,score;
if(curai==human)
{
bestscore=1000000000;
score=0;
}
else
{
bestscore=-1000000000;
score=0;
}
if(emptypos.size()==0)
return 0;
for(auto x:emptypos)
{
temp[x.first][x.second]=curai;
if(checkvictory(temp,n,x)==true)
{
if(curai==human)
return -1;
else
return 1;
}
if(curai==human)
{
score+=aivsai(temp,n,ai,bestmove,deep+1);
if(score<bestscore)
bestscore=score;
}
else
{
score+=aivsai(temp,n,human,bestmove,deep+1);
if(score>bestscore)
{
bestscore=score;
if(deep==0)
bestmove=x;
}
}
temp[x.first][x.second]='-';
}
return bestscore;
}
|