This commit is contained in:
Michał Dulski 2019-04-23 11:24:37 +02:00
commit 977368ad02
12 changed files with 132 additions and 94 deletions

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<configuration> <configuration>
<appSettings> <appSettings>
<add key="imageFilePath" value="C:\Users\micjan11\Projekty\Czoko_Smieciarka\Trunk\MonoGameView\map2.bmp"/> <add key="imageFilePath" value="C:\Users\micjan11\Projekty\Czoko_Smieciarka\Trunk\MonoGameView\map3.bmp"/>
<add key="outputFilePath" value="C:\Users\micjan11\Projekty\Czoko_Smieciarka\Trunk\MonoGameView\bin\Windows\x86\Debug\map.xml"/> <add key="outputFilePath" value="C:\Users\micjan11\Projekty\Czoko_Smieciarka\Trunk\MonoGameView\bin\Windows\x86\Debug\map3.xml"/>
</appSettings> </appSettings>
<startup> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />

View File

@ -10,14 +10,18 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Content;
using System.Threading;
namespace CzokoŚmieciarka.MonoGameView.Algorithms namespace CzokoŚmieciarka.MonoGameView.Algorithms
{ {
public class DFS 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; int count = 0;
public List<Coords> Houses { get; set; } public List<Coords> Houses { get; set; }
@ -40,7 +44,7 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms
var r=Search(content, collector, grid, 0).Key; var r=Search(content, collector, grid, 0).Key;
Console.WriteLine($"Counts : {count}");
if (r == null) return new List<IStep>(); if (r == null) return new List<IStep>();
return r; return r;
@ -65,25 +69,22 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms
foreach (var item in moveSteps) foreach (var item in moveSteps)
{ {
var copyCollector = (AGarbageCollector)collector.Clone(); var copyCollector = (AGarbageCollector)collector.Clone();
try if (item.Invoke(copyCollector, grid))
{ {
item.Invoke(copyCollector, grid);
var gcx = copyCollector.Coords.X; var gcx = copyCollector.Coords.X;
var gcy = copyCollector.Coords.Y; 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); 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<IStep>() var collectSteps = new List<IStep>()
{ {
@ -96,16 +97,8 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms
{ {
var copyCollector = (AGarbageCollector)collector.Clone(); var copyCollector = (AGarbageCollector)collector.Clone();
var copyGrid = CopyGrid(grid); var copyGrid = CopyGrid(grid);
try if (item.Invoke(copyCollector, copyGrid))
{
item.Invoke(copyCollector, copyGrid);
result.Add(item); result.Add(item);
}
catch (Exception e)
{
}
} }
} }
if (grid[collector.Coords.X, collector.Coords.Y] is ADump) if (grid[collector.Coords.X, collector.Coords.Y] is ADump)
@ -121,24 +114,32 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms
{ {
var copyCollector = (AGarbageCollector)collector.Clone(); var copyCollector = (AGarbageCollector)collector.Clone();
var copyGrid = CopyGrid(grid); var copyGrid = CopyGrid(grid);
try if(item.Invoke(copyCollector, copyGrid))
{
item.Invoke(copyCollector, copyGrid);
result.Add(item); result.Add(item);
}
catch (Exception e)
{
} }
} }
}
return result; return result;
} }
KeyValuePair<List<IStep>, int> Search(ContentManager content, GarbageCollector collector, ICloneable[,] grid, int length) KeyValuePair<List<IStep>, int> Search(ContentManager content, GarbageCollector collector, ICloneable[,] grid, int length)
{ {
if (length > 100) return new KeyValuePair<List<IStep>, 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<List<IStep>, int>(null,length);
count++; count++;
if (Houses.All(c => (grid[c.X, c.Y] as IGarbageLocalization).TrashCans.All(j => j.FillPercent == 0.0)) if (Houses.All(c => (grid[c.X, c.Y] as IGarbageLocalization).TrashCans.All(j => j.FillPercent == 0.0))
&& &&

View File

@ -12,50 +12,58 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector
{ {
public abstract class AGarbageCollector : IGarbageCollector, ICloneable public abstract class AGarbageCollector : IGarbageCollector, ICloneable
{ {
public AGarbageCollector(Coords startPosition, IEnumerable<AGarbageCollectorContainer> trashContainers, int columns, int rows) public AGarbageCollector(Coords startPosition, IEnumerable<AGarbageCollectorContainer> trashContainers, int columns, int rows,int houseCounter,int counter = 0)
{ {
this.columns = columns; this.columns = columns;
this.rows = rows; this.rows = rows;
this.Coords = startPosition; this.Coords = startPosition;
this.TrashContainers = trashContainers; 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 Coords Coords { get; set; }
public int columns { get; set; } public int columns { get; set; }
public int rows { get; set; } public int rows { get; set; }
public void MoveUp() public bool MoveUp()
{ {
if(Coords.Y -1 < 0) if(Coords.Y -1 < 0)
{ {
throw new OutOfGridException(); return false;
} }
Coords.Y -= 1; Coords.Y -= 1;
return true;
} }
public void MoveDown() public bool MoveDown()
{ {
if (Coords.Y + 1 >= rows) if (Coords.Y + 1 >= rows)
{ {
throw new OutOfGridException(); return false;
} }
Coords.Y +=1; Coords.Y +=1;
return true;
} }
public void MoveLeft() public bool MoveLeft()
{ {
if (Coords.X - 1 < 0) if (Coords.X - 1 < 0)
{ {
throw new OutOfGridException(); return false;
} }
Coords.X -= 1; Coords.X -= 1;
return true;
} }
public void MoveRight() public bool MoveRight()
{ {
if (Coords.X + 1 >= columns) if (Coords.X + 1 >= columns)
{ {
throw new OutOfGridException(); return false;
} }
Coords.X += 1; Coords.X += 1;
return true;
} }

View File

@ -10,11 +10,13 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector
{ {
public interface IGarbageCollector : ICloneable public interface IGarbageCollector : ICloneable
{ {
int Counter { get; set; }
int HouseCounter { get; set; }
Coords Coords { get; } Coords Coords { get; }
void MoveUp(); bool MoveUp();
void MoveDown(); bool MoveDown();
void MoveLeft(); bool MoveLeft();
void MoveRight(); bool MoveRight();
IEnumerable<AGarbageCollectorContainer> TrashContainers { get; } IEnumerable<AGarbageCollectorContainer> TrashContainers { get; }
} }

View File

@ -4,6 +4,6 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces
{ {
public interface IStep public interface IStep
{ {
void Invoke(IGarbageCollector collector, object [,] grid); bool Invoke(IGarbageCollector collector, object [,] grid);
} }
} }

View File

@ -32,12 +32,12 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans
public virtual void AddGarbage(AGarbage garbage) public virtual void AddGarbage(AGarbage garbage)
{ {
if (this.TypeOfGarbage.GarbageType != garbage.TypeOfGarbage.GarbageType) //if (this.TypeOfGarbage.GarbageType != garbage.TypeOfGarbage.GarbageType)
throw new Exception("You cannot add up different type garbage!"); // 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) //if (newGarbage.Volume > this.MaxVolume)
throw new Exception("Trash overload"); // throw new Exception("Trash overload");
this.Garbage = newGarbage; this.Garbage = newGarbage;
return; return;

View File

@ -16,7 +16,7 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models
{ {
public class GarbageCollector : AGarbageCollector, IDrawables public class GarbageCollector : AGarbageCollector, IDrawables
{ {
public GarbageCollector(Coords c, List<GarbageCollectorContainer> l, int rows, int cols) : base(c,l,rows,cols) public GarbageCollector(Coords c, List<GarbageCollectorContainer> 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() public override object Clone()
{ {
var cloneList = TrashContainers.Select(x=>(GarbageCollectorContainer)x.Clone()).ToList(); 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);
} }
} }
} }

View File

@ -21,18 +21,17 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps
private GarbageType _typeOfGarbage; 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]; 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); 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); var garbage = trashCans.Select(t => t.TakeGarbage()).Aggregate((a,b)=>a+b);
if (_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage.GarbageType != _typeOfGarbage)) 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); _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)) 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)); grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y] = new EmptyHouse(new Coords(_garbageCollector.Coords.X, _garbageCollector.Coords.Y));
_garbageCollector.HouseCounter++;
}
_garbageCollector.Counter = 0;
return true;
} }
} }
} }

View File

@ -20,17 +20,32 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps
private Direction _direction; private Direction _direction;
private IGarbageCollector _garbageCollector; 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) 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)); grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y] = new Road2(new Coords(_garbageCollector.Coords.X, _garbageCollector.Coords.Y));
var pass = false;
switch (_direction) switch (_direction)
{ {
case Direction.Up: _garbageCollector.MoveUp(); break; case Direction.Up:
case Direction.Down: _garbageCollector.MoveDown(); break; pass = _garbageCollector.MoveDown();
case Direction.Left: _garbageCollector.MoveLeft(); break; break;
case Direction.Right: _garbageCollector.MoveRight(); 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;
} }
} }
} }

View File

@ -20,22 +20,19 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps
private GarbageType _typeOfGarbage; 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]; 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) if (_dump.TypeOfGarbage.GarbageType != _typeOfGarbage)
throw new TrashContainerNotFound($"Wysypisko nie przyjmuje smieci typu {_typeOfGarbage}"); return false;
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.GarbageType == _typeOfGarbage)
.Select(t => t.TakeGarbage())
.Aggregate((a, b) => a + b);
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())
{
var garbage = containers.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 x = 0; x < grid.GetLength(0); x++)
{ {
@ -44,6 +41,11 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps
if (grid[x, y] is Road2) grid[x, y] = new Road1(new Coords(x, y)); if (grid[x, y] is Road2) grid[x, y] = new Road1(new Coords(x, y));
} }
} }
_garbageCollector.Counter = 0;
return true;
}
return false;
} }
} }
} }

View File

@ -13,6 +13,8 @@ using MonoGameView.DataModels;
using MonoGameView.DataModels.Models; using MonoGameView.DataModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models; using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.Enums; using CzokoŚmieciarka.MonoGameView.DataModels.Enums;
using System.Threading.Tasks;
using System.Threading;
namespace CzokoŚmieciarka.MonoGameView namespace CzokoŚmieciarka.MonoGameView
{ {
@ -57,37 +59,38 @@ namespace CzokoŚmieciarka.MonoGameView
// TODO: Add your initialization logic here // TODO: Add your initialization logic here
timer = 0f; timer = 0f;
mapLoader.Load(out size,out grid,"map.xml"); mapLoader.Load(out size,out grid,"map3.xml");
var containers = new List<GarbageCollectorContainer>() var containers = new List<GarbageCollectorContainer>()
{ {
new GarbageCollectorContainer( new GarbageCollectorContainer(
new TypeOfGarbage(GarbageType.Glass,50,1), new TypeOfGarbage(GarbageType.Glass,1),
100, 1000,
new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,50,1),0) new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,1),0)
), ),
new GarbageCollectorContainer( new GarbageCollectorContainer(
new TypeOfGarbage(GarbageType.Paper,50,1), new TypeOfGarbage(GarbageType.Paper,1),
100, 1000,
new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,50,1),0) new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,1),0)
), ),
new GarbageCollectorContainer( new GarbageCollectorContainer(
new TypeOfGarbage(GarbageType.Organic,50,1), new TypeOfGarbage(GarbageType.Organic,1),
100, 1000,
new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,50,1),0) new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,1),0)
), ),
new GarbageCollectorContainer( new GarbageCollectorContainer(
new TypeOfGarbage(GarbageType.PlasticMetal,50,1), new TypeOfGarbage(GarbageType.PlasticMetal,1),
100, 1000,
new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,50,1),0) 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; stepN = 0;
var dfs = new DFS(); var dfs = new DFS(collector,grid);
steps = dfs.BestPath(Content, collector, grid); //steps = dfs.BestPath(Content, collector, grid);
new Thread(delegate() { dfs.BestPath(Content, collector, grid); }).Start();
base.Initialize(); base.Initialize();
} }
@ -123,6 +126,7 @@ namespace CzokoŚmieciarka.MonoGameView
{ {
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit(); Exit();
/*
var elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; var elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
timer -= elapsed; timer -= elapsed;
if (timer<0) if (timer<0)
@ -134,6 +138,7 @@ namespace CzokoŚmieciarka.MonoGameView
steps.RemoveAt(0); steps.RemoveAt(0);
} }
} }
*/
// TODO: Add your update logic here // TODO: Add your update logic here
base.Update(gameTime); base.Update(gameTime);

BIN
Trunk/MonoGameView/map3.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 442 B