class Matrix { private struct Size // - . public int Width { get; set; } public int Height { get; set; } } readonly Size _size; // readonly Texture2D[,] _maze; // public Matrix(Maze.Cells size , Texture2D[,] cell2D) { _maze = cell2D; _size.Width = size.X; _size.Height = size.Y; } public void Draw(SpriteBatch spriteBatch) // { for (var i = 0; i < _size.Height; i++) { for (var j = 0; j < _size.Width; j++) { if ((i % 2 != 0 && j % 2 != 0) && (i < _size.Height - 1 && j < _size.Width - 1)) // x y, { spriteBatch.Draw(_maze[i, j], new Rectangle(i * 10, j * 10, 10, 10), Color.White); // } else { spriteBatch.Draw(_maze[i, j], new Rectangle(i * 10, j * 10, 10, 10), Color.White); // } } } } }
public struct Cells { public int X; public int Y; public Cells(int newX, int newY) { X = newX; Y = newY; } } private SpriteBatch _spriteBatch; private readonly Texture2D[,] _maze; // private Cells _size; // private readonly Cells _start; private readonly Cells _finish; private int _cell; // private int _wall; // private int _visited; // private readonly List<Cells> _neighbours; // private readonly Stack<Cells> _path; // private int _status; // public Maze(List<Cells> neighbours, Stack<Cells> path) { _neighbours = neighbours; _path = path; var graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; var winWidth = graphics.PreferredBackBufferWidth = 210; var winHeight = graphics.PreferredBackBufferHeight = 210; _size = new Cells(winWidth/10, winHeight/10); _start = new Cells(1, 1); _finish = new Cells(_size.X - 2, _size.Y - 2); _maze = new Texture2D[_size.X, _size.Y]; _path.Push(_start); IsMouseVisible = true; }
protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); for (var i = 0; i < _size.Y; i++) { for (var j = 0; j < _size.X; j++) { if ((i%2 != 0 && j%2 != 0) && (i < _size.Y - 1 && j < _size.X - 1)) // . x y, { _maze[i, j] = Content.Load<Texture2D>("flat"); // _cell = _maze[i, j].GetHashCode(); // . } else { _maze[i, j] = Content.Load<Texture2D>("wall");// _wall = _maze[i, j].GetHashCode(); } } } }
_wall = _maze[i, j].GetHashCode(); _cell = _maze[i, j].GetHashCode();
private void DrawMaze() { if (_path.Count != 0) // , { GetNeighbours(_path.Peek()); // , if (_neighbours.Count != 0) // , () { var a = _neighbours[new Random().Next(0, _neighbours.Count)]; // RemoteWall(_path.Peek(), a); // _path.Push(a); // _neighbours.Clear(); // } else { _path.Pop(); // , } } else // { MainPoint(); // _status = 1; // } }
private void GetNeighbours(Cells localcell) // { var x = localcell.X; var y = localcell.Y; const int distance = 2; var d = new[] // { new Cells(x, y - distance), // Up new Cells(x + distance, y), // Right new Cells(x, y + distance), // Down new Cells(x - distance, y) // Left }; for (var i = 0; i < 4; i++) // 4 { var s = d[i]; if (sX <= 0 || sX >= _size.X || sY <= 0 || sY >= _size.Y) continue; // if (_maze[sX, sY].GetHashCode() == _wall || _maze[sX, sY].GetHashCode() == _visited) continue; // _neighbours.Add(s); // } }
private void RemoteWall(Cells first, Cells second) // 2 { var xDiff = second.X - first.X; var yDiff = second.Y - first.Y; Cells target; Cells newCells; var addX = (xDiff != 0) ? xDiff/Math.Abs(xDiff) : 0; // var addY = (yDiff != 0) ? yDiff/Math.Abs(yDiff) : 0; target.X = first.X + addX; // target.Y = first.Y + addY; _maze[target.X, target.Y] = Content.Load<Texture2D>("visited"); // - _maze[_path.Peek().X, _path.Peek().Y] = Content.Load<Texture2D>("visited"); // - _visited = _maze[target.X, target.Y].GetHashCode(); // Hash Code newCells.X = first.X + 2*addX; newCells.Y = first.Y + 2*addY; _path.Push(newCells); // _maze[_path.Peek().X, _path.Peek().Y] = Content.Load<Texture2D>("visited"); // - }
private void MainPoint() { _maze[_start.X, _start.Y] = Content.Load<Texture2D>("start"); _starter = _maze[1, 1].GetHashCode(); _maze[_finish.X, _finish.Y] = Content.Load<Texture2D>("finish"); _finisher = _maze[_finish.X, _finish.Y].GetHashCode(); }
Source: https://habr.com/ru/post/265307/