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;
}
}