private void ProccessInput(string input, Labyrinth labyrinth, ref int movesCount, Ladder ladder) { string inputToLower = input.ToLower(); switch (inputToLower) { case "u": case "d": case "l": case "r": //fallthrough bool moveDone = TryMove(inputToLower, labyrinth); if (moveDone == true) { movesCount++; } break; case "top": ladder.PrintLadder(); break; case "exit": Console.WriteLine(UserInputAndOutput.GOODBYE_MSG); Environment.Exit(0); break; case "restart": break; default: string errorMessage = UserInputAndOutput.INVALID_COMMAND_MSG; Console.WriteLine(errorMessage); break; } }
public Game(Random rand, Ladder ladder) { Labyrinth labyrinth = new Labyrinth(rand); UserInputAndOutput.PrintWelcomeMessage(); int movesCount = 0; string input = ""; while (!IsGameOver(labyrinth) && input != "restart") { UserInputAndOutput.PrinyLabyrinth(labyrinth); input = UserInputAndOutput.GetInput(); ProccessInput(input, labyrinth, ref movesCount, ladder); } if (input != "restart") { Console.WriteLine("Congratulations! You escaped in {0} moves.", movesCount); if (ladder.ResultQualifiesInLadder(movesCount)) { Console.WriteLine( UserInputAndOutput.ENTER_NAME_FOR_SCOREBOARD_MSG); string name = Console.ReadLine(); ladder.AddResultInLadder(movesCount, name); } } Console.WriteLine(); }
public void OnSetRoad(Labyrinth labyrinth) { // так не делается (захламление консоли + управление основным потоком), да, но ради эффекта почему бы и нет ShowLabyrinth(labyrinth); System.Threading.Thread.Sleep(20); Console.Clear(); }
public void ShowLabyrinth(Labyrinth labyrinth) { for (int i = 0; i < labyrinth.LabyrinthArray.GetLength(0); i++) { for (int j = 0; j < labyrinth.LabyrinthArray.GetLength(1); j++) { if (labyrinth.LabyrinthArray[i, j] == labyrinth.Road) { Console.ForegroundColor = ConsoleColor.Green; } else if (labyrinth.LabyrinthArray[i, j] == labyrinth.Wall) { Console.ForegroundColor = ConsoleColor.Red; } else if (labyrinth.LabyrinthArray[i, j] == labyrinth.Start) { Console.ForegroundColor = ConsoleColor.Yellow; } else if (labyrinth.LabyrinthArray[i, j] == labyrinth.End) { Console.ForegroundColor = ConsoleColor.Cyan; } else { Console.ForegroundColor = ConsoleColor.White; } Console.Write("|" + labyrinth.LabyrinthArray[i, j]); } Console.ResetColor(); Console.WriteLine(); } }
private void setEndInLabyrinth(Labyrinth labyrinth, Tuple <int, int> startPoint) { // ищет самую дальнюю точку по алгоритму Дейкстры, а затем ищет от этой точки ближайшую стену Dijkstra dijkstra = new Dijkstra(); var farPoint = dijkstra.GetFarPoint(labyrinth, startPoint); var farPointInWall = findPointInWall(labyrinth, farPoint); labyrinth.LabyrinthArray[farPointInWall.Item1, farPointInWall.Item2] = labyrinth.End; }
static void Main(string[] args) { Cord size = new Cord(26, 26); Cord start = new Cord(0, 0); Cord end = new Cord(25, 25); Labyrinth lab = new Labyrinth(size, start, end); Console.ReadKey(); }
private void generate_btn_Click(object sender, EventArgs e) { int size = 35; this.Height = size * 25; this.Width = size * 25 + 10; dun = new Labyrinth(0, size); String data = Visualizer.toHTML(dun.Dungeon, size); browser.DocumentText = data; }
static void Main() { var labyrinth = new Labyrinth(10); Console.WriteLine("Empty labyrinth:\n"); labyrinth.Print(); Console.WriteLine(); labyrinth.Fill(); Console.WriteLine("Filled labyrinth:\n"); labyrinth.Print(); }
static void Main() { var labyrinth = new Labyrinth(); foreach (var s in labyrinth.Pass(labyrinth.Positions[0, 0], 0, 0)) { Console.WriteLine($"{s.Top} {s.Bottom} {s.Left} {s.Right}"); } Console.ReadLine(); }
private Tuple <int, int> GetNextVertex(Labyrinth labyrinth, Dictionary <Tuple <int, int>, int> vertexPoints, Tuple <int, int> lastPoint, Tuple <int, int> lastDirection, ref int length) { lastDirection = Tuple.Create(lastDirection.Item1 * -1, lastDirection.Item2 * -1); length++; int roadCount = 0; Tuple <int, int> newPoint = null; Tuple <int, int> newRoad = null; Tuple <int, int> newDirection = null; foreach (var direction in labyrinth.Directions.OrderBy(d => random.Next())) { var x = lastPoint.Item1 + direction.Item1; var y = lastPoint.Item2 + direction.Item2; newPoint = Tuple.Create(x, y); if (lastDirection.Equals(direction)) { continue; } if (x > labyrinth.LabyrinthArray.GetLength(0) - 1 || x < 0 || y > labyrinth.LabyrinthArray.GetLength(0) - 1 || y < 0) { continue; } if (labyrinth.LabyrinthArray[x, y] == labyrinth.Wall) { continue; } roadCount++; newRoad = newPoint; newDirection = direction; } if (roadCount == 1) { return(GetNextVertex(labyrinth, vertexPoints, newRoad, newDirection, ref length)); } else { if (vertexPoints.ContainsKey(lastPoint)) { return(null); } else { return(lastPoint); } } }
public static void PrinyLabyrinth(Labyrinth labyrinth) { int labyrinthSize = Labyrinth.LABYRINTH_SIZE; for (int row = 0; row < labyrinthSize; row++) { for (int col = 0; col < labyrinthSize; col++) { Cell cell = labyrinth.GetCell(row, col); Console.Write(cell.ValueChar + " "); } Console.WriteLine(); } }
public Tuple <int, int> GetFarPoint(Labyrinth labyrinth, Tuple <int, int> startPoint) { Dictionary <Tuple <int, int>, int> vertexPoints = new Dictionary <Tuple <int, int>, int>(); List <Vertex> vertexOrder = new List <Vertex>(); vertexOrder.Add(new Vertex(startPoint, 0)); while (vertexOrder.Count != 0) { var vertex = vertexOrder[0]; foreach (var direction in labyrinth.Directions.OrderBy(d => random.Next())) { Tuple <int, int> newPoint = null; var x = vertex.Coordinates.Item1 + direction.Item1; var y = vertex.Coordinates.Item2 + direction.Item2; if (x >= labyrinth.LabyrinthArray.GetLength(0) - 1 || x <= 0 || y >= labyrinth.LabyrinthArray.GetLength(0) - 1 || y <= 0 || labyrinth.LabyrinthArray[x, y] == labyrinth.Wall) { continue; } newPoint = Tuple.Create(x, y); int newVertexLength = 0; var newVertex = GetNextVertex(labyrinth, vertexPoints, newPoint, direction, ref newVertexLength); if (newVertex == null) { continue; } if (vertexOrder.FirstOrDefault(v => v.Coordinates.Equals(newVertex)) != null) { if (vertex.Length + newVertexLength < vertexOrder.First(v => v.Coordinates.Equals(newVertex)).Length) { vertexOrder.First(v => v.Coordinates.Equals(newVertex)).Length = vertex.Length + newVertexLength; } } else { vertexOrder.Add(new Vertex(newVertex, newVertexLength + vertex.Length)); } } vertexPoints.Add(vertex.Coordinates, vertex.Length); vertexOrder.Remove(vertex); } return(vertexPoints.OrderByDescending(v => v.Value).ToArray()[0].Key); }
private bool IsGameOver(Labyrinth labyrinth) { bool isGameOver = false; int currentRow = labyrinth.currentCell.Row; int currentCol = labyrinth.currentCell.Col; if (currentRow == 0 || currentCol == 0 || currentRow == Labyrinth.LABYRINTH_SIZE - 1 || currentCol == Labyrinth.LABYRINTH_SIZE - 1) { isGameOver = true; } return isGameOver; }
private bool IsGameOver(Labyrinth labyrinth) { bool isGameOver = false; int currentRow = labyrinth.currentCell.Row; int currentCol = labyrinth.currentCell.Col; if (currentRow == 0 || currentCol == 0 || currentRow == Labyrinth.LABYRINTH_SIZE - 1 || currentCol == Labyrinth.LABYRINTH_SIZE - 1) { isGameOver = true; } return(isGameOver); }
private void GiveUp_Click(object sender, EventArgs e) { if (prikaziPateka != 1) { LabyrinthWorld = Labyrinth.najdiPatekaVoRandomLavirint_DFS(LabyrinthWorld); prikaziPateka = 1; timer.Stop(); TimeBar.Enabled = false; TimeBar.Value = TimeBar.Minimum; NegativniBar.Enabled = false; NegativniBar.Value = NegativniBar.Minimum; TimeLbl.Text = string.Format("0:00"); LavPanel.Invalidate(); KeyEventArgs ee = new KeyEventArgs(Keys.Enter); Form1_KeyDown(sender, ee); } }
private void Labyrinth() { Console.Clear(); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("Escape!"); Console.ForegroundColor = ConsoleColor.White; Labyrinth Lab = new Labyrinth(); Maze Mz = new Maze(); //Mz.Generator(); Lab.Move(); while (Console.ReadKey().Key == ConsoleKey.Backspace) { break; } }
private void setRoadInLabyrinth(Labyrinth labyrinth, Tuple <int, int> lastPoint) { // метод вызывается рекурсивно из каждой точки дороги // рандомно выбираются направления установки дороги foreach (var direction in labyrinth.Directions.OrderBy(d => random.Next())) { var newPoint = Tuple.Create(lastPoint.Item1 + direction.Item1, lastPoint.Item2 + direction.Item2); // условие не позволяет создавать "комнаты", ге дороги становятся в плотную друг к другу if (checkPoint(labyrinth, newPoint.Item1, newPoint.Item2)) { labyrinth.LabyrinthArray[newPoint.Item1, newPoint.Item2] = labyrinth.Road; SetRoad?.Invoke(labyrinth); setRoadInLabyrinth(labyrinth, newPoint); } } }
public Form1(int n, int nivo, int poeni, int t, string igrac) { InitializeComponent(); Top = Left = 50; ff = 0; prikaziPateka = 0; restartFlag = 0; pateka = new List <Koordinata>(); doubleBuffer = new Bitmap(LavPanel.Width, LavPanel.Height); Restart.Image = Image.FromFile("Restart.jpg"); Restart.SizeMode = PictureBoxSizeMode.StretchImage; GiveUp.Image = Image.FromFile("WhiteFlag.jpg"); GiveUp.SizeMode = PictureBoxSizeMode.StretchImage; Igrac = igrac; ImeLbl.Text = string.Format("{0}", Igrac); time = t; TimeBar.Maximum = t; TimeBar.Value = t; NegativniBar.Maximum = poeni; f = 1; pat = 0; krajFlag = 0; FirstPress = 0; TimeLbl.Text = string.Format("{0}:{1}{2}", time / 60, (time - (time / 60) * 60) / 10, (time - (time / 60) * 60) - ((time - (time / 60) * 60) / 10) * 10); this.Text = string.Format("Smart Maze - Ниво {0}", nivo); Nivo = nivo; NivoLbl.Text = string.Format("Ниво {0}", Nivo); VkPoeni = poeni; NegPoeni = 0; NegLbl.Text = string.Format("{0}/{1}", NegPoeni, VkPoeni); N = n; LabyrinthWorld = Labyrinth.generirajLavirint(N); Kocka = (doubleBuffer.Width) / (LabyrinthWorld.Length); newGame(); }
public Labyrinth CreateLabyrinth(int size, int road, int wall, int start, int end) { int[,] labyrinthArray = new int[size, size]; Labyrinth labyrinth = new Labyrinth(road, wall, start, end, labyrinthArray); Random random = new Random(); setDefaultValues(labyrinthArray, labyrinth.Wall); // в случайном месте границы массива устанавливается точка старта var startPoint = getRandomStartPoint(size); labyrinthArray[startPoint.Item1, startPoint.Item2] = labyrinth.Start; // установка дороги случайным образом (рандом просто пуляет в разные стороны) setRoadInLabyrinth(labyrinth, startPoint); // находится самая дальняя точка (по алгоритму Дейкстры), // затем в близжайшей от нее точке в боковой стенке лабиринта устанавливается выход setEndInLabyrinth(labyrinth, startPoint); return(labyrinth); }
private void Restart_Click(object sender, EventArgs e) { restartFlag = 1; LabyrinthWorld = Labyrinth.generirajLavirint(N); timer.Stop(); prikaziPateka = 0; f = 1; pat = 0; krajFlag = 0; FirstPress = 0; time = TimeBar.Maximum; TimeBar.Value = TimeBar.Maximum; NegativniBar.Value = NegativniBar.Minimum; newGame(); LavPanel.Invalidate(); KeyEventArgs ee = new KeyEventArgs(Keys.Enter); NegPoeni = 0; NegLbl.Text = string.Format("{0}/{1}", NegPoeni, VkPoeni); }
private bool TryMove(string direction, Labyrinth labyrinth) { bool moveDone = false; switch (direction) { case "u": moveDone = labyrinth.TryMove(labyrinth.currentCell, Direction.Up); break; case "d": moveDone = labyrinth.TryMove(labyrinth.currentCell, Direction.Down); break; case "l": moveDone = labyrinth.TryMove(labyrinth.currentCell, Direction.Left); break; case "r": moveDone = labyrinth.TryMove(labyrinth.currentCell, Direction.Right); break; default: Console.WriteLine(UserInputAndOutput.INVALID_MOVE_MSG); break; } if (moveDone == false) { Console.WriteLine(UserInputAndOutput.INVALID_MOVE_MSG); } return(moveDone); }
private bool checkPoint(Labyrinth labyrinth, int x, int y) { if (x >= labyrinth.LabyrinthArray.GetLength(0) - 1 || x <= 0 || y >= labyrinth.LabyrinthArray.GetLength(0) - 1 || y <= 0 || labyrinth.LabyrinthArray[x, y] != labyrinth.Wall) { return(false); } else { int size = labyrinth.LabyrinthArray.GetLength(0); int roadCount = 0; for (int xOffset = -1; xOffset < 2; xOffset++) { for (int yOffset = -1; yOffset < 2; yOffset++) { if (xOffset == 0 && yOffset == 0) { continue; } if (labyrinth.LabyrinthArray[x + xOffset, y + yOffset] == labyrinth.Road) { roadCount++; } } } if (roadCount >= 3) { return(false); } else { return(true); } } }
private Tuple <int, int> findPointInWall(Labyrinth labyrinth, Tuple <int, int> point) { Tuple <int, int> lastDirection = null; Tuple <int, int> newDirection = null; Tuple <int, int> nextRoad = null; while (true) { nextRoad = point; lastDirection = newDirection; foreach (var direction in labyrinth.Directions.OrderBy(d => random.Next())) { int x = nextRoad.Item1 + direction.Item1; int y = nextRoad.Item2 + direction.Item2; var newPoint = Tuple.Create(x, y); if (lastDirection == direction) { continue; } if (x == 0 || x == labyrinth.LabyrinthArray.GetLength(0) - 1 || y == 0 || y == labyrinth.LabyrinthArray.GetLength(1) - 1) { return(newPoint); } if (labyrinth.LabyrinthArray[x, y] == labyrinth.Road) { newDirection = Tuple.Create(direction.Item1 * -1, direction.Item2 * -1); point = newPoint; } } } }
private bool TryMove(string direction, Labyrinth labyrinth) { bool moveDone = false; switch (direction) { case "u": moveDone = labyrinth.TryMove(labyrinth.currentCell, Direction.Up); break; case "d": moveDone = labyrinth.TryMove(labyrinth.currentCell, Direction.Down); break; case "l": moveDone = labyrinth.TryMove(labyrinth.currentCell, Direction.Left); break; case "r": moveDone = labyrinth.TryMove(labyrinth.currentCell, Direction.Right); break; default: Console.WriteLine(UserInputAndOutput.INVALID_MOVE_MSG); break; } if (moveDone == false) { Console.WriteLine(UserInputAndOutput.INVALID_MOVE_MSG); } return moveDone; }
// Намиране на път в лабиринт static void Main(string[] args) { Labyrinth.lab = Labyrinth.ReadLab(); Labyrinth.FindPaths(0, 0, 'S'); }
static void Main() { using (var game = new Labyrinth()) game.Run(); }
public Cell(Labyrinth parent, Cord pos) { this.parent = parent; Pos = pos; }
public void Initialize() { labyrinth = new Labyrinth(Height, Width, ConsoleColor.DarkGreen, ConsoleColor.DarkBlue); player = new Player(PlayerSym, labyrinth.Walls); }
public void Start() { Labyrinth labyrinth = new Labyrinth(); }