围住神经猫--逃跑算法

思路:先在神经猫所在位置的六个逃跑方向寻找通路(左、左上、右上、左下、右下、右),再在通路里找到最短路径作为神经猫的逃跑方向,若六个方向都没有通路,则随机选择一个方向移动。

520f6ee090a9465fa4ac968c200ac5b9.png

 代码实现(注:需要在类中声明字段Hashtable escDir = new Hashtable(); //存放往各个方向逃跑的步数)

/// <summary>
/// 神经猫逃跑方向
/// </summary>
/// <param name="pot">格子信息</param>
/// <param name="x">神经猫所在格子对应二维数组下标的x值</param>
/// <param name="y">神经猫所在格子对应二维数组下标的y值</param>
/// <param name="direction">神经猫移动方向</param>
public void EscDir(Pot[,] pot, int x, int y, out EscapeDirection direction)
{
    int min = int.MaxValue; //逃跑的最少步数
    EscapeDirection tempDir = EscapeDirection.CATCH; //临时存储逃跑方向

    //朝左上方逃跑(局部方法)
    void LeftUp()
    {
        int stepDir = 0; //逃跑步数
        for (int i = x, j = y; i > 0 && j > 0;)
        {
            j = (i + 1) % 2 != 0 ? j : j - 1;
            i = i - 1;
            if (pot[i, j].PotStu == PotStatus.PASSABLE)
            {
                stepDir++;
            }
            else
            {
                //该逃跑方向上遍历到被阻塞的点,证明该方向没有通路,则把逃跑步数记录为int.MaxValue
                escDir.Add(EscapeDirection.TOPLEFT, int.MaxValue);
                return;
            }
        }
        escDir.Add(EscapeDirection.TOPLEFT, stepDir);
    }
    //朝右上方逃跑(局部方法)
    void RightUp()
    {
        int stepDir = 0; //逃跑步数
        for (int i = x, j = y; i > 0 && j < pot.GetLength(1) - 1;)
        {                
            j = (i + 1) % 2 != 0 ? j + 1 : j;
            i = i - 1;
            if (pot[i, j].PotStu == PotStatus.PASSABLE)
            {
                stepDir++;
            }
            else
            {
                escDir.Add(EscapeDirection.TOPRIGHT, int.MaxValue);
                return;
            }
        }
        escDir.Add(EscapeDirection.TOPRIGHT, stepDir);
    }
    //朝左下方逃跑(局部方法)
    void LeftDown()
    {
        int stepDir = 0; //逃跑步数
        for (int i = x, j = y; i < pot.GetLength(0) - 1 && j > 0;)
        {
            j = (i + 1) % 2 != 0 ? j : j - 1;
            i = i + 1;
            if (pot[i, j].PotStu == PotStatus.PASSABLE)
            {
                stepDir++;
            }
            else
            {
                escDir.Add(EscapeDirection.LEFTDOWN, int.MaxValue);
                return;
            }
        }
        escDir.Add(EscapeDirection.LEFTDOWN, stepDir);
    }
    //朝右下方逃跑(局部方法)
    void RightDown()
    {
        int stepDir = 0; //逃跑步数
        for (int i = x, j = y; i < pot.GetLength(0) - 1 && j < pot.GetLength(1) - 1;)
        {
            j = (i + 1) % 2 != 0 ? j + 1 : j;
            i = i + 1;
            if (pot[i, j].PotStu == PotStatus.PASSABLE)
            {
                stepDir++;
            }
            else
            {
                escDir.Add(EscapeDirection.RIGHTDOWN, int.MaxValue);
                return;
            }
        }
        escDir.Add(EscapeDirection.RIGHTDOWN, stepDir);
    }
    //朝左方逃跑(局部方法)
    void Left()
    {
        int stepDir = 0; //逃跑步数
        for (int i = x, j = y; j > 0;)
        {
            j = j - 1;
            if (pot[i, j].PotStu == PotStatus.PASSABLE)
            {
                stepDir++;
            }
            else
            {
                escDir.Add(EscapeDirection.LEFT, int.MaxValue);
                return;
            }
        }
        escDir.Add(EscapeDirection.LEFT, stepDir);
       }
    //朝右方逃跑(局部方法)
    void Right()
    {
        int stepDir = 0; //逃跑步数
        for (int i = x, j = y; j < pot.GetLength(1) - 1;)
        {
            j = j + 1;
            if (pot[i, j].PotStu == PotStatus.PASSABLE)
            {
                stepDir++;
            }
            else
            {
                escDir.Add(EscapeDirection.RIGHT, int.MaxValue);
                return;
            }
        }
        escDir.Add(EscapeDirection.RIGHT, stepDir);
    }

    LeftDown();
    Left();
    LeftUp();
    RightUp();
    Right();
    RightDown();

    //遍历哈希表,找到逃跑的最少步数
    foreach (DictionaryEntry entry in escDir)
    {
        if ((int)entry.Value < min)
        {
            min = (int)entry.Value;
            tempDir = (EscapeDirection)entry.Key;
        }
    }
    direction = tempDir;
    escDir.Clear();
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值