private void SaveLabyrinth_Click(object sender, RoutedEventArgs e) { Labyrinth lab = lc.Robot.ActualLabyrinth; string data = string.Join(";", lab.GetAllRelations().Where(r => r.Relation == RelationType.Close)); File.WriteAllText("Data.txt", lab.Width + ";" + lab.Height + ";" + data); }
private Point GetMiddle(Labyrinth labyrinth, Block block) { double widthFactor = ActualWidth / BlocksWidth; double heightFactor = ActualHeight / BlocksHeight; return(new Point(widthFactor * (block.X + 0.5), heightFactor * (block.Y + 0.5))); }
private void NextLabyrinthStep_Click(object sender, RoutedEventArgs e) { try { string[] labyrinthData = File.ReadAllText("Data.txt").Split(';'); int width = int.Parse(labyrinthData.First()); int height = int.Parse(labyrinthData.ElementAt(1)); Labyrinth labyrinth = Labyrinth.GetActual(width, height); foreach (string relationData in labyrinthData.Skip(2)) { double[] relationPosition = relationData.Split('x').Select(d => double.Parse(d)).ToArray(); labyrinth[relationPosition[0], relationPosition[1]].Close(); } lc.Robot.ActualPossibleRoute?.Cancel(); lc.Robot.LearnMaybeRoute?.Cancel(); lc.Robot.LearnPossibleRoute?.Cancel(); lc.Robot.LearnRobotRoute?.Cancel(); lc.Robot = new Robot(labyrinth); } catch { } }
public static Labyrinth GetLern(Labyrinth labyrinth) { int targetX, targetY; labyrinth.GetPosition(labyrinth.Target.TopLeft, out targetX, out targetY); return(new Labyrinth(labyrinth.Width, labyrinth.Height, targetX, targetY)); }
public static Labyrinth GetActual(int width, int height) { Labyrinth labyrinth = new Labyrinth(width, height, width / 2, height / 2); SetRelationValue(labyrinth, 2); return(labyrinth); }
private Point GetMiddle(Labyrinth labyrinth, Block block) { int x, y; labyrinth.GetPosition(block, out x, out y); double widthFactor = ActualWidth / BlocksWidth; double heightFactor = ActualHeight / BlocksHeight; return(new Point(widthFactor * (x + 0.5), heightFactor * (y + 0.5))); }
private static void OnBlocksHeightPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { var s = (LabyrinthControl)sender; var value = (int)e.NewValue; if (s.Robot.ActualLabyrinth.Height == value) { return; } s.Robot = new Robot(Labyrinth.GetActual(s.BlocksWidth, s.BlocksHeight)); }
public Robot(Labyrinth actualLabyrinth) { ActualLabyrinth = actualLabyrinth; LearnLabyrinth = Labyrinth.GetLern(actualLabyrinth); ActualPossibleRoute = new Search(ActualLabyrinth.Width * ActualLabyrinth.Height); LearnRobotRoute = new Search(LearnLabyrinth.Width * LearnLabyrinth.Height); LearnMaybeRoute = new Search(LearnLabyrinth.Width * LearnLabyrinth.Height); LearnPossibleRoute = new Search(LearnLabyrinth.Width * LearnLabyrinth.Height); toTarget = false; NextStep(); }
public Search(Labyrinth labyrinth, ITarget target, IRelationInterpreter interpreter) { Next = Block.None; Labyrinth = labyrinth; Target = target; Interpreter = interpreter; Distances = GetDistancesArray(Labyrinth.Width, Labyrinth.Height); Counts = new int[Labyrinth.Width, Labyrinth.Height]; current = new Block[Labyrinth.Width * Labyrinth.Height]; CurrentLength = 0; PossibleLength = 0; }
private static void OnLabyrinthPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { LabyrinthControl s = sender as LabyrinthControl; Labyrinth value = (Labyrinth)e.NewValue; if (value != null) { foreach (SearchView search in s.Searches) { search.Labyrinth = value; } } s.HasChanges = true; }
private void FullSpeedDrive() { Labyrinth actulLab = lc.Robot.ActualLabyrinth; lc.Robot.ActualPossibleRoute.SearchPossibleAsync(actulLab[0, 0], null, actulLab.Target); while (drive && !lc.Robot.ActualPossibleRoute.Finished) { lc.Robot.ActualPossibleRoute.Task?.Wait(100); } while (drive) { lc.Robot.NextStep(); Task.Delay(wait).Wait(wait + 10); } }
private void SaveLabyrinth_Click(object sender, RoutedEventArgs e) { IRelationInterpreter interpreter = new ActualInterpreter(); Labyrinth lab = lc.Labyrinth; string text = lab.Width + "x" + lab.Height + "|"; text += lab.Target.TopLeft.X + "x" + lab.Target.TopLeft.Y + "|"; text += string.Join(";", GetClosedRealtions(lab.H)) + "|"; text += string.Join(";", GetClosedRealtions(lab.V)); try { File.WriteAllText("Data.txt", text); } catch { } }
public Robot(Labyrinth labyrinth) { Labyrinth = labyrinth; toTarget = true; startTarget = new SingleTarget(Block.Origin); actual = new ActualInterpreter(); best = new BestCaseInterpreter(); worst = new WorstCaseInterpreter(); Brush = Brushes.Blue; DriveSearch = new SearchView() { Labyrinth = Labyrinth, Target = Labyrinth.Target, IsLabyrinthTarget = false, Start = Block.Origin, InterpreterType = RelationInterpreterType.BestCase, SearchType = SearchType.Direct }; BestSearch = new SearchView() { Labyrinth = Labyrinth, Target = Labyrinth.Target, IsLabyrinthTarget = true, Start = Block.Origin, InterpreterType = RelationInterpreterType.BestCase, SearchType = SearchType.Direct, PossibleRouteColor = Colors.Gold }; WorstSearch = new SearchView() { Labyrinth = Labyrinth, Target = Labyrinth.Target, IsLabyrinthTarget = true, Start = Block.Origin, InterpreterType = RelationInterpreterType.WorstCase, SearchType = SearchType.Direct, PossibleRouteColor = Colors.Aqua }; }
private static void SetRelationValue(Labyrinth labyrinth, int value) { for (int i = 0; i < labyrinth.H.Width; i++) { for (int j = 0; j < labyrinth.H.Height; j++) { labyrinth.H[i, j] = value; } } for (int i = 0; i < labyrinth.V.Width; i++) { for (int j = 0; j < labyrinth.V.Height; j++) { labyrinth.V[i, j] = value; } } }
public static void Main(string[] args) { //var testGrid = new char[,] //{ // { CellState.EMPTY, CellState.WALL, CellState.WALL,CellState.WALL }, // { CellState.EMPTY, CellState.EMPTY, CellState.GOAL,CellState.WALL }, // { CellState.EMPTY, CellState.WALL, CellState.WALL,CellState.EMPTY }, // { CellState.EMPTY, CellState.EMPTY, CellState.WALL,CellState.EMPTY }, // { CellState.WALL, CellState.START, CellState.EMPTY,CellState.EMPTY }, //}; //var grid = new Grid { Name = "TestGrid", Tiles = testGrid }; var grid = new Grid(@"./../../Grids/Test.txt"); Console.WriteLine("Running on grid: "); grid.WriteToConsole(); var labyrinth = new Labyrinth { Grid = grid };
public static Labyrinth GetActual(int width, int height) { Labyrinth labyrinth = new Labyrinth(width, height, width / 2, height / 2); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (i + 1 < width) { labyrinth[i, j].Right.Open(); } if (j + 1 < height) { labyrinth[i, j].Bottom.Open(); } } } return(labyrinth); }
private void LoadLabyrinth() { try { string[] generalParts = File.ReadAllText("Data.txt").Split('|'); string[] sizeParts = generalParts[0].Split('x'); int width = int.Parse(sizeParts[0]); int height = int.Parse(sizeParts[1]); string[] targetParts = generalParts[1].Split('x'); int targetX = int.Parse(targetParts[0]); int targetY = int.Parse(targetParts[1]); Labyrinth lab = new Labyrinth(width, height, targetX, targetY); foreach (string hClosedText in generalParts[2].Split(';')) { string[] hClosedParts = hClosedText.Split('x'); int x = int.Parse(hClosedParts[0]); int y = int.Parse(hClosedParts[1]); lab.H[x, y] = 3; } foreach (string vClosedText in generalParts[3].Split(';')) { string[] vClosedParts = vClosedText.Split('x'); int x = int.Parse(vClosedParts[0]); int y = int.Parse(vClosedParts[1]); lab.V[x, y] = 3; } lc.Labyrinth = lab; } catch { } }
public BruteForceSearch(Labyrinth labyrinth, ITarget target, IRelationInterpreter interpreter) : base(labyrinth, target, interpreter) { }
//private static Random ran = new Random(); //public static Labyrinth GetActual(int width, int height, int minRouteLength) //{ // System.Diagnostics.Debug.WriteLine("GetActual"); // Labyrinth labyrinth = new Labyrinth(width, height, width / 2, height / 2); // do // { // for (int i = 0; i < width; i++) // { // for (int j = 0; j < height; j++) // { // if (i + 1 < width) labyrinth[i, j].Right.Open(); // if (j + 1 < height) labyrinth[i, j].Bottom.Open(); // } // } // labyrinth.CloseTarget(); // } while (!labyrinth.AddWalls(minRouteLength)); // System.Diagnostics.Debug.WriteLine("RemoveJustCorners"); // labyrinth.RemoveJustCorners(); // labyrinth.OpenWalls(minRouteLength, 50); // return labyrinth; //} //public void CloseTarget() //{ // IEnumerable<BlockRelation> targetRelations = GetAllRelations(). // Where(r => Target.Is(r.Block1) ^ Target.Is(r.Block2)); // foreach (BlockRelation relation in targetRelations) relation.Close(); // targetRelations.ElementAt(ran.Next(targetRelations.Count())).Open(); //} //public bool AddWalls(int minRouteLength) //{ // List<BlockRelation> relations = GetAddableRelation().ToList(); // Route currentRoute = null; // while (relations.Count > 0) // { // int index = ran.Next(relations.Count); // BlockRelation previousChangedRelation = relations[index]; // previousChangedRelation.Close(); // relations.RemoveAt(index); // currentRoute = new Route(Width * Height); // if (!Start.TryGetPossibleRoute(ref currentRoute, Target)) // { // previousChangedRelation.Open(); // } // else if (currentRoute.CurrentLength > minRouteLength) return true; // } // System.Diagnostics.Debug.WriteLine("AddedWalls: " + currentRoute.CurrentLength); // return false; //} //public void RemoveJustCorners() //{ // Route currentRoute = new Route(Width * Height); // Start.TryGetPossibleRoute(ref currentRoute, Target); // BlockRelation[] relations = GetAddableRelation(). // Where(ar => !currentRoute.GetRelations().Any(rr => ar == rr)).ToArray(); // IEnumerable<BlockRelation> corners = relations.Where(IsAroundJustCorner); // while (true) // { // int count = corners.Count(); // if (count == 0) break; // corners.ElementAt(ran.Next(count)).Close(); // } //} //public bool IsAroundJustCorner(BlockRelation relation) //{ // if (relation.Relation != RelationType.Open) return false; // if (relation.Block2 == null) return false; // if (relation.Block1.GetTopBlock() == relation.Block2) // { // if (relation.Block1.GetLeftBlock()?.Top.Relation == RelationType.Open) return true; // if (relation.Block1.GetRightBlock()?.Top.Relation == RelationType.Open) return true; // } // else if (relation.Block1.GetBottomBlock() == relation.Block2) // { // if (relation.Block1.GetLeftBlock()?.Bottom.Relation == RelationType.Open) return true; // if (relation.Block1.GetRightBlock()?.Bottom.Relation == RelationType.Open) return true; // } // else if (relation.Block1.GetRightBlock() == relation.Block2) // { // if (relation.Block1.GetTopBlock()?.Right.Relation == RelationType.Open) return true; // if (relation.Block1.GetBottomBlock()?.Right.Relation == RelationType.Open) return true; // } // else if (relation.Block1.GetLeftBlock() == relation.Block2) // { // if (relation.Block1.GetTopBlock()?.Right.Relation == RelationType.Open) return true; // if (relation.Block1.GetBottomBlock()?.Right.Relation == RelationType.Open) return true; // } // return false; //} //public void OpenWalls(int minRouteLength, int maxClosedWalls) //{ // Random ran = new Random(); // List<BlockRelation> relations = GetAllRelations(). // Where(r => r.Block2 != null && r.Relation == RelationType.Close).ToList(); // while (relations.Count > maxClosedWalls) // { // int index = ran.Next(relations.Count); // BlockRelation previousChangedRelation = relations[index]; // previousChangedRelation.Open(); // relations.RemoveAt(index); // Route currentRoute = new Route(Width * Height); // Start.TryGetPossibleRoute(ref currentRoute, Target); // if (currentRoute.CurrentLength < minRouteLength) previousChangedRelation.Close(); // } //} //private IEnumerable<BlockRelation> GetAddableRelation() //{ // return GetAllRelations(). // Where(r => r.Relation == RelationType.Open && (!Target.Is(r.Block1) && !Target.Is(r.Block2))); //} #endregion public static Labyrinth GetLern(Labyrinth labyrinth) { int targetX = labyrinth.Target.TopLeft.X, targetY = labyrinth.Target.TopLeft.Y; return(new Labyrinth(labyrinth.Width, labyrinth.Height, targetX, targetY)); }
private void RenderRoute(DrawingContext drawingContext, IEnumerable <Block> route, Labyrinth labyrinth, Pen pen) { var array = route.ToArray(); if (array.Length <= 1) { return; } double widthFactor = ActualWidth / BlocksWidth; double heightFactor = ActualHeight / BlocksHeight; Point previousPoint = GetMiddle(labyrinth, array.First()); foreach (Block block in array.Skip(1)) { Point currentPoint = GetMiddle(labyrinth, block); drawingContext.DrawLine(pen, previousPoint, currentPoint); previousPoint = currentPoint; } }
public DirectSearch(Labyrinth labyrinth, ITarget target, IRelationInterpreter interpreter) : base(labyrinth, target, interpreter) { }