[Robotics Studio] 继续修改迷宫, 加上机器人-- Day26
2009/1/16 17:51 | 阅读数: 4829 | 我要推荐 | 3 Comments | 订阅嗯,之前的迷宫产生程式有点小bug,就是在分割房间的时候,如果产生的墙壁刚好挡住之前挖开的洞,这个迷宫就有可能会不通...
所以加上一点小修改来弥补这样的错误,最后整个MazeEntity的程式如下(我直接把注解写在code 当中了):
001 using System;
002 using System.Collections.Generic;
003 using System.ComponentModel;
004 using Microsoft.Ccr.Core;
005 using Microsoft.Dss.Core.Attributes;
006 using Microsoft.Dss.ServiceModel.Dssp;
007 using Microsoft.Dss.ServiceModel.DsspServiceBa;
脸肿是什么原因
008 using W3C.Soap;
009 using submgr = Microsoft.Dss.Services.SubscriptionManager;
010 using engine = Microsoft.Robotics.Simulation.Engine.Proxy;
011 using Microsoft.Robotics.Simulation.Engine;
012 using Microsoft.Robotics.Simulation.Physics;
013 using Microsoft.Robotics.PhysicalModel;
014
015
016 namespace MazeGenerator
漏机油是什么原因
017 {
018 public class MazeEntity : MultiShapeEntity
019 {
020 /// <summary>
021 /// Default constructor
022 /// </summary>
023 public MazeEntity() { }
024
025 /// <summary>
026 /// Initialization constructor
砂仁的功效
027 /// </summary>
028 /// <param name="shape"></param>
029 /// <param name="initialPos"></param>
030 public MazeEntity(Vector3 initialPos,byte[,]
blocks,float mazeheight,float blocksize)
031 {
032 for(int x = 0; x < blocks.GetLength(0); x++) 033 {
034
for(int y
= 0; y <
blocks.GetLength(1); y++)
035 {
036 if(blocks[x,y] == 0) 037 continue; 038
039 var boxshape
=new BoxShape(
04 0
new BoxShapeProper ties(
04 1
100,// mass in kilograms.
04 2
new Po(new Vector 3(initialPos.X + x * blocksize, initialPos.Y + mazeheight / 2, initialPos.Z - y*blocksize)),// relative po
04 3
new Vector3(blocks ize, mazeheight, blocksize)));
044
04 5
this.BoxShapes.Add(boxsha pe);// dimensions
046 }
047
}
048
049 this.State.MassDensity.Mass
=
blocks.GetLength(0)*blocks.GetLength(1)*100; 050 }
051
052 public static byte[,]
GenerateMaze(int width,int height)
053 {
054 byte[,] result =new byte[width, height]; 055 Random r =new Random();
056 for(int x = 0; x < width; x++)
057 for(int y = 0; y < height; y++)
彩霞是什么意思
058 result[x, y] = (byte)(((x
一体机电脑== 0) || (y == 0) || (x == width - 1) || (y == height - 1)) ? 1 : 0); 059
060 // dig a hole of left-top, and right-down 061 result[0, 1] = 0;
062 result[width - 1, height - 2] = 0;
063
064 splitMazeChamber(r, result, 0, 0, width - 1,
height - 1);
065
066 return result; 067 }
068
069 /// <summary>
070 /// 将一个房间切割为迷宫, 前提是这个房间四周都是
墙壁, 但是会有一些通道, 连到其他房间
071 /// </summary>
072 /// <param name="r"></param>
073 /// <param name="maze"></param> 074 /// <param name="left"></param> 075 /// <param name="top"></param> 076 /// <param name="right"></param> 077 /// <param name="bottom"></param>
078 private static void splitMazeChamber(Random
r,byte[,] maze,int left,int top,int right,int bottom)
079 {
080 int wallX = 0;
081 if(right - left > 3)
082 wallX = r.Next(left + 2, right - 1); 083
084 int wallY = 0;
085 if(bottom - top > 3)
086 wallY = r.Next(top + 2, bottom - 1); 087
088 // 停止条件- 没得切割房间
089 if((wallX <= 0) && (wallY <= 0))
090 return;
091
092 // 建造墙壁来隔开房间
093 if(wallX > 0)
094 {
095 for(int i = top + 1; i < bottom; i++) 096 maze[wallX, i] = 1;
097
098 // 假如这道墙壁刚好盖在外部的通
道, 就拆掉一格墙壁
099 if(maze[wallX, top] == 0)
100 maze[wallX, top + 1] = 0; 101 if(maze[wallX, bottom] == 0)
102 maze[wallX, bottom - 1] = 0; 103 }
104
105 if(wallY > 0)
106 {
107 for(int i = left + 1; i < right; i++) 108 maze[i, wallY] = 1;
109
110 // 假如这道墙壁刚好盖在外部的通
道, 就拆掉一格墙壁
111 if(maze[left, wallY] == 0)
112 maze[left + 1, wallY] = 0; 113 if(maze[right, wallY] == 0)
9月10日什么星座
114 maze[right - 1, wallY] = 0; 115 }
116
117 // 打通各个房间, 然后继续切割每个房间
118 if((wallX > 0) && (wallY > 0))
119 {
120 List<KeyValuePair<int,int>>
holes =new List<KeyValuePair<int,int>>();
12holes.Add(new KeyValuePair<int,in
1 t>(wallX, r.Next(top + 1, wallY - 1)));
12 2
holes.Add(new KeyValuePair<int,in t>(wallX, r.Next(wallY + 1, bottom - 1)));
12 3
holes.Add(new KeyValuePair<int,in t>(r.Next(left + 1, wallX - 1), wallY));
12 4
holes.Add(new KeyValuePair<int,in t>(r.Next(wallX + 1, right - 1), wallY));
125 holes.RemoveAt(r.Next(0, 4));
126
holes.ForEach(hole
=>
maze[hole.Key, hole.Value] = 0); 127
快乐夏天128 splitMazeChamber(r, maze, left,
top, wallX, wallY);
129 splitMazeChamber(r, maze, wallX,
top, right, wallY);
简历在校经历
130 splitMazeChamber(r, maze, left,
wallY, wallX, bottom);
131 splitMazeChamber(r, maze, wallX,
wallY, right, bottom);
132 }
133 el if(wallX > 0) 134 {
135 maze[wallX, r.Next(top + 1, bottom
- 1)] = 0; 136
137 splitMazeChamber(r, maze, left,
top, wallX, bottom);
138 splitMazeChamber(r, maze, wallX,
top, right, bottom);
139 }
140 el if(wallY > 0) 141 {
142 maze[r.Next(left + 1, right - 1),
wallY] = 0; 143
144 splitMazeChamber(r, maze, left,
top, right, wallY);
145 splitMazeChamber(r, maze, left,