写的一个寻路算法(•̀ᴗ•́)

using System.Collections;
using UnityEngine;
using System.Collections.Generic;
using UnityEditor.Animations;

public class Test : MonoBehaviour
{
    public IEnumerator Start()
    {
        cube = GameObject.Find("Cube");
        CreateMaze();
        oriPos = transform.position;
        stack.Clear();
        stack.Push(oriPos);
        stack.Push(oriPos);
        stack.Push(oriPos);
        stack.Push(oriPos);
        bool isArrive = false;//是否找到出口
        cube.GetComponent<MeshRenderer>().material.color = Color.yellow;
        while (!isArrive)
        {
            yield return new WaitForSeconds(0.2f);
            if (BreakMaze() == 2)
            {
                cube.GetComponent<MeshRenderer>().material.color = Color.red;
                isArrive = true;
            }
        }
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
            BreakMaze();
               }

    List<List<int>> maze = new List<List<int>>();
    private GameObject cube;
    private Stack<Vector3> stack = new Stack<Vector3>();
    private Vector3 oriPos;
    private void CreateMaze()
    {
        // 迷宫定义
        maze = new List<List<int>>
        {
        new List<int>    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1 } ,
        new List<int>    { 1, 1, 0, 0, 0, 0, 1, 1, 0, 1 ,1, 0, 0, 0, 0, 1, 0, 1} ,
        new List<int>    { 1, 0, 0, 1, 1, 1, 0, 0, 0, 1 ,0, 0, 1, 1, 1, 0, 0, 1} ,
        new List<int>    { 1, 1, 0, 0, 0, 1, 0, 1, 0, 1 ,1, 0, 0, 0, 1, 0, 1, 1} ,
        new List<int>    { 1, 0, 1, 1, 0, 1, 0, 0, 1, 1 ,0, 1, 1, 0, 1, 0, 0, 1} ,
        new List<int>    { 1, 0, 0, 0, 0, 1, 1, 0, 0, 1 ,0, 0, 0, 0, 1, 1, 0, 1} ,
        new List<int>    { 1, 0, 1, 1, 0, 0, 0, 0, 1, 1 ,0, 1, 1, 0, 0, 0, 0, 1} ,
        new List<int>    { 1, 1, 0, 0, 0, 0, 1, 1, 1, 1 ,1, 0, 0, 0, 0, 1, 0, 1} ,
        new List<int>    { 1, 0, 0, 1, 1, 1, 0, 0, 1, 1 ,0, 0, 1, 1, 1, 0, 0, 1} ,
        new List<int>    { 1, 1, 0, 0, 0, 1, 0, 1, 1, 1 ,1, 0, 0, 0, 1, 0, 1, 1} ,
        new List<int>    { 1, 0, 1, 1, 0, 1, 0, 0, 1, 1 ,0, 1, 1, 0, 1, 0, 0, 1} ,
        new List<int>    { 1, 0, 0, 0, 0, 1, 1, 0, 1, 1 ,0, 0, 0, 0, 1, 1, 0, 1} ,
        new List<int>    { 1, 0, 1, 0, 0, 0, 0, 0, 1, 1 ,0, 1, 1, 0, 0, 0, 0, 1} ,
        new List<int>    { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 ,1, 1, 1, 1, 0, 1, 1, 1} ,
        new List<int>    { 1, 1, 0, 0, 0, 0, 1, 1, 1, 1 ,1, 0, 0, 0, 0, 1, 1, 1} ,
        new List<int>    { 1, 0, 0, 1, 1, 1, 0, 0, 1, 1 ,0, 0, 1, 1, 1, 0, 0, 1} ,
        new List<int>    { 1, 1, 0, 0, 0, 1, 0, 1, 1, 0 ,1, 0, 0, 0, 1, 0, 1, 1} ,
        new List<int>    { 1, 0, 1, 1, 0, 1, 0, 0, 1, 1 ,0, 1, 1, 0, 1, 0, 0, 1} ,
        new List<int>    { 1, 0, 0, 0, 0, 1, 1, 0, 1, 2 ,0, 0, 0, 0, 1, 1, 0, 1} ,
        new List<int>    { 1, 0, 1, 1, 0, 0, 0, 0, 1, 1 ,0, 1, 1, 0, 0, 0, 0, 1} ,
        new List<int>    { 1, 1, 0, 0, 0, 0, 1, 1, 1, 1 ,1, 0, 0, 0, 0, 1, 1, 1} ,
        new List<int>    { 1, 0, 0, 1, 1, 1, 0, 0, 1, 1 ,0, 0, 1, 1, 1, 0, 0, 1} ,
        new List<int>    { 1, 1, 0, 0, 0, 1, 0, 1, 1, 1 ,1, 0, 0, 0, 1, 0, 1, 1} ,
        new List<int>    { 1, 0, 1, 1, 0, 1, 0, 0, 1, 1 ,0, 1, 1, 0, 1, 0, 0, 1} ,
        new List<int>    { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0 ,0, 0, 0, 0, 1, 1, 0, 1} ,
        new List<int>    { 1, 0, 1, 1, 0, 0, 0, 0, 1, 1 ,0, 1, 1, 0, 0, 0, 0, 1 } ,
        new List<int>    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1 } ,
        };

        for (int i = 0; i < maze.Count; i++)
        {
            for (int j = 0; j < maze[i].Count; j++)
            {
                if (maze[i][j] == 1)
                {
                    GameObject game = Instantiate(cube);
                    game.transform.position = new Vector3(i, 0, j);
                }
                else if (maze[i][j] == 2)
                {
                    GameObject game = Instantiate(cube);
                    game.transform.position = new Vector3(i, 0, j);
                    game.GetComponent<MeshRenderer>().material.color = Color.black;
                }
            }
        }
        cube.SetActive(false);
    }

    int BreakMaze()
    {
        Vector3 mPos = transform.position;
        int x = (int)mPos.x;
        int y = (int)mPos.y;
        int z = (int)mPos.z;
        int direction = 0;
        Vector3 _pass = new Vector3(x, y, z);
        int count = 0;
        while (direction < 4)
        {
            switch (direction)
            {
                case 0://右
                    _pass.x = x;
                    _pass.y = y;
                    _pass.z = z + 1;
                    break;
                case 1://上
                    _pass.x = x - 1;
                    _pass.y = y;
                    _pass.z = z;
                    break;
                case 2://左
                    _pass.x = x;
                    _pass.y = y;
                    _pass.z = z - 1;
                    break;
                case 3://下
                    _pass.x = x + 1;
                    _pass.y = y;
                    _pass.z = z;
                    break;
                default://....
                    Debug.Log(direction + "************");
                    break;
            }
            switch (maze[(int)_pass.x][(int)_pass.z])//判断是障碍还是可通过
            {
                case 0://0为可通过
                    transform.position = _pass;
                    stack.Push(_pass);
                    maze[x][z] = 9;
                    return 0;
                case 1://1为障碍
                    count++;
                    break;
                case 2://2为出口
                    Debug.Log("找到出口!");
                    return 2;
                case 9://9为已经走过
                    count++;
                    break;
            }
            if (count >= 4)//当某个方向没有可通过的状态时累加1,四个方向都无法通过时判定为此条路径无解
            {
                Debug.Log("此条路径无解,换条路试试!");
                count = 0;//重新计数
                maze[x][z] = 9;//标记为已经到达过这里
                if (stack.Pop() == oriPos)
                {
                    Debug.Log("回到了原点?");
                    stack.Push(oriPos);
                }
                transform.position = stack.Peek();//取出栈顶元素
            }
            direction++;//当前方向无法通过,换另一个方向
        }
        return -1;
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值