AI部分源码如下
bool NoHit();//碰撞检测
bool NoBody(int a);//障碍检测
bool eat();//吃羊检测
int Alive();//还有几只羊
bool SheepAction();//羊的行动
bool hold(int key);//是否已经占据该关键点
bool CanBeEat(int key);//判断移动后是否会被吃
bool UP(int key);//向上找羊
bool DOWN(int key);//向下找羊
bool LEFT(int key);//向左找羊
bool RIGHT(int key);//向右找羊
bool SaveTop(int key);//移动前上面是否安全
bool SaveBelow(int key);//移动前下面是否安全
bool SaveLeft(int key);//移动前左面是否安全
bool SaveRight(int key);//移动前右面是否安全
bool UPSAVE(int key);//移动后上面是否安全
bool DOWNSAVE(int key);//移动后下面是否安全
bool LEFTSAVE(int key);//移动后左面是否安全
bool RIGHTSAVE(int key);//移动后右面是否安全
bool WolfUp(int wolf);//判断狼的上方是否被围
bool WolfDown(int wolf);//判断狼的下方是否被围
bool WolfLeft(int wolf);//判断狼的左方是否被围
bool WolfRight(int wolf);//判断狼的右方是否被围
int getPos(int key);//根据狼或羊的编号获得位置编号
bool WolfCanAction();//判断是否还有狼可以行动
// 羊的行动
bool SheepAction()
{
// 先判断是否有羊处于狼口之下 有的话就跑
for (int i = 0; i<15; i++)
{
if (!SaveTop(i))
{
p[i].y -= 102;
if (SaveTop(i) && SaveBelow(i) && SaveLeft(i) && SaveRight(i))
{
return true;
}
else
{
p[i].y += 102;
}
}
if (!SaveBelow(i))
{
p[i].y += 102;
if (SaveTop(i) && SaveBelow(i) && SaveLeft(i) && SaveRight(i))
{
return true;
}
else
{
p[i].y -= 102;
}
}
if (!SaveLeft(i))
{
p[i].x -= 102;
if (SaveTop(i) && SaveBelow(i) && SaveLeft(i) && SaveRight(i))
{
return true;
}
else
{
p[i].x += 102;
}
}
if (!SaveRight(i))
{
p[i].x += 102;
if (SaveTop(i) && SaveBelow(i) && SaveLeft(i) && SaveRight(i))
{
return true;
}
else
{
p[i].x -= 102;
}
}
}
//判断狼是否被围
for (int i = 15; i<18; i++)
{
if (!WolfUp(i))
{
if (UP(getPos(i)-5) || LEFT(getPos(i)-5) || RIGHT(getPos(i)-5))
{
return true;
}
}
if (!WolfDown(i))
{
if (DOWN(getPos(i)+5) || LEFT(getPos(i)+5) || RIGHT(getPos(i)+5))
{
return true;
}
}
if (!WolfLeft(i))
{
if (UP(getPos(i)-1) || DOWN(getPos(i)-1) || LEFT(getPos(i)-1))