在“吃豆子”游戏中,计划将有3种不同类型的敌人。
· 松散型:在碰到墙壁后才会回头
· 守卫型:当敌人和主人公“大嘴”处在同一行或者是同一列后才会引起警觉接近大嘴。
· 扰乱性:不断的接近大嘴。
这三种敌人分别用以下的函数实现:
//AI人工智能处理
void RedOne::MakeDecision(bool b)
{
int i = rand();
if (b)//碰到墙壁,改变方向
{
//逆时针
if (i % 4 == 0)
{
tw = UP ? twCommand = LEFT : twCommand = UP;
}
else if (i % 3 == 0)
{
tw = DOWN ? twCommand = RIGHT: twCommand = DOWN;
}
else if (i % 2 == 0)
{
tw = RIGHT ? twCommand = UP : twCommand = RIGHT;
}
else
{
tw = LEFT ? twCommand = DOWN : twCommand = LEFT;
}
return;
}
}
void BlueOne::MakeDecision(bool b)
{
const int DR = this->dRow - player->GetRow();
const int DA = this->dArray - player->GetArray();
if (!b&&DR == 0)
{
if (DA <= BLUE_ALERT&&DA > 0)
{
twCommand = LEFT;
return;
}
if (DA < 0 && DA >= -BLUE_ALERT)
{
twCommand = RIGHT;
return;
}
}
if (!b&&DA == 0)
{
if (DR <= BLUE_ALERT&&DR > 0)
{
twCommand = UP;
return;
}
if (DR < 0 && DR >= -BLUE_ALERT)
{
twCommand = DOWN;
return;
}
}
RedOne::MakeDecision(b);
}
void YellowOne::MakeDecision(bool b)
{
const int DR = this->dRow - player->GetRow();
const int DA = this->dArray - player->GetArray();
if (!b)
{
if (DR*DR > DA*DA)
{
if (DA > 0)
{
twCommand = LEFT;
return;
}
else if (DA < 0)
{
twCommand = RIGHT;
return;
}
}
else
{
if (DR>0)
{
twCommand = UP;
return;
}
if (DR < 0)
{
twCommand = DOWN;
return;
}
}
}
RedOne::MakeDecision(b);
}
在Enermy类中,用一个函数来实现人工智能的方式:
void virtual MakeDecision(bool b) = 0; //AI实现
@ Mayuko