/// <summary> /// Generate maze. Start with cell with X and Y /// </summary> /// <param name="X"></param> /// <param name="Y"></param> /// <returns></returns> public Labirint Generate(int X = 0, int Y = 0) { lab = new Labirint(Wigth, Height); //All cells with spetificator == 1 is add to NotPassCell foreach (var cell in lab.Cells) { if ((cell.spetificator == Cell.Spetificator.Pass)) { NotPassCell.Add(cell); } } //Random first cell if (X == 0 && Y == 0) { while (true) { X = random.Next(Wigth - 1) + 1; Y = random.Next(Height - 1) + 1; if (lab[X, Y].spetificator == Cell.Spetificator.Pass) { NotPassCell.Remove(lab[X, Y]); break; } } } Cell currentCell = lab[X, Y]; StartCell = currentCell; while (NotPassCell.Count != 0) { List <Cell> NearCells = FindNear(currentCell.X, currentCell.Y); while (NearCells.Count == 0) { currentCell = PassCell.Pop(); X = currentCell.X; Y = currentCell.Y; NearCells = FindNear(X, Y); if (PassCell.Count == 0) { break; } } //The choice direction int direct = random.Next(NearCells.Count); PassCell.Push(currentCell); if (NearCells.Count != 0) { DeleteWall(currentCell, NearCells[direct]); } if (NearCells.Count != 0) { currentCell = NearCells[direct]; } NotPassCell.Remove(currentCell); } StopCell = currentCell; currentCell.spetificator = Cell.Spetificator.Exit; lab = GenerateCoins(lab); return(lab); }
/// <summary> /// Drow all elements in console /// </summary> /// <param name="lab">Maze</param> /// <param name="hero">Hero</param> public static void Drow(Labirint lab, Hero hero) { var str = new StringBuilder(); str.AppendLine("Welcome to simple maze!"); str.AppendLine("Some rules: "); str.AppendLine(" You're X. Your task is find exit from the maze."); str.AppendLine(" Exit is marked with a symbol >."); str.AppendLine(" In maze you can find coins, that will be increase your score. Good luck!"); str.AppendLine("Use Arrows or WASD to move "); str.AppendLine("Press R - to rebuild maze(Score and Level will be reset)."); str.AppendLine("Press ESC - to exit from application."); str.AppendLine($"Score: {hero.Score}"); str.AppendLine($"Level: {hero.Level}"); for (int y = 0; y < lab.Height; y++) { str.AppendLine(); for (int x = 0; x < lab.Width; x++) { if ((x == hero.X) && (y == hero.Y)) { str.Append(hero.Symbol); } else { if (lab[x, y].spetificator == Cell.Spetificator.Wall) { str.Append("#"); } else { if (lab[x, y].spetificator == Cell.Spetificator.Coin) { str.Append("c"); } else { if (lab[x, y].spetificator == Cell.Spetificator.Exit) { str.Append(">"); } else { str.Append(" "); } } } } } } Console.Write(str); }
/// <summary> /// Generate 5 coins in maze. /// </summary> /// <param name="lab"></param> /// <returns></returns> private Labirint GenerateCoins(Labirint lab) { int i = 0; while (i < 5) { int X = random.Next(lab.Width - 1) + 1; int Y = random.Next(lab.Height - 1) + 1; if (lab[X, Y].spetificator == Cell.Spetificator.Pass) { lab[X, Y].spetificator = Cell.Spetificator.Coin; i++; } } return(lab); }