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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

int LookForWin(char board[],char player[],int play, bool fork=false)
{
int wincombos[8][3]={{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
vector<int>possiblemoves; // this is to store the possibilities if it needs to look for a fork.
for (int i=0; i<8; i++)
{
int pieces=0;
int emptysquares=0;
for (int j=0; j<3; j++)
{
if (board[wincombos[i][j]]==player[play])
{
pieces++;
}
else if (board[wincombos[i][j]]!='X' && board[wincombos[i][j]]!='O')
{
emptysquares++;
}
}
if (pieces==2 && emptysquares==1)
{
for (int j=0; j<3; j++)
{
if (board[wincombos[i][j]]!='X' && board[wincombos[i][j]]!='O')
{
return wincombos[i][j];
}
}
}
if (fork && pieces==1 && emptysquares==2)
{
// find a line with the same property
for (int k=i+1; k<8; k++)
{
pieces=0;
emptysquares=0;
for (int j=0; j<3; j++)
{
if (board[wincombos[k][j]]==player[play])
{
pieces++;
}
else if (board[wincombos[k][j]]!='X' && board[wincombos[k][j]]!='O')
{
emptysquares++;
}
}
if (pieces==1 && emptysquares==2)
{
// we have now found another such line, check if they intersect and if so, if the common square is empty
for (int j=0; j<3; j++)
{
for (int m=0; m<3; m++)
{
if (wincombos[i][j]==wincombos[k][m] && board[wincombos[i][j]]!='X' && board[wincombos[i][j]]!='O')
{
//cout << i << " " << j << endl;
possiblemoves.push_back(i);
possiblemoves.push_back(j);
//return wincombos[i][j];
}
}
}
}
}
}
}
cout << possiblemoves.size() << endl;
if (possiblemoves.size()==2)
{
// there is only 1 possible threat, return it
return wincombos[possiblemoves[0]][possiblemoves[1]];
}
else if (possiblemoves.size()>2)
{
//check if move would give it tempo by threatening a line
for (unsigned int j=0; j<possiblemoves.size(); j+=2) // declared as unsigned to remove warning
{
for (unsigned int m=0; m<8; m++)
{
bool linehaspiece=false;
bool linehasmove=false;
bool linehasempty=false;
for (unsigned int p=0; p<3; p++)
{
if(wincombos[m][p]==player[1])
linehaspiece=true;
else if(m==j && p==j+1)
linehasmove=true;
else if(wincombos[m][p]!=player[0])
linehasempty=true;
}
if (linehaspiece&&linehasempty&&linehasmove)
return wincombos[j][j+1];
}
}
}
possiblemoves.erase(possiblemoves.begin(),possiblemoves.end());
return 9;
}
 