private static async Task Main(string[] args) { var map = @" 1 1R 1111 1111 11 1 111 111 1 1111 1 1 11 1 1 1111111 111 1 111 1111 11 1 11 11 1 111 1111 11 1 11 1111 1Q 11 "; var cellsProvider = new FakeCellsProvider(map); IEnumerable <Cell> cells = null; IRobot robot = new Robot(); await((Robot)robot).Init("тест"); var mazeSolver = new MazeSolver(robot); //await mazeSolver.Run(); await mazeSolver.Run(f => { Console.Clear(); Console.WriteLine(f.ToString()); }); }
public Maze Solve(MazeSolver solver) { var ans = new Maze(width, height); ans.Field = solver.Solve(width, height, CopyField(width, height, Field)); return(ans); }
private void BG_Worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { try { if (MakeMazeCB.Checked) { MazeGenerator mazeGenerator = new MazeGenerator(new Size((int)ColumnsNUD.Value, (int)RowsNUD.Value)); mazeGenerator.Generate(); mazeGenerator.SaveAsImage(MakeOutputPathTB.Text); mazeGenerator = null; if (!SolveMazeCB.Checked) { System.Diagnostics.Process.Start(MakeOutputPathTB.Text); } } if (SolveMazeCB.Checked) { MazeSolver mazeSolver = new MazeSolver(SolveInputPathTB.Text); mazeSolver.Solve(); mazeSolver.SaveSolutionAsImage(SolveOutputPathTB.Text); mazeSolver = null; System.Diagnostics.Process.Start(SolveOutputPathTB.Text); } GC.Collect(); } catch (Exception ex) { MessageBox.Show(ex.Message); throw; } }
public void InitializeMazeSolverTests() { var width = 5; var heigth = 5; var mazeData = new string[][] { new[] { "north", "west" }, new[] { "north", "west" }, new[] { "north" }, new[] { "north" }, new[] { "north" }, new[] { "west" }, new[] { "north" }, new[] { "north", "west" }, new string[0], new[] { "west" }, new[] { "north", "west" }, new string[0], new[] { "west" }, new[] { "north" }, new[] { "west" }, new[] { "west" }, new[] { "north", "west" }, new[] { "north" }, new[] { "west" }, new[] { "west" }, new[] { "west" }, new string[0], new[] { "west" }, new string[0], new[] { "west" } }; var mazeState = new MazeState() { Id = "TestMaze1234", Difficulty = 1, PlayerName = "moi", Dimensions = new[] { width, heigth }, Data = mazeData, PonyPosition = new[] { ponyPosition }, DomokunPosition = new[] { domokunPosition }, ExitPosition = new[] { exitPosition }, GameState = new GameState() { MazeState = "Active", MoveStatus = "Successfully created" } }; mazeSolver = new MazeSolver(mazeState); }
Vector3 getNextPosition(MazeGrid.Grid maze, Cell current, Cell target) { MazeSolver solver = new MazeSolver(maze, 0, 0, rows - 1, columns - 1); LinkedList <Cell> path = solver.Solve(current); Cell next = path.First.Next.Value; return(new Vector3(next.Row * wallLength + wallLength / 2, 0.0f, next.Column * wallLength + wallLength / 2)); }
public void PassMaze_SuccessfulTests_Case4() { MazeSolver solver = new MazeSolver(sourceData[3], startXs[3], startYs[3]); solver.PassMaze(); int[,] output = solver.MazeWithPass(); CollectionAssert.AreEquivalent(output, result[3]); }
public void ShouldReturnNoNeighborsIfNoDestroyedWalls() { MazeSolver solver = new MazeSolver(BuildMaze(3, 3)); MazeSlot slot = new MazeSlot(1, 1); List <MazeSlot> moves = solver.NextMoves(slot); Assert.Empty(moves); }
private async Task <bool> UpdateMazeState(MazeState state = null) { var waitBmp = MazePainter.MazePainter.GetWaitBmp(); pictureBox1.Image = waitBmp; if (state == null) { try { state = await _client.GetMazeCurrentState(_mazeId); } catch (PonyClientException ex) { DoLog(ex.Message); return(false); } catch (Exception ex) { DoLog("CRITICAL: " + ex.Message); return(false); } } if (!string.IsNullOrEmpty(state.HiddenImageUrl)) { try { var imgData = await _client.LoadImage(state.HiddenImageUrl); var ms = new MemoryStream(imgData); pictureBox1.Image = Image.FromStream(ms); } catch (Exception ex) { DoLog(ex.Message); } } else { _solver = MazeSolver.Load(state); // paint a maze var bmp = MazePainter.MazePainter.RepresentMaze(_solver.Field, state.MazeWidth, state.MazeHeight, _solver.PonyLocation.x, _solver.PonyLocation.y, _solver.ExitLocation.x, _solver.ExitLocation.y, _solver.DomokunLocation.x, _solver.DomokunLocation.y); pictureBox1.Image = bmp; } if (state.State == "over" || state.State == "won") { return(false); } return(true); }
public void PassMaze_SuccessfulTests() { for (int i = 0; i < sourceData.Length; i++) { MazeSolver solver = new MazeSolver(sourceData[i], startXs[i], startYs[i]); solver.PassMaze(); Assert.AreEqual(MatrixAreEquals(solver.MazeWithPass(), result[i]), true); } }
/// <summary> /// Called when solve button is clicked. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param> private async void OnSolve(object sender, RoutedEventArgs e) { this.MovesTextBlock.Text = null; this.SolverButton.IsEnabled = false; this.solver = this.factory.CreateSolver(); this.solver.Progress += this.OnProgress; await this.solver.SolveAsync(); this.solver.Progress -= this.OnProgress; this.SolverButton.IsEnabled = true; }
static void Main(string[] args) { Console.WriteLine("Hello World!"); Console.ReadKey(true); MazeSolver solver = new MazeSolver(); solver.TakeStep(); //solver.PrintMaze(); //Console.WriteLine(solver.GetCurrentPathString()); Console.ReadKey(true); }
public void PassMazeAndStructReturn() { var solver = new MazeSolver(result[0], 3, 5); solver.PassMaze(); if (!PointAreEquals(solver.PointsForExit(), List)) { Assert.Fail(); } Assert.Pass(); }
public void PassMaze_SuccessfulTests() { for (int i = 0; i < sourceData.Length; i++) { MazeSolver solver = new MazeSolver(sourceData[i], startXs[i], startYs[i]); solver.PassMaze(); if (!MatrixAreEquals(solver.MazeWithPass(), result[i])) { Assert.Fail("Matrix are not equal"); } } }
private void CreateViewSolution() { ViewSolution = new ObservableCollection <CellItem>(); List <Cell> solution = MazeSolver.FindSolution(Maze); foreach (CellItem cellItem in ViewCells) { if (solution.Contains(cellItem.Cell)) { ViewSolution.Add(cellItem); } } }
public void ShouldReturnCorrectNeighbors(int x, int y, WallPosition expectedWall) { MazeSlot[,] maze = BuildMaze(3, 3); MazeSolver solver = new MazeSolver(maze); MazeSlot slot = new MazeSlot(1, 1); slot.DestroyWall(maze[x, y]); List <MazeSlot> moves = solver.NextMoves(slot); Assert.Single(moves); Assert.Contains(expectedWall, slot.DestroyedWalls); }
public void ShouldIgnoreVisitedNeighbors(int x, int y) { MazeSlot[,] maze = BuildMaze(3, 3); MazeSolver solver = new MazeSolver(maze); MazeSlot slot = new MazeSlot(1, 1); slot.DestroyWall(maze[x, y]); maze[x, y].VisitedBySolver = true; List <MazeSlot> moves = solver.NextMoves(slot); Assert.Empty(moves); }
public void PassMaze_SuccessfulTests() { for (int i = 0; i < 2; i++) // first two working { MazeSolver solver = new MazeSolver(sourceData[i], startXs[i], startYs[i]); solver.PassMaze(); if (!MatrixAreEquals(solver.MazeWithPass(), result[i])) { Assert.Fail(); } } }
public void PassMaze_SuccessfulTests() { for (int i = 0; i < sourceData.Length; i++) { MazeSolver solver = new MazeSolver(sourceData[i], startXs[i], startYs[i]); solver.PassMaze(); var maze = solver.MazeWithPass(); if (!MatrixAreEquals(maze, result[i])) { PrintMaze(maze); Assert.Fail($"Test #{i}"); } } }
public static void Main() { int row = 1; int col = 0; MazeSolver maze = new MazeSolver(); maze.solve(row, col); for (int i = 0; i < maze.maze.GetLength(0); i++) { for (int j = 0; j < maze.maze.GetLength(1); j++) { System.Console.Write(maze.maze[i, j]); } System.Console.WriteLine(""); } }
static void Main(string[] args) { int[,] map = new int[, ] { { -1, -1, -1, -1, -1, -1 }, { 0, 0, 0, -1, 0, 0 }, { -1, 0, -1, -1, 0, -1 }, { -1, 0, -1, -1, 0, 0 }, { 0, 0, 0, 0, 0, -1 }, { -1, -1, -1, -1, -1, -1 } }; int startX = 3; int startY = 5; MazeSolver solver = new MazeSolver(map, startX, startY); solver.PassMaze(); solver.PrintMap(); }
public void OnInit(InitContext context) { if (context == InitContext.Loaded) { _path = new Stack <Point2>(); _playerMonsterTiles = GameObj.GetComponent <Tilemap>(); //TODO: get player position from player controll component _playerController = GameObj.GetComponent <PlayerController>(); _monsterPosition = new Point2(_playerMonsterTiles.Size.X - 1, _playerMonsterTiles.Size.Y - 1); Tile monsterInitTile = _playerMonsterTiles.Tiles[_monsterPosition.X, _monsterPosition.Y]; monsterInitTile.Index = 1; _playerMonsterTiles.SetTile(_monsterPosition.X, _monsterPosition.Y, monsterInitTile); //Do pathfinding _mazeTiles = GameObj.Parent.ChildByName("BordersTilemap").GetComponent <Tilemap>(); _path = MazeSolver.SolveMaze(_mazeTiles, _playerController.PlayerPosition, _monsterPosition); } }
public void MazeSolverTest_WhenMaze2_2_ThenFindsPath() { Maze maze = new Maze(3, 3); maze.Grid[0, 0].SetWall(Direction.right, false); maze.Grid[1, 0].SetWall(Direction.right, false); maze.Grid[1, 0].SetWall(Direction.left, false); maze.Grid[1, 0].SetWall(Direction.down, false); maze.Grid[2, 0].SetWall(Direction.left, false); maze.Grid[2, 0].SetWall(Direction.down, false); maze.Grid[0, 1].SetWall(Direction.down, false); maze.Grid[1, 1].SetWall(Direction.down, false); maze.Grid[1, 1].SetWall(Direction.up, false); maze.Grid[2, 1].SetWall(Direction.up, false); maze.Grid[0, 2].SetWall(Direction.up, false); maze.Grid[0, 2].SetWall(Direction.right, false); maze.Grid[1, 2].SetWall(Direction.up, false); maze.Grid[1, 2].SetWall(Direction.left, false); maze.Grid[1, 2].SetWall(Direction.right, false); maze.Grid[2, 2].SetWall(Direction.left, false); MazeSolver solver = new MazeSolver(maze); solver.MarkPath(0, 2, 2, 0); string expectedMaze = " _ _ _\n" + "|_ * *|\n" + "| |*|_|\n" + "|_ _ _|\n"; Assert.AreEqual(expectedMaze, maze.ToString()); }
static void Main(string[] args) { //var m = tinyMaze; //Node[] nodes = Nodify(m); //foreach (Node n in nodes) //{ // (int x, int y) = n.Position; // m[x, y] = 3; //} //PrintMaze(m); //return; int[,] maze = tinyMaze; MazeSolver solveMaze = RightHandRule; Stopwatch sw = new Stopwatch(); Console.WriteLine("Maze:"); PrintMaze(maze); Console.WriteLine("Crunching numbers..."); sw.Start(); int[,] solvedMaze = solveMaze(maze, out bool solved); sw.Stop(); if (solved) { Console.WriteLine("Solution found!"); PrintMaze(solvedMaze); } else { Console.WriteLine("No solution found!"); } Console.WriteLine($"\nElapsed time: {sw.ElapsedMilliseconds} ms"); Console.ReadLine(); }
private void DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; bool failed = false; MazeSolver solver = CreateSolver(); solver.Stage += SolverOnStage; solver.SolveMaze(Maze.Value, SourceCell.Value, TargetCell.Value); solver.Stage -= SolverOnStage; if (!failed) { e.Result = solver; } void SolverOnStage(ProgressStage stage, Maze maze, float percentage) { if (worker.CancellationPending && (stage == ProgressStage.Started || stage == ProgressStage.Ongoing)) { e.Cancel = true; solver.Cancel(); } switch (stage) { case ProgressStage.Failed: failed = true; break; case ProgressStage.Ongoing: worker.ReportProgress((int)percentage); break; } } }
private async void btnLoadMaze_Click(object sender, EventArgs e) { txtSequence.Text = ""; if (!Guid.TryParse(edtMazeId.Text, out _mazeId)) { edtMazeId.Text = ""; DoLog("Error: Maze ID guid is not valid."); } // load try { var state = await _client.GetMazeCurrentState(_mazeId); _solver = MazeSolver.Load(state); DoLog($"Maze with ID = {_mazeId} loaded."); DoLog("Maze State: " + state.State); DoLog("Maze Result State: " + state.MazeStateResult); if (!string.IsNullOrEmpty(state.HiddenImageUrl)) { DoLog("Hidden Image Url: " + state.HiddenImageUrl); } await UpdateMazeState(state); } catch (PonyClientException ex) { DoLog(ex.Message); return; } catch (Exception ex) { DoLog("CRITICAL: " + ex.Message); return; } }
private void CreateViewCells() { ViewCells = new ObservableCollection <CellItem>(); double graphicsSize = MazeCanvasSize / (double)Size; List <Cell> solution = MazeSolver.FindSolution(Maze); for (int row = 0; row < Maze.RowsCount; row++) { for (int column = 0; column < Maze.ColumnsCount; column++) { Cell cell = Maze[row, column]; CellItem cellItem = new CellItem(cell) { TopLocation = row * graphicsSize, LeftLocation = column * graphicsSize, Width = graphicsSize, }; ViewCells.Add(cellItem); } } }
private void _backGroundWorker_DoWork(object sender, DoWorkEventArgs e) { object[] args = e.Argument as object[]; int value = 25; // Cell size bool solving = (bool)args[1]; if (!solving) { //this._maze.Generate(this.pictureBoxDraw.Width / value, // (this.pictureBoxDraw.Height - value) / value, // (int)args[2]); _maze = new Maze(mazePicBox.Width, mazePicBox.Height, 25); _mazeGenerator = new GenerateMaze(_maze); _mazeGenerator.Generate(value); } else { var solver = new MazeSolver(_maze); solver.Solve(); this.hasSolution = true; } this.mazePicBox.Invalidate(); }
public string SolveMaze(string mazeAsString) { MazeSolver solver = new MazeSolver(); // Set the symbols that may occur in the maze solver.GoalSymbol = 'B'; solver.OpenSymbol = '.'; solver.StartSymbol = 'A'; solver.WallSymbol = '#'; // Set the additional symbol that may occur in a solution // to the maze solver.PathSymbol = '@'; string errorMessage = ""; try { // Information on the solution attempt may retrieved // from properties of the solver object after the // SolveMaze() call completes solver.SolveMaze(mazeAsString); errorMessage = solver.ErrorMessage; } catch (Exception exception) { errorMessage = String.Format("Exception: {0} {1}", exception.Message, exception.StackTrace); } // Construct the JSON string that is to be returned StringBuilder jsonBuilder = new StringBuilder(); // Since the task specification didn't request any error // reporting, I extended the JSON response to include four // items related to error reporting. jsonBuilder.AppendLine("{"); jsonBuilder.Append(" \"error message\": \""); jsonBuilder.Append(errorMessage); jsonBuilder.AppendLine("\""); jsonBuilder.Append(" \"symbols are valid\": "); jsonBuilder.AppendLine(solver.SymbolsAreValid.ToString()); jsonBuilder.Append(" \"maze is valid\": "); jsonBuilder.AppendLine(solver.MazeIsValid.ToString()); jsonBuilder.Append(" \"solution found\": "); jsonBuilder.AppendLine(solver.SolutionFound.ToString()); jsonBuilder.Append(" \"steps\": "); jsonBuilder.AppendLine(solver.NumSolutionSteps.ToString()); jsonBuilder.Append(" \"solution\": \""); jsonBuilder.Append(solver.Solution); jsonBuilder.AppendLine("\""); jsonBuilder.AppendLine("}"); return(jsonBuilder.ToString()); }
private void Awake() { singleton = this; }
static void Main(string[] args) { // The command line must contain one argument, the full // path and name of a file. It is recommended to // wrap this argument in double-quotes to avoid misparsing. if (args.LongLength == 1) { try { string filePathName = args[0]; if (File.Exists(filePathName)) { string mazeAsString = File.ReadAllText(filePathName); Console.WriteLine(mazeAsString); MazeSolver solver = new MazeSolver(); // Set the symbols that may occur in the maze solver.GoalSymbol = 'B'; solver.OpenSymbol = '.'; solver.StartSymbol = 'A'; solver.WallSymbol = '#'; // Set the additional symbol that may occur in a solution // to the maze solver.PathSymbol = '@'; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); solver.SolveMaze(mazeAsString); stopwatch.Stop(); Console.WriteLine(); Console.WriteLine(solver.Solution); Console.WriteLine(); if (solver.ErrorMessage.Length > 0) { Console.WriteLine("Error message: {0}", solver.ErrorMessage); } Console.WriteLine("Symbols are {0}", solver.SymbolsAreValid ? "valid" : "not valid"); if (solver.SymbolsAreValid) { Console.WriteLine("Maze is {0}", solver.MazeIsValid ? "valid" : "not valid"); if (solver.MazeIsValid) { Console.WriteLine("A solution was {0}", solver.SolutionFound ? "found" : "not found"); if (solver.SolutionFound) { Console.WriteLine("Number of solution steps = {0}", solver.NumSolutionSteps); } } } Console.WriteLine("Elapsed time: {0} milliseconds", stopwatch.ElapsedMilliseconds); } else { Console.WriteLine("File '{0}' does not exist", filePathName); } } catch (Exception exception) { Console.WriteLine("Exception: {0} {1}", exception.Message, exception.StackTrace); } Console.WriteLine(); Console.Write("Press any key to exit the program... "); Console.ReadKey(); } else { Console.WriteLine("Usage: mazeFilePathName"); } }
private void toolStripButton1_Click(object sender, EventArgs e) { var maze = this.generator.GetMaze( int.Parse(this.toolStripTextBox2.Text), int.Parse(this.toolStripTextBox1.Text), int.Parse(this.toolStripTextBox3.Text) ); var mazeString = new String[maze.GetLength(1),maze.GetLength(0)]; GraphNode<MazeNode,int> start = null; GraphNode<MazeNode,int> end = null; for(int i=0;i<maze.GetLength(1) ;i++) { for(int j=0;j<maze.GetLength(0);j++) { var mazeCell = maze[j,i]; var strCell = String.Empty; switch(mazeCell.Value.Type) { case Maze.CellType.start : strCell = "S"; start = mazeCell; break; case Maze.CellType.end : strCell = "E";end = mazeCell ; break; case Maze.CellType.empty : strCell = "";break; default : throw new Exception("Unsuported cell type:" + mazeCell.Value.Type); } foreach(var n in mazeCell.Neighbours) { if(mazeCell.Value.X ==n.Key.Value.X) { if(mazeCell.Value.Y == n.Key.Value.Y +1) { strCell+="↑"; } else if(mazeCell.Value.Y == n.Key.Value.Y -1) { strCell +="↓" ; } else { strCell+="?"; } } else if(mazeCell.Value.Y ==n.Key.Value.Y) { if(mazeCell.Value.X == n.Key.Value.X +1) { strCell+="←"; } else if(mazeCell.Value.X == n.Key.Value.X -1) { strCell +="→" ; } else { strCell+="?"; } } else { strCell+="?"; } } mazeString[i,j] = strCell; } } // solve it var mazeSolver = new MazeSolver(); var path = mazeSolver.Solve(maze,start.Value.X , start.Value.Y,end.Value.X,end.Value.Y).ToList(); // do display it this.dataGridView1.Rows.Clear(); this.dataGridView1.Columns.Clear(); this.dataGridView1.DefaultCellStyle.Font = new System.Drawing.Font("Lucida Sans Unicode", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); for(int index=0;index<mazeString.GetLength(0);index++) { this.dataGridView1.Columns.Add(index.ToString(),index.ToString()); } this.dataGridView1.SuspendLayout(); for(int index=0;index<mazeString.GetLength(1);index++) { var row = new DataGridViewRow(); row.HeaderCell.Value = index.ToString(); for(int i=0;i<mazeString.GetLength(0);i++) { var cell = new DataGridViewTextBoxCell() { Value = mazeString[i, index] }; row.Cells.Add(cell); if(path.Any(p=>p.Value.X == index && p.Value.Y == i)) { cell.Style.BackColor = Color.Green; } } this.dataGridView1.Rows.Add(row); } this.dataGridView1.ResumeLayout(true); }