C#自动生成迷宫
2013-10-15 15:32
246 查看
网上找到些自动生成迷宫的代码,主要是用C语言写的,可参照http://blog.sina.com.cn/s/blog_66ad7bba0100hk6k.html,本文对其稍作修改,使用C#改写,代码应该是我见到的最简单的了吧,如有兴趣,我们可以互相讨论。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
static class CreateMaze
{
static int[,] m=new int[50,50];
static int[,] d = new int[4,2] { {0,1},{1,0},{0,-1},{-1,0}};
static int w = 5,h=5,u = w * 2 + 1, v = h * 2 + 1;
static Random num = new Random();
static int Init(int y, int x)
{
if (x < 1 || y < 1 || x >= u - 1 || y >= v - 1 ||m[y,x]==1)
{
return 0;
}
else
{
m[y,x] = 1;
}
for (int f = num.Next() % 4, i = 0, p = (num.Next() &1)==1 ? 3: 1; i < 4; ++i, f = (f + p) % 4)
{
if (Init(y + d[f,0] * 2, x + d[f,1] * 2)==1)
{
m[y + d[f,0],x + d[f,1]] = 1;
}
}
return 1;
}
static void Main()
{
Init(1,1);
m[1, 0] = m[v - 2, u - 1] = 1;
for (int y = 0; y < v; y++)
{
for (int x = 0; x < u; x++)
{
Console.Write((m[y,x]==1) ? " " : "█");
}
Console.WriteLine("");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
static class CreateMaze
{
static int[,] m=new int[50,50];
static int[,] d = new int[4,2] { {0,1},{1,0},{0,-1},{-1,0}};
static int w = 5,h=5,u = w * 2 + 1, v = h * 2 + 1;
static Random num = new Random();
static int Init(int y, int x)
{
if (x < 1 || y < 1 || x >= u - 1 || y >= v - 1 ||m[y,x]==1)
{
return 0;
}
else
{
m[y,x] = 1;
}
for (int f = num.Next() % 4, i = 0, p = (num.Next() &1)==1 ? 3: 1; i < 4; ++i, f = (f + p) % 4)
{
if (Init(y + d[f,0] * 2, x + d[f,1] * 2)==1)
{
m[y + d[f,0],x + d[f,1]] = 1;
}
}
return 1;
}
static void Main()
{
Init(1,1);
m[1, 0] = m[v - 2, u - 1] = 1;
for (int y = 0; y < v; y++)
{
for (int x = 0; x < u; x++)
{
Console.Write((m[y,x]==1) ? " " : "█");
}
Console.WriteLine("");
}
}
}
}
相关文章推荐
- C#.NET自动生成Excel图形报表
- 以实例说明如何使用C#从数据库中提取数据,按要求自动生成定制的Excel表格?
- [C#]一步一步开发自己的自动代码生成工具之五:DAL层模板
- 自动从C#代码生成流程图
- .NET1.1下,使用C#自动生成Word2003文档(通过操作COM组件实现)
- 使用Signature Tool自动生成P/Invoke调用Windows API的C#函数声明
- asp.net中用C#自动生成透明的GIF图片
- C#分析数据库结构,使用XSL模板自动生成代码
- 自动生成迷宫
- C#分析数据库结构,使用XSL模板自动生成代码
- 如何在asp.net中用C#自动生成透明的GIF图片
- 基于递归分割的迷宫生成算法与自动寻路
- C# 自动生成类
- [C#]一步一步开发自己的自动代码生成工具之六:业务层模板
- C#迷宫生成的算法 --转
- 使用Visual Studio 2005 IDE的宏,自动为c#变量生成属性
- 以实例说明如何使用C#从数据库中提取数据,按要求自动生成定制的Excel表格
- 使用Signature Tool自动生成P/Invoke调用Windows API的C#函数声明
- AutoCode (vs插件)自动生成代码插件 下载及使用教程(C#版)
- asp.net中用C#自动生成透明的GIF图片