diff --git a/Trunk/MonoGameView/Algorithms/BFS.cs b/Trunk/MonoGameView/Algorithms/BFS.cs index e6a73b8..14ab60a 100644 --- a/Trunk/MonoGameView/Algorithms/BFS.cs +++ b/Trunk/MonoGameView/Algorithms/BFS.cs @@ -1,12 +1,220 @@ -using System; +using CzokoŚmieciarka.MonoGameView.DataModels.Enums; +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector; +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans; +using CzokoŚmieciarka.MonoGameView.DataModels.Models; +using CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.Xna.Framework.Content; +using System.Threading; namespace MonoGameView.Algorithms { - class BFS + public class BFS { + public GarbageCollector Collector { get; set; } + public ICloneable[,] Grid { get; set; } + public BFS(GarbageCollector collector, ICloneable[,] grid) + { + this.Collector = collector; + this.Grid = grid; + } + int count = 0; + public List Houses { get; set; } + + public List BestPath(ContentManager content, GarbageCollector collector, ICloneable[,] grid) + { + Houses = new List(); + for (int x = 0; x < grid.GetLength(0); x++) + { + for (int y = 0; y < grid.GetLength(1); y++) + { + if (grid[x, y] is House) + { + Houses.Add(new Coords(x, y)); + + } + } + } + + + + var r = SearchBfs(content, collector, grid, 0).Key; + Console.WriteLine($"Counts : {count}"); + if (r == null) return new List(); + return r; + + } + + List PossibleSteps(AGarbageCollector collector, ICloneable[,] grid) + { + + + var result = new List(); + + if (!(collector.TrashContainers.Any(x => x.FillPercent > 0) && grid[collector.Coords.X, collector.Coords.Y] is ADump)) + { + var moveSteps = new List() + { + new MoveStep(Direction.Up), + new MoveStep(Direction.Down), + new MoveStep(Direction.Left), + new MoveStep(Direction.Right) + }; + var filteredMoveSteps = new List(); + foreach (var item in moveSteps) + { + var copyCollector = (AGarbageCollector)collector.Clone(); + if (item.Invoke(copyCollector, grid)) + { + var gcx = copyCollector.Coords.X; + var gcy = copyCollector.Coords.Y; + if (grid[gcx, gcy] is Road1 || grid[gcx, gcy] is House || (grid[gcx, gcy] is ADump && copyCollector.TrashContainers.Any(x => x.FillPercent > 0))) + { + result.Add(item); + } + } + + + } + + + } + if (grid[collector.Coords.X, collector.Coords.Y] is House) + { + var collectSteps = new List() + { + new CollectStep(GarbageType.Glass), + new CollectStep(GarbageType.Organic), + new CollectStep(GarbageType.Paper), + new CollectStep(GarbageType.PlasticMetal) + }; + foreach (var item in collectSteps) + { + var copyCollector = (AGarbageCollector)collector.Clone(); + var copyGrid = CopyGrid(grid); + if (item.Invoke(copyCollector, copyGrid)) + result.Add(item); + } + } + if (grid[collector.Coords.X, collector.Coords.Y] is ADump) + { + var collectSteps = new List() + { + new SpillStep(GarbageType.Glass), + new SpillStep(GarbageType.Organic), + new SpillStep(GarbageType.Paper), + new SpillStep(GarbageType.PlasticMetal) + }; + foreach (var item in collectSteps) + { + var copyCollector = (AGarbageCollector)collector.Clone(); + var copyGrid = CopyGrid(grid); + if (item.Invoke(copyCollector, copyGrid)) + result.Add(item); + + + } + } + return result; + } + + KeyValuePair, int> SearchBfs(ContentManager content, GarbageCollector collector, ICloneable[,] grid, int length) + { + + //Thread.Sleep(1); + int count = 0; + + var nodes = new List, GarbageCollector, ICloneable[,]>>() { + new Tuple, GarbageCollector, ICloneable[,]>(new List(),collector,grid) + }; + while (true) + { + + Console.WriteLine(count); + Console.WriteLine(nodes.Count); + count++; + var nodes2 = new List, GarbageCollector, ICloneable[,]>>(); + foreach (var item in nodes) + { + Thread.Sleep(100); + this.Collector.Coords = item.Item2.Coords; + for (int x = 0; x < item.Item3.GetLength(0); x++) + { + for (int y = 0; y < item.Item3.GetLength(1); y++) + { + this.Grid[x, y] = item.Item3[x, y]; + } + } + + if (Houses.All(c => (item.Item3[c.X, c.Y] as IGarbageLocalization).TrashCans.All(j => j.FillPercent == 0.0)) + && + item.Item2.TrashContainers.All(i => i.FillPercent == 0.0) + ) + { + return new KeyValuePair, int>(item.Item1, length); + } + if (true)//item.Item2.Counter <= 12) + { + + + foreach (var step in PossibleSteps(item.Item2, item.Item3)) + { + var collectorClone = (GarbageCollector)item.Item2.Clone(); + var gridClone = CopyGrid(item.Item3); + step.Invoke(collectorClone, gridClone); + + + var steps = new List(); + steps.AddRange(item.Item1); + steps.Add(step); + nodes2.Add(new Tuple, GarbageCollector, ICloneable[,]>(steps, collectorClone, gridClone)); + } + } + + + } + nodes = nodes2; + } + } + + + + + + + + + + + + + + + + + + + + + + + private ICloneable[,] CopyGrid(ICloneable[,] grid) + { + ICloneable[,] result = new ICloneable[grid.GetLength(0), grid.GetLength(1)]; + for (int x = 0; x < grid.GetLength(0); x++) + { + for (int y = 0; y < grid.GetLength(1); y++) + { + result[x, y] = (ICloneable)grid[x, y].Clone(); + } + } + return result; + } } } + diff --git a/Trunk/MonoGameView/Game1.cs b/Trunk/MonoGameView/Game1.cs index cb0c4a7..c42765a 100644 --- a/Trunk/MonoGameView/Game1.cs +++ b/Trunk/MonoGameView/Game1.cs @@ -15,6 +15,7 @@ using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models; using CzokoŚmieciarka.MonoGameView.DataModels.Enums; using System.Threading.Tasks; using System.Threading; +using MonoGameView.Algorithms; namespace CzokoŚmieciarka.MonoGameView { @@ -59,7 +60,7 @@ namespace CzokoŚmieciarka.MonoGameView // TODO: Add your initialization logic here timer = 0f; - mapLoader.Load(out size,out grid,"map3.xml"); + mapLoader.Load(out size,out grid,"map1.xml"); var containers = new List() { new GarbageCollectorContainer( @@ -88,7 +89,7 @@ namespace CzokoŚmieciarka.MonoGameView stepN = 0; - var dfs = new DFS(collector,grid); + var dfs = new BFS(collector,grid); //steps = dfs.BestPath(Content, collector, grid); new Thread(delegate() { var x = dfs.BestPath(Content, collector, grid); diff --git a/Trunk/MonoGameView/MonoGameView.csproj b/Trunk/MonoGameView/MonoGameView.csproj index 3a82a42..a966011 100644 --- a/Trunk/MonoGameView/MonoGameView.csproj +++ b/Trunk/MonoGameView/MonoGameView.csproj @@ -42,6 +42,7 @@ app.manifest +