#include "computerPieces.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
HANDLE handle2;
COORD coord2;
void ComputerPieces::fallsChess(int *x,int *y,int map[][17]) //电脑走棋
{
int num;
int i;
srand((unsigned int)time(0));
for (i=0;i<15;i++)
{
for (int j=0;j<15;j++)
{
if (map[i][j]==2) //要赢啦(己方4子)
{
if (map[i][j+1]==2&&map[i][j+2]==2&&map[i][j+3]==2)
{
if (map[i][j+4]==0)
{
*x=i;
*y=j+4;
return;
}
else if (j-1>=0)
{
if (map[i][j-1]==0)
{
*x=i;
*y=j-1;
return;
}
}
}
else if (map[i+1][j]==2&&map[i+2][j]==2&&map[i+3][j]==2)
{
if (map[i+4][j]==0)
{
*x=i+4;
*y=j;
return;
}
else if (i-1>=0)
{
if (map[i-1][j]==0)
{
*x=i-1;
*y=j;
return;
}
}
}
else if (map[i+1][j+1]==2&&map[i+2][j+2]==2&&map[i+3][j+3]==2)
{
if (map[i+4][j+4]==0)
{
*x=i+4;
*y=j+4;
return;
}
else if (i-1>=0&&j-1>=0)
{
if (map[i-1][j-1]==0)
{
*x=i-1;
*y=j-1;
return;
}
}
}
else if (map[i-1][j+1]==2&&map[i-2][j+2]==2&&map[i-3][j+3]==2)
{
if (i-4>=0)
{
if (map[i-4][j+4]==0)
{
*x=i-4;
*y=j+4;
return;
}
}
else if (j-1>=0)
{
if (map[i+1][j-1]==0)
{
*x=i+1;
*y=j-1;
return;
}
}
}
}
}
}
for (i=0;i<15;i++)
{
for (int j=0;j<15;j++)
{
if (map[i][j]==1) //必须守下(敌方4子)
{
if (map[i][j+1]==1&&map[i][j+2]==1&&map[i][j+3]==1)
{
if (map[i][j+4]==0)
{
*x=i;
*y=j+4;
return;
}
else if (j-1>=0)
{
if (map[i][j-1]==0)
{
*x=i;
*y=j-1;
return;
}
}
}
else if (map[i+1][j]==1&&map[i+2][j]==1&&map[i+3][j]==1)
{
if (map[i+4][j]==0)
{
*x=i+4;
*y=j;
return;
}
else if (i-1>=0)
{
if (map[i-1][j]==0)
{
*x=i-1;
*y=j;
return;
}
}
}
else if (map[i+1][j+1]==1&&map[i+2][j+2]==1&&map[i+3][j+3]==1)
{
if (map[i+4][j+4]==0)
{
*x=i+4;
*y=j+4;
return;
}
else if (j>1&&i-1>=0)
{
if (map[i-1][j-1]==0)
{
*x=i-1;
*y=j-1;
return;
}
}
}
else if (map[i-1][j+1]==1&&map[i-2][j+2]==1&&map[i-3][j+3]==1)
{
if (map[i-4][j+4]==0&&j-4>=0)
{
*x=i-4;
*y=j+4;
return;
}
else if (j-1>=0)
{
if (map[i+1][j-1]==0)
{
*x=i+1;
*y=j-1;
return;
}
}
}
}
}
}
for (i=0;i<15;i++)
{
for (int j=0;j<15;j++)
{
if (map[i][j]==2) //弄死你(己方3子无阻拦)
{
if (map[i][j+1]==2&&map[i][j+2]==2&&map[i][j-1]==0&&map[i][j+3]==0)
{
num=rand()%1;
while (true)
{
if (num==0)
{
*x=i;
*y=j+3;
return;
}
else if (num==1)
{
if (j-1<0)
{
num=0;
continue;
}
*x=i;
*y=j-1;
return;
}
}
}
else if (map[i+1][j]==2&&map[i+2][j]==2&&map[i+3][j]==0&&map[i-1][j]==0)
{
num=rand()%1;
while (true)
{
if (num==0)
{
*x=i+3;
*y=j;
return;
}
else if (num==1)
{
if (i-1<0)
{
num=0;
continue;
}
*x=i-1;
*y=j;
return;
}
}
}
else if (map[i+1][j+1]==2&&map[i+2][j+2]==2&&map[i+3][j+3]==0&&map[i-1][j-1]==0)
{
num=rand()%1;
while (true)
{
if (num==0)
{
*x=i+3;
*y=j+3;
return;
}
else if (num==1)
{
if (j-1<0||i-1<0)
{
num=0;
continue;
}
*x=i-1;
*y=j-1;
return;
}
}
}
else if (map[i-1][j+1]==2&&map[i-2][j+2]==2&&map[i-3][j+3]==0&&map[i+1][j-1]==0)
{
num=rand()%1;
while (true)
{
if (num==0)
{
if (i-3<0)
{
if (j-1<0)
{
break;
}
num=1;
continue;
}
*x=i-3;
*y=j+3;
return;
}
else if (num==1)
{
if (j-1<0)
{
if (i-3<0)
{
break;
}
num=0;
continue;
}
*x=i+1;
*y=j-1;
return;
}
}
}
}
}
}
for (i=0;i<15;i++)
{
for (int j=0;j<15;j++)
{
if (map[i][j]==1) //危险啦(敌方3子无阻拦)
{
if (map[i][j+1]==1&&map[i][j+2]==1&&map[i][j-1]==0&&map[i][j+3]==0)
{
num=rand()%1;
while (true)
{
if (num==0)
{
*x=i;
*y=j+3;
return;
}
else if (num==1)
{
if (j-1<0)
{
num=0;
continue;
}
*x=i;
*y=j-1;
return;
}
}
}
else if (map[i+1][j]==1&&map[i+2][j]==1&&map[i+3][j]==0&&map[i-1][j]==0)
{
num=rand()%1;
while (true)
{
if (num==0)
{
*x=i+3;
*y=j;
return;
}
else if (num==1)
{
if (i-1<0)
{
num=0;
continue;
}
*x=i-1;
*y=j;
return;
}
}
}
else if (map[i+1][j+1]==1&&map[i+2][j+2]==1&&map[i+3][j+3]==0&&map[i-1][j-1]==0)
{
num=rand()%1;
while (true)
{
if (num==0)
{
*x=i+3;
*y=j+3;
return;
}
else if (num==1)
{
if (j-1<0||i-1<0)
{
num=0;
continue;
}
*x=i-1;
*y=j-1;
return;
}
}
}
else if (map[i-1][j+1]==1&&map[i-2][j+2]==1&&map[i-3][j+3]==0&&map[i+1][j-1]==0)
{
num=rand()%1;
while (true)
{
if (num==0)
{
if (i-3<0)
{
if (j-1<0)
{
break;
}
num=1;
continue;
}
*x=i-3;
*y=j+3;
return;
}
else if (num==1)
{
if (j-1<0)
{
if (i-3<0)
{
break;
}
num=0;
continue;
}
*x=i+1;
*y=j-1;
return;
}
}
}
}
}
}
for (i=0;i<15;i++)
{
for (int j=0;j<15;j++)
{
if (map[i][j]==2) //让你走哪就走哪(己方3子有阻拦)
{
if (map[i][j+1]==2&&map[i][j+2]==2&&(map[i][j-1]==0||map[i][j+3]==0))
{
if(map[i][j-1]==0)
{
*x=i;
*y=j-1;
return;
}
else if (map[i][j+3]==0)
{
*x=i;
*y=j+3;
return;
}
}
else if (map[i+1][j]==2&&map[i+2][j]==2&&(map[i+3][j]==0||map[i-1][j]==0))
{
if(map[i+3][j]==0)
{
*x=i+3;
*y=j;
return;
}
else if (map[i-1][j]==0)
{
*x=i-1;
*y=j;
return;
}
}
else if (map[i+1][j+1]==2&&map[i+2][j+2]==2&&(map[i+3][j+3]==0||map[i-1][j-1]==0))
{
if (map[i+3][j+3]==0)
{
*x=i+3;
*y=j+3;
return;
}
else if (map[i-1][j-1]==0)
{
*x=i-1;
*y=j-1;
return;
}
}
else if (map[i-1][j+1]==2&&map[i-2][j+2]==2&&(map[i-3][j+3]==0||map[i+1][j-1]==0))
{
if (map[i-3][j+3]==0)
{
*x=i-3;
*y=j+3;
return;
}
else if