用C#来设计一个简单迷宫?
2个回答
展开全部
using System;using System.Collections.Generic;using System.Text;
namespace Maze{ class Maze { public class Path { public Dictionary<int, bool> rooms = new Dictionary<int, bool>(); public Dictionary<Path, bool> neighors = new Dictionary<Path, bool>(); public Dictionary<int, bool> deletedColumnWalls = new Dictionary<int, bool>(); public Dictionary<int, bool> deletedRowWalls = new Dictionary<int, bool>();
public Path(int id) { rooms.Add(id, true); }
public override string ToString() { return rooms.ToString(); } } int row; int column; Random rand = new Random(); List<Path> paths = new List<Path>();
static void Main(string[] args) { Maze m = new Maze(); m.row = 20;//行 m.column = 18;//列 m.run(); m.printResult(); }
char EMPTY = ' '; private char[] GRID = { ' ','┃','━','┏', '━','┓','━','┳', '┃','┃','┗','┣', '┛','┫','┻','╋' };
private void run() { for (int x = 0; x < row; x++) { for (int y = 0; y < column; y++) { int id = x * column + y; Path path = new Path(id);
if (y > 0) { Path left = paths[paths.Count - 1]; left.neighors.Add(path, true); path.neighors.Add(left, true); } if (x > 0) { Path top = paths[paths.Count - column]; top.neighors.Add(path, true); path.neighors.Add(top, true); } paths.Add(path); } } while (paths.Count > 1) mergePaths(); }
private void mergePaths() { Path first = paths[rand.Next(paths.Count)];
List<int> options = new List<int>(); for (int i = 0; i < paths.Count; i++) { Path tmp = paths[i]; if (first != tmp) { if (first.neighors.ContainsKey(tmp)) options.Add(i); } }
int secondIndex = options[rand.Next(options.Count)]; Path second = paths[secondIndex];
List<List<int>> connectWalls = getConnectWall(first, second); int wallIndex = rand.Next(connectWalls[0].Count + connectWalls[1].Count); if (wallIndex < connectWalls[0].Count) first.deletedRowWalls.Add(connectWalls[0][wallIndex], true); else first.deletedColumnWalls.Add(connectWalls[1][wallIndex - connectWalls[0].Count], true);
first.neighors.Remove(second); second.neighors.Remove(first); foreach (Path secondNeighbor in second.neighors.Keys) { secondNeighbor.neighors.Remove(second); if (!secondNeighbor.neighors.ContainsKey(first)) secondNeighbor.neighors.Add(first, true); if (!first.neighors.ContainsKey(secondNeighbor)) first.neighors.Add(secondNeighbor, true); } foreach (int room in second.rooms.Keys) first.rooms.Add(room, true);
foreach (int wall in second.deletedRowWalls.Keys) first.deletedRowWalls.Add(wall, true); foreach (int wall in second.deletedColumnWalls.Keys) first.deletedColumnWalls.Add(wall, true);
paths.RemoveAt(secondIndex); }
private List<List<int>> getConnectWall(Path first, Path second) { List<List<int>> result = new List<List<int>>(); List<int> rowWalls = new List<int>(); List<int> columnWalls = new List<int>(); result.Add(rowWalls); result.Add(columnWalls);
bool firstLess = first.rooms.Count <= second.rooms.Count; Dictionary<int, bool> rooms = firstLess ? first.rooms : second.rooms; Dictionary<int, bool> secondRooms = firstLess ? second.rooms : first.rooms; foreach (int room in rooms.Keys) { if (secondRooms.ContainsKey(room - column)) rowWalls.Add(room - column); if (secondRooms.ContainsKey(room + column)) rowWalls.Add(room);
int columnIndex = room % column; if (columnIndex > 0 && secondRooms.ContainsKey(room - 1)) columnWalls.Add(room - 1); if (columnIndex < column - 1 && secondRooms.ContainsKey(room + 1)) columnWalls.Add(room); } return result; }
private void printResult() { char[][] grid = new char[row * 2 + 2][]; for (int i = 0; i < grid.Length; i++) { grid[i] = new char[column * 2 + 2]; for (int z = 0; z < grid[i].Length; z++) grid[i][z] = EMPTY; } Path path = paths[0]; for (int rowIndex = 0; rowIndex <= row; rowIndex++) { for (int columnIndex = 0; columnIndex <= column; columnIndex++) { int id = columnIndex + rowIndex * column; bool leftWall = rowIndex != row && (columnIndex == 0 || columnIndex == column || ! path.deletedColumnWalls.ContainsKey(id - 1)); bool topWall = columnIndex != column && (rowIndex == 0 || rowIndex == row || ! path.deletedRowWalls.ContainsKey(id - column)); bool leftTopWall = columnIndex != 0 && (rowIndex == 0 || rowIndex == row || ! path.deletedRowWalls.ContainsKey(id - column - 1)); bool topLeftWall = rowIndex != 0 && (columnIndex == 0 || columnIndex == column || ! path.deletedColumnWalls.ContainsKey(id-column-1));
grid[rowIndex * 2 + 1][columnIndex * 2 + 1] = EMPTY; grid[rowIndex * 2 + 1][columnIndex * 2] = leftWall ? '┃' : EMPTY; grid[rowIndex * 2][columnIndex * 2 + 1] = topWall ? '━' : EMPTY;
int index = 0; index |= leftWall ? 1 : 0; index |= topWall ? 2 : 0; index |= leftTopWall ? 4 : 0; index |= topLeftWall ? 8 : 0; grid[rowIndex * 2][columnIndex * 2] = GRID[index]; } }
grid[1][0] = EMPTY; grid[row * 2 - 1][column * 2] = EMPTY; for (int rowIndex = 0; rowIndex < grid.Length - 1; rowIndex++) { for (int columnIndex = 0; columnIndex < grid[row].Length - 1; columnIndex++) Console.Write(grid[rowIndex][columnIndex]); Console.WriteLine(); } } }
}
namespace Maze{ class Maze { public class Path { public Dictionary<int, bool> rooms = new Dictionary<int, bool>(); public Dictionary<Path, bool> neighors = new Dictionary<Path, bool>(); public Dictionary<int, bool> deletedColumnWalls = new Dictionary<int, bool>(); public Dictionary<int, bool> deletedRowWalls = new Dictionary<int, bool>();
public Path(int id) { rooms.Add(id, true); }
public override string ToString() { return rooms.ToString(); } } int row; int column; Random rand = new Random(); List<Path> paths = new List<Path>();
static void Main(string[] args) { Maze m = new Maze(); m.row = 20;//行 m.column = 18;//列 m.run(); m.printResult(); }
char EMPTY = ' '; private char[] GRID = { ' ','┃','━','┏', '━','┓','━','┳', '┃','┃','┗','┣', '┛','┫','┻','╋' };
private void run() { for (int x = 0; x < row; x++) { for (int y = 0; y < column; y++) { int id = x * column + y; Path path = new Path(id);
if (y > 0) { Path left = paths[paths.Count - 1]; left.neighors.Add(path, true); path.neighors.Add(left, true); } if (x > 0) { Path top = paths[paths.Count - column]; top.neighors.Add(path, true); path.neighors.Add(top, true); } paths.Add(path); } } while (paths.Count > 1) mergePaths(); }
private void mergePaths() { Path first = paths[rand.Next(paths.Count)];
List<int> options = new List<int>(); for (int i = 0; i < paths.Count; i++) { Path tmp = paths[i]; if (first != tmp) { if (first.neighors.ContainsKey(tmp)) options.Add(i); } }
int secondIndex = options[rand.Next(options.Count)]; Path second = paths[secondIndex];
List<List<int>> connectWalls = getConnectWall(first, second); int wallIndex = rand.Next(connectWalls[0].Count + connectWalls[1].Count); if (wallIndex < connectWalls[0].Count) first.deletedRowWalls.Add(connectWalls[0][wallIndex], true); else first.deletedColumnWalls.Add(connectWalls[1][wallIndex - connectWalls[0].Count], true);
first.neighors.Remove(second); second.neighors.Remove(first); foreach (Path secondNeighbor in second.neighors.Keys) { secondNeighbor.neighors.Remove(second); if (!secondNeighbor.neighors.ContainsKey(first)) secondNeighbor.neighors.Add(first, true); if (!first.neighors.ContainsKey(secondNeighbor)) first.neighors.Add(secondNeighbor, true); } foreach (int room in second.rooms.Keys) first.rooms.Add(room, true);
foreach (int wall in second.deletedRowWalls.Keys) first.deletedRowWalls.Add(wall, true); foreach (int wall in second.deletedColumnWalls.Keys) first.deletedColumnWalls.Add(wall, true);
paths.RemoveAt(secondIndex); }
private List<List<int>> getConnectWall(Path first, Path second) { List<List<int>> result = new List<List<int>>(); List<int> rowWalls = new List<int>(); List<int> columnWalls = new List<int>(); result.Add(rowWalls); result.Add(columnWalls);
bool firstLess = first.rooms.Count <= second.rooms.Count; Dictionary<int, bool> rooms = firstLess ? first.rooms : second.rooms; Dictionary<int, bool> secondRooms = firstLess ? second.rooms : first.rooms; foreach (int room in rooms.Keys) { if (secondRooms.ContainsKey(room - column)) rowWalls.Add(room - column); if (secondRooms.ContainsKey(room + column)) rowWalls.Add(room);
int columnIndex = room % column; if (columnIndex > 0 && secondRooms.ContainsKey(room - 1)) columnWalls.Add(room - 1); if (columnIndex < column - 1 && secondRooms.ContainsKey(room + 1)) columnWalls.Add(room); } return result; }
private void printResult() { char[][] grid = new char[row * 2 + 2][]; for (int i = 0; i < grid.Length; i++) { grid[i] = new char[column * 2 + 2]; for (int z = 0; z < grid[i].Length; z++) grid[i][z] = EMPTY; } Path path = paths[0]; for (int rowIndex = 0; rowIndex <= row; rowIndex++) { for (int columnIndex = 0; columnIndex <= column; columnIndex++) { int id = columnIndex + rowIndex * column; bool leftWall = rowIndex != row && (columnIndex == 0 || columnIndex == column || ! path.deletedColumnWalls.ContainsKey(id - 1)); bool topWall = columnIndex != column && (rowIndex == 0 || rowIndex == row || ! path.deletedRowWalls.ContainsKey(id - column)); bool leftTopWall = columnIndex != 0 && (rowIndex == 0 || rowIndex == row || ! path.deletedRowWalls.ContainsKey(id - column - 1)); bool topLeftWall = rowIndex != 0 && (columnIndex == 0 || columnIndex == column || ! path.deletedColumnWalls.ContainsKey(id-column-1));
grid[rowIndex * 2 + 1][columnIndex * 2 + 1] = EMPTY; grid[rowIndex * 2 + 1][columnIndex * 2] = leftWall ? '┃' : EMPTY; grid[rowIndex * 2][columnIndex * 2 + 1] = topWall ? '━' : EMPTY;
int index = 0; index |= leftWall ? 1 : 0; index |= topWall ? 2 : 0; index |= leftTopWall ? 4 : 0; index |= topLeftWall ? 8 : 0; grid[rowIndex * 2][columnIndex * 2] = GRID[index]; } }
grid[1][0] = EMPTY; grid[row * 2 - 1][column * 2] = EMPTY; for (int rowIndex = 0; rowIndex < grid.Length - 1; rowIndex++) { for (int columnIndex = 0; columnIndex < grid[row].Length - 1; columnIndex++) Console.Write(grid[rowIndex][columnIndex]); Console.WriteLine(); } } }
}
更多追问追答
追问
我是一新手,刚接触,啥都不会。
这个我是看不懂,不过真的很感谢你。。。
能不能给我来点简单的。。。
谢谢
追答
这是自动绘图的迷宫,每次编译都不一样的,难道你想要固定的迷宫?
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询