diff --git a/Trunk/Helper Programs/MapEditor/MapEditor/App.config b/Trunk/Helper Programs/MapEditor/MapEditor/App.config index 9133650..77d206f 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/Helper Programs/MapEditor/MapEditor/Helpers/ColorToFieldTypeConverter.cs b/Trunk/Helper Programs/MapEditor/MapEditor/Helpers/ColorToFieldTypeConverter.cs index 7a880a6..a951ee1 100644 --- a/Trunk/Helper Programs/MapEditor/MapEditor/Helpers/ColorToFieldTypeConverter.cs +++ b/Trunk/Helper Programs/MapEditor/MapEditor/Helpers/ColorToFieldTypeConverter.cs @@ -22,7 +22,7 @@ namespace MapEditor.Helpers return FieldType.House; if (color == Color.FromArgb(255,255,0)) - return FieldType.House; + return FieldType.Dump; throw new NotImplementedException($"Conversion form {color.ToKnownColor()} has not been implemented."); diff --git a/Trunk/MonoGameView/Algorithms/DFS.cs b/Trunk/MonoGameView/Algorithms/DFS.cs index fd67bfc..2efbc2c 100644 --- a/Trunk/MonoGameView/Algorithms/DFS.cs +++ b/Trunk/MonoGameView/Algorithms/DFS.cs @@ -17,19 +17,36 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms { public DFS() { + } int count = 0; + public List Houses { get; set; } - - public List BestPath(ContentManager content, AGarbageCollector collector, object[,] grid) + 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=Search(content, collector, grid, 0).Key; - Console.WriteLine(count); + if (r == null) return new List(); return r; + } - List PossibleSteps(ContentManager content, AGarbageCollector collector, object[,] grid) + List PossibleSteps(ContentManager content, AGarbageCollector collector, ICloneable[,] grid) { @@ -44,14 +61,13 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms var filteredMoveSteps = new List(); foreach (var item in moveSteps) { - var copyCollector = (AGarbageCollector)collector.Clone(content); - var copyGrid = (object[,])grid.Clone(); + var copyCollector = (AGarbageCollector)collector.Clone(); try { item.Invoke(copyCollector, grid); var gcx = copyCollector.Coords.X; var gcy = copyCollector.Coords.Y; - if (grid[gcx, gcy] is IRoad1 || grid[gcx, gcy] is IGarbageLocalization || grid[gcx, gcy] is ADump) + if (grid[gcx, gcy] is Road1 || grid[gcx, gcy] is House || grid[gcx, gcy] is ADump) { result.Add(item); } @@ -61,21 +77,78 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms } } + if (grid[collector.Coords.X, collector.Coords.Y] is IGarbageLocalization) + { + 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); + try + { + item.Invoke(copyCollector, copyGrid); + result.Add(item); + } + catch (Exception e) + { + + } + } + } + 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); + try + { + item.Invoke(copyCollector, copyGrid); + result.Add(item); + + } + catch (Exception e) + { + + } + } + } return result; } - KeyValuePair, int> Search(ContentManager content, AGarbageCollector collector, object[,] grid, int length) + KeyValuePair, int> Search(ContentManager content, GarbageCollector collector, ICloneable[,] grid, int length) { + if (length > 200) return new KeyValuePair, int>(null,length); count++; - if (length > 40) return new KeyValuePair, int>(new List(), length); - var possibleSteps = PossibleSteps(content, collector, grid); + if (Houses.All(c => (grid[c.X, c.Y] as IGarbageLocalization).TrashCans.All(j => j.FillPercent == 0.0)) + && + collector.TrashContainers.All(i => i.FillPercent == 0.0) + ) + + { + return new KeyValuePair, int>(new List(), length); + } + var possibleSteps = PossibleSteps(content, collector, grid); foreach (var item in possibleSteps) { - var copyCollector = (AGarbageCollector)collector.Clone(content); - var copyGrid = (object[,])grid.Clone(); - + var copyCollector = (GarbageCollector)collector.Clone(); + var copyGrid = CopyGrid(grid); + //if (copyGrid[copyCollector.Coords.X, copyCollector.Coords.Y] is IRoad1) copyGrid[copyCollector.Coords.X, copyCollector.Coords.Y] = new Road2(); item.Invoke(copyCollector, copyGrid); @@ -102,5 +175,17 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms } + 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/DataModels/Interfaces/Garbage/AGarbage.cs b/Trunk/MonoGameView/DataModels/Interfaces/Garbage/AGarbage.cs index 3c71b29..53a13b0 100644 --- a/Trunk/MonoGameView/DataModels/Interfaces/Garbage/AGarbage.cs +++ b/Trunk/MonoGameView/DataModels/Interfaces/Garbage/AGarbage.cs @@ -8,7 +8,7 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage { public abstract class AGarbage : IGarbage { - protected AGarbage(ITypeOfGarbage typeOfGarbage, int weight) + protected AGarbage(ITypeOfGarbage typeOfGarbage, double weight) { this.TypeOfGarbage = typeOfGarbage; this.Weight = weight; @@ -16,11 +16,11 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage public ITypeOfGarbage TypeOfGarbage { get; } - public virtual int Weight { get; set; } + public virtual double Weight { get; set; } - public virtual int Volume + public virtual double Volume { - get { return this.Weight / TypeOfGarbage.Density; } + get { return (double) this.Weight / TypeOfGarbage.Density; } } protected abstract AGarbage Add(AGarbage garbageToAdd); @@ -40,9 +40,9 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage #endregion - public object Clone() + public virtual object Clone() { - return this.MemberwiseClone(); + return new NotImplementedException("xD"); } } } diff --git a/Trunk/MonoGameView/DataModels/Interfaces/Garbage/IGarbage.cs b/Trunk/MonoGameView/DataModels/Interfaces/Garbage/IGarbage.cs index 097f8b7..6135e71 100644 --- a/Trunk/MonoGameView/DataModels/Interfaces/Garbage/IGarbage.cs +++ b/Trunk/MonoGameView/DataModels/Interfaces/Garbage/IGarbage.cs @@ -10,8 +10,8 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage { ITypeOfGarbage TypeOfGarbage { get; } - int Weight { get; } + double Weight { get; } - int Volume { get; } + double Volume { get; } } } diff --git a/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/AGarbageCollector.cs b/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/AGarbageCollector.cs index 4c0d953..b9a7758 100644 --- a/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/AGarbageCollector.cs +++ b/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/AGarbageCollector.cs @@ -57,14 +57,10 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector } Coords.X += 1; } - - public virtual object Clone(ContentManager content) - { - throw new NotImplementedException(); - } + public IEnumerable TrashContainers { get; } - public object Clone() + public virtual object Clone() { throw new NotImplementedException(); } diff --git a/Trunk/MonoGameView/DataModels/Interfaces/IDrawables.cs b/Trunk/MonoGameView/DataModels/Interfaces/IDrawables.cs index 8d02b5f..16b886d 100644 --- a/Trunk/MonoGameView/DataModels/Interfaces/IDrawables.cs +++ b/Trunk/MonoGameView/DataModels/Interfaces/IDrawables.cs @@ -1,9 +1,10 @@ using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; +using System; namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces { - public interface IDrawables + public interface IDrawables : ICloneable { void Draw(SpriteBatch spriteBatch, int size); } diff --git a/Trunk/MonoGameView/DataModels/Interfaces/ITypeOfGarbage.cs b/Trunk/MonoGameView/DataModels/Interfaces/ITypeOfGarbage.cs index f7e6e85..c2ad9a9 100644 --- a/Trunk/MonoGameView/DataModels/Interfaces/ITypeOfGarbage.cs +++ b/Trunk/MonoGameView/DataModels/Interfaces/ITypeOfGarbage.cs @@ -7,7 +7,7 @@ using CzokoŚmieciarka.MonoGameView.DataModels.Enums; namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces { - public interface ITypeOfGarbage + public interface ITypeOfGarbage : ICloneable { GarbageType GarbageType { get; } diff --git a/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/AGarbageCollectorContainer.cs b/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/AGarbageCollectorContainer.cs index de7a40c..884297e 100644 --- a/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/AGarbageCollectorContainer.cs +++ b/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/AGarbageCollectorContainer.cs @@ -1,8 +1,10 @@ -namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage; + +namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans { public abstract class AGarbageCollectorContainer : ATrashCan { - protected AGarbageCollectorContainer(ITypeOfGarbage typeOfGarbage, int maxVolume) : base(typeOfGarbage, maxVolume) + protected AGarbageCollectorContainer(ITypeOfGarbage typeOfGarbage, int maxVolume, AGarbage garbage) : base(typeOfGarbage, maxVolume,garbage) { } diff --git a/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/ATrashCan.cs b/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/ATrashCan.cs index 5df8a4b..7ddd4a2 100644 --- a/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/ATrashCan.cs +++ b/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/ATrashCan.cs @@ -1,14 +1,16 @@ using System; +using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models; using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage; namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans { - public abstract class ATrashCan + public abstract class ATrashCan : ICloneable { protected ATrashCan(ITypeOfGarbage typeOfGarbage, int maxVolume) { this.MaxVolume = maxVolume; this.TypeOfGarbage = typeOfGarbage; + Garbage = new BasicGarbage(typeOfGarbage, 0); } protected ATrashCan(ITypeOfGarbage typeOfGarbage, int maxVolume, AGarbage garbage) { @@ -23,26 +25,34 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans public AGarbage Garbage { get; private set; } - public int FillPercent + public double FillPercent { - get { return Garbage.Volume / this.MaxVolume; } + get { return ((double)Garbage.Volume) / this.MaxVolume; } } - public virtual bool AddGarbage(IGarbage garbage) + public virtual void AddGarbage(AGarbage garbage) { - if (this.TypeOfGarbage != garbage.TypeOfGarbage) + if (this.TypeOfGarbage.GarbageType != garbage.TypeOfGarbage.GarbageType) throw new Exception("You cannot add up different type garbage!"); - var newGarbage = this.Garbage + Garbage; + var newGarbage = this.Garbage + garbage; if (newGarbage.Volume > this.MaxVolume) - return false; + throw new Exception("Trash overload"); this.Garbage = newGarbage; - return true; + return; + } + + public virtual object Clone() + { + throw new NotImplementedException(); } public virtual AGarbage TakeGarbage() { + + if (this.Garbage.Weight == 0) + throw new Exception("Pusty śmietnik matole"); var result = (AGarbage)this.Garbage.Clone(); this.Garbage.Weight = 0; return result; diff --git a/Trunk/MonoGameView/DataModels/MapLoader.cs b/Trunk/MonoGameView/DataModels/MapLoader.cs index 29f5fd2..581d885 100644 --- a/Trunk/MonoGameView/DataModels/MapLoader.cs +++ b/Trunk/MonoGameView/DataModels/MapLoader.cs @@ -1,114 +1,115 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Xml; -using CzokoŚmieciarka.MonoGameView.DataModels.Enums; -using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models; -using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; -using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans; -using CzokoŚmieciarka.MonoGameView.DataModels.Models; -using MonoGameView.DataModels.Models; - -namespace MonoGameView.DataModels -{ - public class MapLoader - { - public void Load(out int size, out IDrawables[,] grid, string filename) - { - XmlDocument xml = new XmlDocument(); - xml.Load(filename); - XmlNode node = xml.GetElementsByTagName("Map").Item(0); - XmlNode sizeNode = node.SelectSingleNode("/Map/Size"); - size = Convert.ToInt32(sizeNode.InnerText); - grid = new IDrawables[size,size]; - for (int x = 0; x < size; x++) - { - for (int y = 0; y < size; y++) - { - grid[x, y] = new Grass(new Coords(x,y)); - } - } - foreach(XmlNode objectNode in node.SelectNodes("/Map/Objects/Object")) - { - XmlNode positionNode; - int x; - int y; - switch (objectNode.SelectSingleNode("Type").InnerText) - { - case "Road": - positionNode = objectNode.SelectSingleNode("Position"); - x = Convert.ToInt32(positionNode.SelectSingleNode("X").InnerText); - y = Convert.ToInt32(positionNode.SelectSingleNode("Y").InnerText); - Road1 road = new Road1(new Coords(x,y)); - grid[x, y] = road; - break; - case "House": - positionNode = objectNode.SelectSingleNode("Position"); - x = Convert.ToInt32(positionNode.SelectSingleNode("X").InnerText); - y = Convert.ToInt32(positionNode.SelectSingleNode("Y").InnerText); - List trashCans = new List(); - foreach (XmlNode trashCanNode in objectNode.SelectSingleNode("TrashCans")) - { - GarbageType type; - switch (trashCanNode.SelectSingleNode("GarbageType").InnerText) - { - case "Glass": - type = GarbageType.Glass; - break; - case "Organic": - type = GarbageType.Organic; - break; - case "Paper": - type = GarbageType.Paper; - break; - default: - type = GarbageType.PlasticMetal; - break; - } - int canDensity = Convert.ToInt32(trashCanNode.SelectSingleNode("Density").InnerText); - int canProcessingTimePerUnit = Convert.ToInt32(trashCanNode.SelectSingleNode("ProcessingTimePerUnit").InnerText); +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using CzokoŚmieciarka.MonoGameView.DataModels.Enums; +using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models; +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans; +using CzokoŚmieciarka.MonoGameView.DataModels.Models; +using MonoGameView.DataModels.Models; + +namespace MonoGameView.DataModels +{ + public class MapLoader + { + public void Load(out int size, out IDrawables[,] grid, string filename) + { + XmlDocument xml = new XmlDocument(); + xml.Load(filename); + XmlNode node = xml.GetElementsByTagName("Map").Item(0); + XmlNode sizeNode = node.SelectSingleNode("/Map/Size"); + size = Convert.ToInt32(sizeNode.InnerText); + grid = new IDrawables[size,size]; + for (int x = 0; x < size; x++) + { + for (int y = 0; y < size; y++) + { + grid[x, y] = new Grass(new Coords(x,y)); + } + } + foreach(XmlNode objectNode in node.SelectNodes("/Map/Objects/Object")) + { + XmlNode positionNode; + int x; + int y; + var dupa = objectNode.SelectSingleNode("Type").InnerText.Trim(); + switch (dupa) + { + case "Road": + positionNode = objectNode.SelectSingleNode("Position"); + x = Convert.ToInt32(positionNode.SelectSingleNode("X").InnerText); + y = Convert.ToInt32(positionNode.SelectSingleNode("Y").InnerText); + Road1 road = new Road1(new Coords(x,y)); + grid[x, y] = road; + break; + case "House": + positionNode = objectNode.SelectSingleNode("Position"); + x = Convert.ToInt32(positionNode.SelectSingleNode("X").InnerText); + y = Convert.ToInt32(positionNode.SelectSingleNode("Y").InnerText); + List trashCans = new List(); + foreach (XmlNode trashCanNode in objectNode.SelectNodes("TrashCans/Can")) + { + GarbageType type; + switch (trashCanNode.SelectSingleNode("GarbageType").InnerText.Trim()) + { + case "Glass": + type = GarbageType.Glass; + break; + case "Organic": + type = GarbageType.Organic; + break; + case "Paper": + type = GarbageType.Paper; + break; + default: + type = GarbageType.PlasticMetal; + break; + } + int canDensity = Convert.ToInt32(trashCanNode.SelectSingleNode("Density").InnerText); + int canProcessingTimePerUnit = Convert.ToInt32(trashCanNode.SelectSingleNode("ProcessingTimePerUnit").InnerText); int volume = Convert.ToInt32(trashCanNode.SelectSingleNode("Volume").InnerText); - TypeOfGarbage canTypeOfGarbage = new TypeOfGarbage(type, canDensity,canProcessingTimePerUnit); - TrashCan trashCan = new TrashCan(canTypeOfGarbage, volume); - trashCans.Add(trashCan); - } - House house = new House(new Coords(x,y), trashCans); - grid[x, y] = house; - break; - case "Dump": - positionNode = objectNode.SelectSingleNode("Position"); - x = Convert.ToInt32(positionNode.SelectSingleNode("X").InnerText); - y = Convert.ToInt32(positionNode.SelectSingleNode("Y").InnerText); - XmlNode garbageNode = objectNode.SelectSingleNode("Garbage"); - TypeOfGarbage typeOfGarbage; - GarbageType garbageType; - switch (garbageNode.SelectSingleNode("GarbageType").InnerText) - { - case "Glass": - garbageType = GarbageType.Glass; - break; - case "Organic": - garbageType = GarbageType.Organic; - break; - case "Paper": - garbageType = GarbageType.Paper; - break; - default: - garbageType = GarbageType.PlasticMetal; - break; - } - - int density = Convert.ToInt32(garbageNode.SelectSingleNode("Density").InnerText); - int processingTimePerUnit = Convert.ToInt32(garbageNode.SelectSingleNode("ProcessingTimePerUnit").InnerText); + TypeOfGarbage canTypeOfGarbage = new TypeOfGarbage(type, canDensity,canProcessingTimePerUnit); + TrashCan trashCan = new TrashCan(canTypeOfGarbage, volume, new BasicGarbage(canTypeOfGarbage, volume)); + trashCans.Add(trashCan); + } + House house = new House(new Coords(x,y), trashCans); + grid[x, y] = house; + break; + case "Dump": + positionNode = objectNode.SelectSingleNode("Position"); + x = Convert.ToInt32(positionNode.SelectSingleNode("X").InnerText); + y = Convert.ToInt32(positionNode.SelectSingleNode("Y").InnerText); + XmlNode garbageNode = objectNode.SelectSingleNode("Garbage"); + TypeOfGarbage typeOfGarbage; + GarbageType garbageType; + switch (garbageNode.SelectSingleNode("GarbageType").InnerText.Trim()) + { + case "Glass": + garbageType = GarbageType.Glass; + break; + case "Organic": + garbageType = GarbageType.Organic; + break; + case "Paper": + garbageType = GarbageType.Paper; + break; + default: + garbageType = GarbageType.PlasticMetal; + break; + } + + int density = Convert.ToInt32(garbageNode.SelectSingleNode("Density").InnerText); + int processingTimePerUnit = Convert.ToInt32(garbageNode.SelectSingleNode("ProcessingTimePerUnit").InnerText); int maxVolume = Convert.ToInt32(objectNode.SelectSingleNode("Volume").InnerText); - typeOfGarbage = new TypeOfGarbage(garbageType, density, processingTimePerUnit); - Dump dump = new Dump(typeOfGarbage, maxVolume, new Coords(x, y)); - grid[x, y] = dump; - break; - } - } - } - } -} + typeOfGarbage = new TypeOfGarbage(garbageType, density, processingTimePerUnit); + Dump dump = new Dump(typeOfGarbage, maxVolume, new Coords(x, y)); + grid[x, y] = dump; + break; + } + } + } + } +} diff --git a/Trunk/MonoGameView/DataModels/Models/BasicGarbage.cs b/Trunk/MonoGameView/DataModels/Models/BasicGarbage.cs new file mode 100644 index 0000000..bfae64f --- /dev/null +++ b/Trunk/MonoGameView/DataModels/Models/BasicGarbage.cs @@ -0,0 +1,31 @@ +using System; +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage; + +namespace CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models +{ + public class BasicGarbage : AGarbage + { + public BasicGarbage(ITypeOfGarbage typeOfGarbage, double weight) : base(typeOfGarbage, weight) + { + } + + protected override AGarbage Add(AGarbage garbageToAdd) + { + return new BasicGarbage(TypeOfGarbage, Weight + garbageToAdd.Weight); + + } + + protected override AGarbage Subtract(AGarbage garbageToSubtract) + { + return new BasicGarbage(TypeOfGarbage, Weight - garbageToSubtract.Weight); + + } + + + public override object Clone() + { + return new BasicGarbage((ITypeOfGarbage)TypeOfGarbage.Clone(), Weight); + } + } +} diff --git a/Trunk/MonoGameView/DataModels/Models/Coords.cs b/Trunk/MonoGameView/DataModels/Models/Coords.cs index 8d27913..ab3b9b2 100644 --- a/Trunk/MonoGameView/DataModels/Models/Coords.cs +++ b/Trunk/MonoGameView/DataModels/Models/Coords.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace CzokoŚmieciarka.MonoGameView.DataModels.Models { - public class Coords + public class Coords : ICloneable { public Coords(int x,int y) { @@ -50,5 +50,10 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models { return base.GetHashCode(); } + + public object Clone() + { + return new Coords(X, Y); + } } } diff --git a/Trunk/MonoGameView/DataModels/Models/Dump.cs b/Trunk/MonoGameView/DataModels/Models/Dump.cs index 585134a..e459af1 100644 --- a/Trunk/MonoGameView/DataModels/Models/Dump.cs +++ b/Trunk/MonoGameView/DataModels/Models/Dump.cs @@ -17,5 +17,11 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models { batch.Draw(ImageContainer.GetImage(TypeOfGarbage.GarbageType.ToString()), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White); } + + public override object Clone() + { + return new Dump((ITypeOfGarbage)TypeOfGarbage.Clone(), MaxVolume, (Coords)Coords.Clone()); + + } } } diff --git a/Trunk/MonoGameView/DataModels/Models/EmptyHouse.cs b/Trunk/MonoGameView/DataModels/Models/EmptyHouse.cs new file mode 100644 index 0000000..ff90e68 --- /dev/null +++ b/Trunk/MonoGameView/DataModels/Models/EmptyHouse.cs @@ -0,0 +1,37 @@ +using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models; +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans; +using CzokoŚmieciarka.MonoGameView.DataModels.Models; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MonoGameView.DataModels.Models +{ + public class EmptyHouse : IDrawables, IGarbageLocalization, ICloneable + { + public Coords Coords { get; } + + public EmptyHouse(Coords coords) + { + Coords = coords; + TrashCans = new List(); + } + public void Draw(SpriteBatch batch, int size) + { + batch.Draw(ImageContainer.GetImage("road2"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White); + } + + public object Clone() + { + return new House((Coords)Coords.Clone(), new List()); + } + + public IEnumerable TrashCans { get; set; } + } + +} diff --git a/Trunk/MonoGameView/DataModels/Models/Garbage.cs b/Trunk/MonoGameView/DataModels/Models/Garbage.cs deleted file mode 100644 index 165d1a2..0000000 --- a/Trunk/MonoGameView/DataModels/Models/Garbage.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; -using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage; - -namespace CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models -{ - public class Garbage : AGarbage - { - public Garbage(ITypeOfGarbage typeOfGarbage, int weight) : base(typeOfGarbage, weight) - { - } - - protected override AGarbage Add(AGarbage garbageToAdd) - { - throw new NotImplementedException(); - - } - - protected override AGarbage Subtract(AGarbage garbageToSubtract) - { - throw new NotImplementedException(); - } - } -} diff --git a/Trunk/MonoGameView/DataModels/Models/GarbageCollector.cs b/Trunk/MonoGameView/DataModels/Models/GarbageCollector.cs index b22d054..8738497 100644 --- a/Trunk/MonoGameView/DataModels/Models/GarbageCollector.cs +++ b/Trunk/MonoGameView/DataModels/Models/GarbageCollector.cs @@ -1,34 +1,35 @@ -using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector; -using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans; -using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Content; -using Microsoft.Xna.Framework.Graphics; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector; +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans; +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; using MonoGameView.DataModels.Models; +using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models; -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 void Draw(SpriteBatch batch,int size) - { - batch.Draw(ImageContainer.GetImage("collector"), new Rectangle(Coords.X*500/size, Coords.Y*500/size, 500/size, 500/size), Color.White); - } - - - public override object Clone(ContentManager content) - { - var cloneList = new List(); - return new GarbageCollector(new Coords(Coords.X,Coords.Y), cloneList, rows,columns); - } - } -} +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 void Draw(SpriteBatch batch,int size) + { + batch.Draw(ImageContainer.GetImage("collector"), new Rectangle(Coords.X*500/size, Coords.Y*500/size, 500/size, 500/size), Color.White); + } + + + public override object Clone() + { + var cloneList = TrashContainers.Select(x=>(GarbageCollectorContainer)x.Clone()).ToList(); + return new GarbageCollector((Coords)Coords.Clone(), cloneList, rows,columns); + } + } +} diff --git a/Trunk/MonoGameView/DataModels/Models/GarbageCollectorContainer.cs b/Trunk/MonoGameView/DataModels/Models/GarbageCollectorContainer.cs index 7f0fbc3..20eedff 100644 --- a/Trunk/MonoGameView/DataModels/Models/GarbageCollectorContainer.cs +++ b/Trunk/MonoGameView/DataModels/Models/GarbageCollectorContainer.cs @@ -5,8 +5,13 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models { public class GarbageCollectorContainer : AGarbageCollectorContainer { - public GarbageCollectorContainer(ITypeOfGarbage typeOfGarbage, int maxVolume) : base(typeOfGarbage, maxVolume) + public GarbageCollectorContainer(TypeOfGarbage typeOfGarbage, int maxVolume, BasicGarbage garbage) : base(typeOfGarbage, maxVolume,garbage) { } + + public override object Clone() + { + return new GarbageCollectorContainer((TypeOfGarbage)TypeOfGarbage.Clone(), MaxVolume,(BasicGarbage)Garbage.Clone()); + } } } diff --git a/Trunk/MonoGameView/DataModels/Models/Grass.cs b/Trunk/MonoGameView/DataModels/Models/Grass.cs index 180fbcc..53ec9cc 100644 --- a/Trunk/MonoGameView/DataModels/Models/Grass.cs +++ b/Trunk/MonoGameView/DataModels/Models/Grass.cs @@ -20,9 +20,16 @@ namespace MonoGameView.DataModels.Models Coords = coords; } + public object Clone() + { + return new Grass((Coords)Coords.Clone()); + } + public void Draw(SpriteBatch batch, int size) { batch.Draw(ImageContainer.GetImage("grass"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White); } + + } } diff --git a/Trunk/MonoGameView/DataModels/Models/House.cs b/Trunk/MonoGameView/DataModels/Models/House.cs index a7a38d6..5568c56 100644 --- a/Trunk/MonoGameView/DataModels/Models/House.cs +++ b/Trunk/MonoGameView/DataModels/Models/House.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models; using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans; using Microsoft.Xna.Framework; @@ -12,19 +13,25 @@ using MonoGameView.DataModels.Models; namespace CzokoŚmieciarka.MonoGameView.DataModels.Models { - public class House : IDrawables, IGarbageLocalization + public class House : IDrawables, IGarbageLocalization, ICloneable { public Coords Coords { get; } - public House(Coords coords, IEnumerable trashCans) + public House(Coords coords, IEnumerable trashCans) { Coords = coords; + TrashCans = trashCans; } public void Draw(SpriteBatch batch, int size) { batch.Draw(ImageContainer.GetImage("house"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White); } + public object Clone() + { + return new House((Coords) Coords.Clone(), TrashCans.Select(x => (TrashCan) x.Clone()).ToList()); + } + public IEnumerable TrashCans { get; set; } } } diff --git a/Trunk/MonoGameView/DataModels/Models/Road1.cs b/Trunk/MonoGameView/DataModels/Models/Road1.cs index 8996763..e1f7e8e 100644 --- a/Trunk/MonoGameView/DataModels/Models/Road1.cs +++ b/Trunk/MonoGameView/DataModels/Models/Road1.cs @@ -20,6 +20,11 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models Coords = coords; } + public object Clone() + { + return new Road1((Coords)Coords.Clone()); + } + public void Draw(SpriteBatch batch, int size) { batch.Draw(ImageContainer.GetImage("road1"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White); diff --git a/Trunk/MonoGameView/DataModels/Models/Road2.cs b/Trunk/MonoGameView/DataModels/Models/Road2.cs index 01adf2b..0b4c7c6 100644 --- a/Trunk/MonoGameView/DataModels/Models/Road2.cs +++ b/Trunk/MonoGameView/DataModels/Models/Road2.cs @@ -17,8 +17,13 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models public Road2(Coords coords) { Coords = coords; - } - + } + + public object Clone() + { + return new Road2((Coords)Coords.Clone()); + } + public void Draw(SpriteBatch batch, int size) { batch.Draw(ImageContainer.GetImage("road2"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White); diff --git a/Trunk/MonoGameView/DataModels/Models/Steps/CollectStep.cs b/Trunk/MonoGameView/DataModels/Models/Steps/CollectStep.cs index 75ebfe7..42b1363 100644 --- a/Trunk/MonoGameView/DataModels/Models/Steps/CollectStep.cs +++ b/Trunk/MonoGameView/DataModels/Models/Steps/CollectStep.cs @@ -3,35 +3,47 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using CzokoŚmieciarka.MonoGameView.DataModels.Enums; +using CzokoŚmieciarka.MonoGameView.DataModels.Exceptions; using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector; using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans; +using MonoGameView.DataModels.Models; namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps { public class CollectStep : IStep { - public CollectStep(ITypeOfGarbage typeOfGarbage) + public CollectStep(GarbageType typeOfGarbage) { this._typeOfGarbage = typeOfGarbage; } - private ITypeOfGarbage _typeOfGarbage; + private GarbageType _typeOfGarbage; - public void Invoke(IGarbageCollector garbageCollector, object [,] grid) + public void 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 == _typeOfGarbage); + var trashCans = _garbageLocalization.TrashCans.Where(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage); var garbage = trashCans.Select(t => t.TakeGarbage()).Aggregate((a,b)=>a+b); - if (_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage != _typeOfGarbage)) - throw new TrashContainerNotFound($"Nie znaleziono kontenera na {_typeOfGarbage.GarbageType}."); + if (_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage.GarbageType != _typeOfGarbage)) + throw new TrashContainerNotFound($"Nie znaleziono kontenera na {_typeOfGarbage}."); - _garbageCollector.TrashContainers.First(t => t.TypeOfGarbage == _typeOfGarbage).AddGarbage(garbage); - */ + _garbageCollector.TrashContainers.First(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage).AddGarbage(garbage); + + for (int x= 0;x < grid.GetLength(0); x++){ + for (int y = 0; y < grid.GetLength(1); y++) + { + if (grid[x, y] is Road2) grid[x, y] = new Road1(new Coords(x,y)); + } + } + 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)); } } } diff --git a/Trunk/MonoGameView/DataModels/Models/Steps/SpillStep.cs b/Trunk/MonoGameView/DataModels/Models/Steps/SpillStep.cs index d259717..f6bb52c 100644 --- a/Trunk/MonoGameView/DataModels/Models/Steps/SpillStep.cs +++ b/Trunk/MonoGameView/DataModels/Models/Steps/SpillStep.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using CzokoŚmieciarka.MonoGameView.DataModels.Enums; +using CzokoŚmieciarka.MonoGameView.DataModels.Exceptions; using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector; using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans; @@ -11,29 +13,37 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps { public class SpillStep : IStep { - public SpillStep(ITypeOfGarbage typeOfGarbage) + public SpillStep(GarbageType typeOfGarbage) { this._typeOfGarbage = typeOfGarbage; } - private ITypeOfGarbage _typeOfGarbage; + private GarbageType _typeOfGarbage; - public void Invoke(IGarbageCollector garbageCollector, object [,] grid) - {/* + public void 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 != _typeOfGarbage) - throw new TrashContainerNotFound($"Wysypisko nie przyjmuje smieci typu {_typeOfGarbage.GarbageType}"); + if(_dump.TypeOfGarbage.GarbageType != _typeOfGarbage) + throw new TrashContainerNotFound($"Wysypisko nie przyjmuje smieci typu {_typeOfGarbage}"); - if(_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage != _typeOfGarbage)) - throw new TrashContainerNotFound($"Smieciarka nie ma pojemnika na {_typeOfGarbage.GarbageType}!"); + if(_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage.GarbageType != _typeOfGarbage)) + throw new TrashContainerNotFound($"Smieciarka nie ma pojemnika na {_typeOfGarbage}!"); - var garbage = _garbageCollector.TrashContainers.Where(t => t.TypeOfGarbage == _typeOfGarbage) + var garbage = _garbageCollector.TrashContainers.Where(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage) .Select(t => t.TakeGarbage()) .Aggregate((a, b) => a + b); - _dump.AddGarbage(garbage);*/ + _dump.AddGarbage(garbage); + for (int x = 0; x < grid.GetLength(0); x++) + { + for (int y = 0; y < grid.GetLength(1); y++) + { + if (grid[x, y] is Road2) grid[x, y] = new Road1(new Coords(x, y)); + } + } } } } diff --git a/Trunk/MonoGameView/DataModels/Models/TrashCan.cs b/Trunk/MonoGameView/DataModels/Models/TrashCan.cs index 74a0637..68b4d97 100644 --- a/Trunk/MonoGameView/DataModels/Models/TrashCan.cs +++ b/Trunk/MonoGameView/DataModels/Models/TrashCan.cs @@ -1,4 +1,5 @@ using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage; using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans; namespace CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models @@ -8,8 +9,13 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models public TrashCan(ITypeOfGarbage typeOfGarbage, int maxVolume) : base(typeOfGarbage, maxVolume) { } - public TrashCan(ITypeOfGarbage typeOfGarbage, int maxVolume, Garbage garbage) : base(typeOfGarbage, maxVolume, garbage) + public TrashCan(ITypeOfGarbage typeOfGarbage, int maxVolume, BasicGarbage garbage) : base(typeOfGarbage, maxVolume, garbage) { } + + public override object Clone() + { + return new TrashCan((TypeOfGarbage) TypeOfGarbage.Clone(), MaxVolume, (BasicGarbage) Garbage.Clone()); + } } } diff --git a/Trunk/MonoGameView/DataModels/Models/TypeOfGarbage.cs b/Trunk/MonoGameView/DataModels/Models/TypeOfGarbage.cs index c942553..8303377 100644 --- a/Trunk/MonoGameView/DataModels/Models/TypeOfGarbage.cs +++ b/Trunk/MonoGameView/DataModels/Models/TypeOfGarbage.cs @@ -15,5 +15,10 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models public GarbageType GarbageType { get; } public int ProcessingTimePerUnit { get; } public int Density { get; } + + public object Clone() + { + return new TypeOfGarbage(GarbageType, Density, ProcessingTimePerUnit); + } } } diff --git a/Trunk/MonoGameView/Game1.cs b/Trunk/MonoGameView/Game1.cs index 5fc0aa8..0d826ce 100644 --- a/Trunk/MonoGameView/Game1.cs +++ b/Trunk/MonoGameView/Game1.cs @@ -11,6 +11,8 @@ using System.Linq; using CzokoŚmieciarka.MonoGameView.Algorithms; using MonoGameView.DataModels; using MonoGameView.DataModels.Models; +using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models; +using CzokoŚmieciarka.MonoGameView.DataModels.Enums; namespace CzokoŚmieciarka.MonoGameView { @@ -22,14 +24,10 @@ namespace CzokoŚmieciarka.MonoGameView private static int size; GraphicsDeviceManager graphics; SpriteBatch spriteBatch; - Texture2D road1; - Texture2D road2; - Texture2D grass; - Texture2D house; MapLoader mapLoader = new MapLoader(); Vector2 roadPos; float timer; - const float TIMER = 0.2f; + const float TIMER = 0.02f; int stepN; List steps; GarbageCollector collector; @@ -55,11 +53,18 @@ namespace CzokoŚmieciarka.MonoGameView ImageContainer.InitContainer(Content); // TODO: Add your initialization logic here - roadPos = new Vector2(0, 0); - timer = 0.2f; + timer = 5f; mapLoader.Load(out size,out grid,"map.xml"); - collector = new GarbageCollector(new Coords(0, 0), new List(), size, size); + var containers = new List() + { + new GarbageCollectorContainer( + new TypeOfGarbage(GarbageType.Glass,50,1), + 100, + new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,50,1),1) + ), + }; + collector = new GarbageCollector(new Coords(0,0), containers, size, size); stepN = 0; @@ -77,10 +82,6 @@ namespace CzokoŚmieciarka.MonoGameView { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); - road1 = Content.Load("road1"); - road2 = Content.Load("road2"); - grass = Content.Load("grass"); - house = Content.Load("house"); // TODO: use this.Content to load your game content here } diff --git a/Trunk/MonoGameView/MonoGameView.csproj b/Trunk/MonoGameView/MonoGameView.csproj index 4085629..7b6617a 100644 --- a/Trunk/MonoGameView/MonoGameView.csproj +++ b/Trunk/MonoGameView/MonoGameView.csproj @@ -65,7 +65,8 @@ - + + diff --git a/Trunk/MonoGameView/map.bmp b/Trunk/MonoGameView/map.bmp new file mode 100644 index 0000000..2efd73c Binary files /dev/null and b/Trunk/MonoGameView/map.bmp differ