diff --git a/Trunk/Helper Programs/MapEditor/MapEditor/App.config b/Trunk/Helper Programs/MapEditor/MapEditor/App.config index faa5528..ea012ec 100644 --- a/Trunk/Helper Programs/MapEditor/MapEditor/App.config +++ b/Trunk/Helper Programs/MapEditor/MapEditor/App.config @@ -1,8 +1,8 @@  - - + + diff --git a/Trunk/MonoGameView/Algorithms/DFS.cs b/Trunk/MonoGameView/Algorithms/DFS.cs index 28f1fa8..094ff89 100644 --- a/Trunk/MonoGameView/Algorithms/DFS.cs +++ b/Trunk/MonoGameView/Algorithms/DFS.cs @@ -10,14 +10,18 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xna.Framework.Content; +using System.Threading; namespace CzokoŚmieciarka.MonoGameView.Algorithms { public class DFS { - public DFS() + public GarbageCollector Collector { get; set; } + public ICloneable[,] Grid { get; set; } + public DFS(GarbageCollector collector, ICloneable[,] grid) { - + this.Collector = collector; + this.Grid = grid; } int count = 0; public List Houses { get; set; } @@ -40,7 +44,7 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms var r=Search(content, collector, grid, 0).Key; - + Console.WriteLine($"Counts : {count}"); if (r == null) return new List(); return r; @@ -65,25 +69,22 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms foreach (var item in moveSteps) { var copyCollector = (AGarbageCollector)collector.Clone(); - try + if (item.Invoke(copyCollector, grid)) { - 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))) + 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); } } - catch (Exception e) - { - - } + + } } - if (grid[collector.Coords.X, collector.Coords.Y] is IGarbageLocalization) + if (grid[collector.Coords.X, collector.Coords.Y] is House) { var collectSteps = new List() { @@ -96,16 +97,8 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms { var copyCollector = (AGarbageCollector)collector.Clone(); var copyGrid = CopyGrid(grid); - try - { - item.Invoke(copyCollector, copyGrid); + if (item.Invoke(copyCollector, copyGrid)) result.Add(item); - - } - catch (Exception e) - { - - } } } if (grid[collector.Coords.X, collector.Coords.Y] is ADump) @@ -121,16 +114,10 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms { var copyCollector = (AGarbageCollector)collector.Clone(); var copyGrid = CopyGrid(grid); - try - { - item.Invoke(copyCollector, copyGrid); + if(item.Invoke(copyCollector, copyGrid)) result.Add(item); - } - catch (Exception e) - { - - } + } } return result; @@ -138,7 +125,21 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms KeyValuePair, int> Search(ContentManager content, GarbageCollector collector, ICloneable[,] grid, int length) { - if (length > 100) return new KeyValuePair, int>(null,length); + + //Thread.Sleep(1); + this.Collector.Coords = collector.Coords; + for (int x = 0; x < grid.GetLength(0); x++) + { + for (int y = 0; y < grid.GetLength(1); y++) + { + this.Grid[x, y] = grid[x, y]; + } + } + + Console.WriteLine(collector.HouseCounter); + + if (collector.Counter> 10 || length > 55) + return new KeyValuePair, int>(null,length); count++; if (Houses.All(c => (grid[c.X, c.Y] as IGarbageLocalization).TrashCans.All(j => j.FillPercent == 0.0)) && diff --git a/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/AGarbageCollector.cs b/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/AGarbageCollector.cs index b9a7758..1531481 100644 --- a/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/AGarbageCollector.cs +++ b/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/AGarbageCollector.cs @@ -12,50 +12,58 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector { public abstract class AGarbageCollector : IGarbageCollector, ICloneable { - public AGarbageCollector(Coords startPosition, IEnumerable trashContainers, int columns, int rows) + public AGarbageCollector(Coords startPosition, IEnumerable trashContainers, int columns, int rows,int houseCounter,int counter = 0) { this.columns = columns; this.rows = rows; this.Coords = startPosition; this.TrashContainers = trashContainers; + this.Counter = counter; + this.HouseCounter = houseCounter; } + public int Counter { get; set; } + public int HouseCounter { get; set; } public Coords Coords { get; set; } public int columns { get; set; } public int rows { get; set; } - public void MoveUp() + public bool MoveUp() { if(Coords.Y -1 < 0) { - throw new OutOfGridException(); + return false; } Coords.Y -= 1; + return true; } - public void MoveDown() + public bool MoveDown() { if (Coords.Y + 1 >= rows) { - throw new OutOfGridException(); + return false; } Coords.Y +=1; + return true; } - public void MoveLeft() + public bool MoveLeft() { if (Coords.X - 1 < 0) { - throw new OutOfGridException(); + return false; } Coords.X -= 1; + return true; } - public void MoveRight() + public bool MoveRight() { if (Coords.X + 1 >= columns) { - throw new OutOfGridException(); + return false; } Coords.X += 1; + return true; } diff --git a/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/IGarbageCollector.cs b/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/IGarbageCollector.cs index c705805..1cfbd4b 100644 --- a/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/IGarbageCollector.cs +++ b/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/IGarbageCollector.cs @@ -10,11 +10,13 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector { public interface IGarbageCollector : ICloneable { + int Counter { get; set; } + int HouseCounter { get; set; } Coords Coords { get; } - void MoveUp(); - void MoveDown(); - void MoveLeft(); - void MoveRight(); + bool MoveUp(); + bool MoveDown(); + bool MoveLeft(); + bool MoveRight(); IEnumerable TrashContainers { get; } } diff --git a/Trunk/MonoGameView/DataModels/Interfaces/IStep.cs b/Trunk/MonoGameView/DataModels/Interfaces/IStep.cs index f526bec..cc5c983 100644 --- a/Trunk/MonoGameView/DataModels/Interfaces/IStep.cs +++ b/Trunk/MonoGameView/DataModels/Interfaces/IStep.cs @@ -4,6 +4,6 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces { public interface IStep { - void Invoke(IGarbageCollector collector, object [,] grid); + bool Invoke(IGarbageCollector collector, object [,] grid); } } diff --git a/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/ATrashCan.cs b/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/ATrashCan.cs index 7ddd4a2..ea2a4c6 100644 --- a/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/ATrashCan.cs +++ b/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/ATrashCan.cs @@ -32,12 +32,12 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans public virtual void AddGarbage(AGarbage garbage) { - if (this.TypeOfGarbage.GarbageType != garbage.TypeOfGarbage.GarbageType) - throw new Exception("You cannot add up different type garbage!"); + //if (this.TypeOfGarbage.GarbageType != garbage.TypeOfGarbage.GarbageType) + // throw new Exception("You cannot add up different type garbage!"); var newGarbage = this.Garbage + garbage; - if (newGarbage.Volume > this.MaxVolume) - throw new Exception("Trash overload"); + //if (newGarbage.Volume > this.MaxVolume) + // throw new Exception("Trash overload"); this.Garbage = newGarbage; return; diff --git a/Trunk/MonoGameView/DataModels/Models/GarbageCollector.cs b/Trunk/MonoGameView/DataModels/Models/GarbageCollector.cs index 8738497..c8ffbd8 100644 --- a/Trunk/MonoGameView/DataModels/Models/GarbageCollector.cs +++ b/Trunk/MonoGameView/DataModels/Models/GarbageCollector.cs @@ -16,7 +16,7 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models { public class GarbageCollector : AGarbageCollector, IDrawables { - public GarbageCollector(Coords c, List l, int rows, int cols) : base(c,l,rows,cols) + public GarbageCollector(Coords c, List l, int rows, int cols,int houseCounter, int counter = 0) : base(c,l,rows,cols,houseCounter,counter) { } @@ -29,7 +29,7 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models public override object Clone() { var cloneList = TrashContainers.Select(x=>(GarbageCollectorContainer)x.Clone()).ToList(); - return new GarbageCollector((Coords)Coords.Clone(), cloneList, rows,columns); + return new GarbageCollector((Coords)Coords.Clone(), cloneList, rows,columns, HouseCounter,Counter); } } } diff --git a/Trunk/MonoGameView/DataModels/Models/Steps/CollectStep.cs b/Trunk/MonoGameView/DataModels/Models/Steps/CollectStep.cs index 42b1363..21085d8 100644 --- a/Trunk/MonoGameView/DataModels/Models/Steps/CollectStep.cs +++ b/Trunk/MonoGameView/DataModels/Models/Steps/CollectStep.cs @@ -21,18 +21,17 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps private GarbageType _typeOfGarbage; - public void Invoke(IGarbageCollector _garbageCollector, object [,] grid) + public bool Invoke(IGarbageCollector _garbageCollector, object [,] grid) { var _garbageLocalization = (IGarbageLocalization) grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y]; - if(_garbageCollector.Coords != _garbageLocalization.Coords) - throw new WrongPositionException("Śmieciarka nie jest w miejscu oderbania śmieci"); - + var trashCans = _garbageLocalization.TrashCans.Where(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage); + if (!trashCans.Any()) return false; var garbage = trashCans.Select(t => t.TakeGarbage()).Aggregate((a,b)=>a+b); if (_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage.GarbageType != _typeOfGarbage)) - throw new TrashContainerNotFound($"Nie znaleziono kontenera na {_typeOfGarbage}."); + return false; _garbageCollector.TrashContainers.First(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage).AddGarbage(garbage); @@ -43,7 +42,13 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps } } if (_garbageLocalization.TrashCans.All(x => x.FillPercent == 0.0)) + { grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y] = new EmptyHouse(new Coords(_garbageCollector.Coords.X, _garbageCollector.Coords.Y)); + + _garbageCollector.HouseCounter++; + } + _garbageCollector.Counter = 0; + return true; } } } diff --git a/Trunk/MonoGameView/DataModels/Models/Steps/MoveStep.cs b/Trunk/MonoGameView/DataModels/Models/Steps/MoveStep.cs index 4d5478b..1bd790c 100644 --- a/Trunk/MonoGameView/DataModels/Models/Steps/MoveStep.cs +++ b/Trunk/MonoGameView/DataModels/Models/Steps/MoveStep.cs @@ -20,17 +20,32 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps private Direction _direction; private IGarbageCollector _garbageCollector; - public void Invoke(IGarbageCollector _garbageCollector, object[,] grid) + public bool Invoke(IGarbageCollector _garbageCollector, object[,] grid) { if(grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y] is Road1) grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y] = new Road2(new Coords(_garbageCollector.Coords.X, _garbageCollector.Coords.Y)); + var pass = false; switch (_direction) { - case Direction.Up: _garbageCollector.MoveUp(); break; - case Direction.Down: _garbageCollector.MoveDown(); break; - case Direction.Left: _garbageCollector.MoveLeft(); break; - case Direction.Right: _garbageCollector.MoveRight(); break; + case Direction.Up: + pass = _garbageCollector.MoveDown(); + break; + + case Direction.Down: + pass = _garbageCollector.MoveRight(); + break; + case Direction.Left: + pass = _garbageCollector.MoveUp(); + break; + case Direction.Right: + pass = _garbageCollector.MoveLeft(); + break; } + if (pass) + { + _garbageCollector.Counter++; + } + return pass; } } } diff --git a/Trunk/MonoGameView/DataModels/Models/Steps/SpillStep.cs b/Trunk/MonoGameView/DataModels/Models/Steps/SpillStep.cs index f6bb52c..7028bcb 100644 --- a/Trunk/MonoGameView/DataModels/Models/Steps/SpillStep.cs +++ b/Trunk/MonoGameView/DataModels/Models/Steps/SpillStep.cs @@ -20,30 +20,32 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps private GarbageType _typeOfGarbage; - public void Invoke(IGarbageCollector _garbageCollector, object [,] grid) + public bool Invoke(IGarbageCollector _garbageCollector, object [,] grid) { - var _dump = (ADump)grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y]; - if(_garbageCollector.Coords != _dump.Coords) - throw new WrongPositionException("Śmieciarka nie na terenie podanego wyspiska"); - - if(_dump.TypeOfGarbage.GarbageType != _typeOfGarbage) - throw new TrashContainerNotFound($"Wysypisko nie przyjmuje smieci typu {_typeOfGarbage}"); + var _dump = (ADump)grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y]; - if(_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage.GarbageType != _typeOfGarbage)) - throw new TrashContainerNotFound($"Smieciarka nie ma pojemnika na {_typeOfGarbage}!"); + if (_dump.TypeOfGarbage.GarbageType != _typeOfGarbage) + return false; - var garbage = _garbageCollector.TrashContainers.Where(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage) - .Select(t => t.TakeGarbage()) - .Aggregate((a, b) => a + b); - - _dump.AddGarbage(garbage); - for (int x = 0; x < grid.GetLength(0); x++) + if (_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage.GarbageType != _typeOfGarbage)) + return false; + var containers = _garbageCollector.TrashContainers.Where(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage && t.FillPercent > 0); + if (containers.Any()) { - for (int y = 0; y < grid.GetLength(1); y++) + var garbage = containers.Select(t => t.TakeGarbage()).Aggregate((a, b) => a + b); + _dump.AddGarbage(garbage); + for (int x = 0; x < grid.GetLength(0); x++) { - if (grid[x, y] is Road2) grid[x, y] = new Road1(new Coords(x, y)); + for (int y = 0; y < grid.GetLength(1); y++) + { + if (grid[x, y] is Road2) grid[x, y] = new Road1(new Coords(x, y)); + } } + _garbageCollector.Counter = 0; + return true; } + return false; + } } } diff --git a/Trunk/MonoGameView/Game1.cs b/Trunk/MonoGameView/Game1.cs index a87418e..1a1e39d 100644 --- a/Trunk/MonoGameView/Game1.cs +++ b/Trunk/MonoGameView/Game1.cs @@ -13,6 +13,8 @@ using MonoGameView.DataModels; using MonoGameView.DataModels.Models; using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models; using CzokoŚmieciarka.MonoGameView.DataModels.Enums; +using System.Threading.Tasks; +using System.Threading; namespace CzokoŚmieciarka.MonoGameView { @@ -57,37 +59,38 @@ namespace CzokoŚmieciarka.MonoGameView // TODO: Add your initialization logic here timer = 0f; - mapLoader.Load(out size,out grid,"map.xml"); + mapLoader.Load(out size,out grid,"map3.xml"); var containers = new List() { new GarbageCollectorContainer( - new TypeOfGarbage(GarbageType.Glass,50,1), - 100, - new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,50,1),0) + new TypeOfGarbage(GarbageType.Glass,1), + 1000, + new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,1),0) ), new GarbageCollectorContainer( - new TypeOfGarbage(GarbageType.Paper,50,1), - 100, - new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,50,1),0) + new TypeOfGarbage(GarbageType.Paper,1), + 1000, + new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,1),0) ), new GarbageCollectorContainer( - new TypeOfGarbage(GarbageType.Organic,50,1), - 100, - new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,50,1),0) + new TypeOfGarbage(GarbageType.Organic,1), + 1000, + new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,1),0) ), new GarbageCollectorContainer( - new TypeOfGarbage(GarbageType.PlasticMetal,50,1), - 100, - new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,50,1),0) + new TypeOfGarbage(GarbageType.PlasticMetal,1), + 1000, + new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,1),0) ), }; - collector = new GarbageCollector(new Coords(0,0), containers, size, size); + collector = new GarbageCollector(new Coords(9,9), containers, size, size,0); stepN = 0; - var dfs = new DFS(); - steps = dfs.BestPath(Content, collector, grid); + var dfs = new DFS(collector,grid); + //steps = dfs.BestPath(Content, collector, grid); + new Thread(delegate() { dfs.BestPath(Content, collector, grid); }).Start(); base.Initialize(); } @@ -123,6 +126,7 @@ namespace CzokoŚmieciarka.MonoGameView { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); + /* var elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; timer -= elapsed; if (timer<0) @@ -134,6 +138,7 @@ namespace CzokoŚmieciarka.MonoGameView steps.RemoveAt(0); } } + */ // TODO: Add your update logic here base.Update(gameTime); diff --git a/Trunk/MonoGameView/map3.bmp b/Trunk/MonoGameView/map3.bmp new file mode 100644 index 0000000..8ce6e6f Binary files /dev/null and b/Trunk/MonoGameView/map3.bmp differ