ło panie

This commit is contained in:
Michał Dulski 2019-04-22 14:17:44 +02:00
parent dc380b2b89
commit 9668e23646
10 changed files with 145 additions and 50 deletions

View File

@ -9,6 +9,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Xna.Framework.Content;
namespace CzokoŚmieciarka.MonoGameView.Algorithms namespace CzokoŚmieciarka.MonoGameView.Algorithms
{ {
@ -20,14 +21,14 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms
int count = 0; int count = 0;
public List<IStep> BestPath(AGarbageCollector collector, object[,] grid) public List<IStep> BestPath(ContentManager content, AGarbageCollector collector, object[,] grid)
{ {
var r=Search(collector, grid, 0).Key; var r=Search(content, collector, grid, 0).Key;
Console.WriteLine(count); Console.WriteLine(count);
return r; return r;
} }
List<IStep> PossibleSteps(AGarbageCollector collector, object[,] grid) List<IStep> PossibleSteps(ContentManager content, AGarbageCollector collector, object[,] grid)
{ {
@ -42,7 +43,7 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms
var filteredMoveSteps = new List<IStep>(); var filteredMoveSteps = new List<IStep>();
foreach (var item in moveSteps) foreach (var item in moveSteps)
{ {
var copyCollector = (AGarbageCollector)collector.Clone(); var copyCollector = (AGarbageCollector)collector.Clone(content);
var copyGrid = (object[,])grid.Clone(); var copyGrid = (object[,])grid.Clone();
try try
{ {
@ -63,26 +64,21 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms
return result; return result;
} }
KeyValuePair<List<IStep>, int> Search(AGarbageCollector collector, object[,] grid, int length) KeyValuePair<List<IStep>, int> Search(ContentManager content, AGarbageCollector collector, object[,] grid, int length)
{ {
count++; count++;
if (length > 40) return new KeyValuePair<List<IStep>, int>(new List<IStep>(), length); if (length > 40) return new KeyValuePair<List<IStep>, int>(new List<IStep>(), length);
var possibleSteps = PossibleSteps(collector, grid); var possibleSteps = PossibleSteps(content, collector, grid);
foreach (var item in possibleSteps) foreach (var item in possibleSteps)
{ {
var copyCollector = (AGarbageCollector)collector.Clone(); var copyCollector = (AGarbageCollector)collector.Clone(content);
var copyGrid = (object[,])grid.Clone(); var copyGrid = (object[,])grid.Clone();
//if (copyGrid[copyCollector.Coords.X, copyCollector.Coords.Y] is IRoad1) copyGrid[copyCollector.Coords.X, copyCollector.Coords.Y] = new Road2(); //if (copyGrid[copyCollector.Coords.X, copyCollector.Coords.Y] is IRoad1) copyGrid[copyCollector.Coords.X, copyCollector.Coords.Y] = new Road2();
item.Invoke(copyCollector, copyGrid); item.Invoke(copyCollector, copyGrid);
var s = Search(copyCollector, copyGrid, length + 1); var s = Search(content, copyCollector, copyGrid, length + 1);
if (s.Key != null) if (s.Key != null)
{ {
s.Key.Insert(0, item); s.Key.Insert(0, item);

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using CzokoŚmieciarka.MonoGameView.DataModels.Exceptions; using CzokoŚmieciarka.MonoGameView.DataModels.Exceptions;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans; using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
using CzokoŚmieciarka.MonoGameView.DataModels.Models; using CzokoŚmieciarka.MonoGameView.DataModels.Models;
using Microsoft.Xna.Framework.Content;
namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector
{ {
@ -57,11 +58,15 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector
Coords.X += 1; Coords.X += 1;
} }
public virtual object Clone() public virtual object Clone(ContentManager content)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public IEnumerable<AGarbageCollectorContainer> TrashContainers { get; } public IEnumerable<AGarbageCollectorContainer> TrashContainers { get; }
public object Clone()
{
throw new NotImplementedException();
}
} }
} }

View File

@ -1,9 +1,10 @@
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces
{ {
public interface IDrawables public interface IDrawables
{ {
void Draw(SpriteBatch spriteBatch, int size); void Draw(ContentManager content, SpriteBatch spriteBatch, int size);
} }
} }

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.Models;
using MonoGameView.DataModels.Models;
namespace MonoGameView.DataModels
{
public class MapLoader
{
public void Load(out int size, out object[,] 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 object[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);
House house = new House(new Coords(x,y));
grid[x, y] = house;
break;
}
}
}
}
}

View File

@ -14,27 +14,20 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models
{ {
public class GarbageCollector : AGarbageCollector, IDrawables public class GarbageCollector : AGarbageCollector, IDrawables
{ {
public Texture2D Image { get; set; } public GarbageCollector(Coords c, List<AGarbageCollectorContainer> l, int rows, int cols) : base(c,l,rows,cols)
public GarbageCollector(ContentManager content, Coords c, List<AGarbageCollectorContainer> l, int rows, int cols) : base(c,l,rows,cols)
{ {
Image = content.Load<Texture2D>("collector");
} }
public GarbageCollector(Texture2D image, Coords c, List<AGarbageCollectorContainer> l, int rows, int cols) : base(c, l, rows, cols)
public void Draw(ContentManager content, SpriteBatch batch,int size)
{ {
Image = image; batch.Draw(content.Load<Texture2D>("collector"), new Rectangle(Coords.X*500/size, Coords.Y*500/size, 500/size, 500/size), Color.White);
} }
public void Draw(SpriteBatch batch,int size) public override object Clone(ContentManager content)
{
batch.Draw(Image, new Rectangle(Coords.X*500/size, Coords.Y*500/size, 500/size, 500/size), Color.White);
}
public override object Clone()
{ {
var cloneList = new List<AGarbageCollectorContainer>(); var cloneList = new List<AGarbageCollectorContainer>();
return new GarbageCollector(Image, new Coords(Coords.X,Coords.Y), cloneList, rows,columns); return new GarbageCollector(new Coords(Coords.X,Coords.Y), cloneList, rows,columns);
} }
} }
} }

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Models;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace MonoGameView.DataModels.Models
{
public class Grass : IDrawables
{
private Coords Coords;
public Grass(Coords coords)
{
Coords = coords;
}
public void Draw(ContentManager content, SpriteBatch batch, int size)
{
batch.Draw(content.Load<Texture2D>("grass"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White);
}
}
}

View File

@ -4,10 +4,23 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace CzokoŚmieciarka.MonoGameView.DataModels.Models namespace CzokoŚmieciarka.MonoGameView.DataModels.Models
{ {
public class House : IHouse public class House : IDrawables
{ {
private Coords Coords;
public House(Coords coords)
{
Coords = coords;
}
public void Draw(ContentManager content, SpriteBatch batch, int size)
{
batch.Draw(content.Load<Texture2D>("house"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White);
}
} }
} }

View File

@ -4,10 +4,24 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace CzokoŚmieciarka.MonoGameView.DataModels.Models namespace CzokoŚmieciarka.MonoGameView.DataModels.Models
{ {
public class Road1 :IRoad1 public class Road1 :IRoad1
{ {
private Coords Coords;
public Road1(Coords coords)
{
Coords = coords;
}
public void Draw(ContentManager content, SpriteBatch batch, int size)
{
batch.Draw(content.Load<Texture2D>("grass"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White);
}
} }
} }

View File

@ -9,6 +9,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using CzokoŚmieciarka.MonoGameView.Algorithms; using CzokoŚmieciarka.MonoGameView.Algorithms;
using MonoGameView.DataModels;
namespace CzokoŚmieciarka.MonoGameView namespace CzokoŚmieciarka.MonoGameView
{ {
@ -17,20 +18,21 @@ namespace CzokoŚmieciarka.MonoGameView
/// </summary> /// </summary>
public class Game1 : Game public class Game1 : Game
{ {
private static int size = 20; private static int size;
GraphicsDeviceManager graphics; GraphicsDeviceManager graphics;
SpriteBatch spriteBatch; SpriteBatch spriteBatch;
Texture2D road1; Texture2D road1;
Texture2D road2; Texture2D road2;
Texture2D grass; Texture2D grass;
Texture2D house; Texture2D house;
MapLoader mapLoader = new MapLoader();
Vector2 roadPos; Vector2 roadPos;
float timer; float timer;
const float TIMER = 0.2f; const float TIMER = 0.2f;
int stepN; int stepN;
List<IStep> steps; List<IStep> steps;
GarbageCollector collector; GarbageCollector collector;
object[,] grid = new object[size, size]; object[,] grid;
public Game1() public Game1()
{ {
graphics = new GraphicsDeviceManager(this); graphics = new GraphicsDeviceManager(this);
@ -52,23 +54,14 @@ namespace CzokoŚmieciarka.MonoGameView
// TODO: Add your initialization logic here // TODO: Add your initialization logic here
roadPos = new Vector2(0, 0); roadPos = new Vector2(0, 0);
timer = 0.2f; timer = 0.2f;
for (int x=0;x<size;x++)
{
for (int y=0;y<size;y++)
{
if (x % 2 == 0 || y == 1 || y == 5 || y == 9 || y == 13 || y == 17) grid[x, y] = new Road1();
else grid[x, y] = null;
}
}
grid[4, 0] = new House(); mapLoader.Load(out size,out grid,"map.xml");
grid[6, 4] = new House(); collector = new GarbageCollector(new Coords(0, 0), new List<AGarbageCollectorContainer>(), size, size);
collector = new GarbageCollector(Content,new Coords(0, 0), new List<AGarbageCollectorContainer>(), size, size);
stepN = 0; stepN = 0;
var dfs = new DFS(); var dfs = new DFS();
steps = dfs.BestPath(collector, grid); steps = dfs.BestPath(Content, collector, grid);
base.Initialize(); base.Initialize();
} }
@ -119,11 +112,6 @@ namespace CzokoŚmieciarka.MonoGameView
} }
} }
// TODO: Add your update logic here // TODO: Add your update logic here
base.Update(gameTime); base.Update(gameTime);
} }
@ -147,7 +135,7 @@ namespace CzokoŚmieciarka.MonoGameView
else spriteBatch.Draw(grass, new Rectangle(x * 500 / size, y * 500 / size, 500 / size, 500 / size), Color.White); else spriteBatch.Draw(grass, new Rectangle(x * 500 / size, y * 500 / size, 500 / size, 500 / size), Color.White);
} }
} }
collector.Draw(spriteBatch, size); collector.Draw(Content, spriteBatch, size);
spriteBatch.End(); spriteBatch.End();
// TODO: Add your drawing code here // TODO: Add your drawing code here

View File

@ -62,11 +62,13 @@
<Compile Include="DataModels\Interfaces\TrashCans\ADump.cs" /> <Compile Include="DataModels\Interfaces\TrashCans\ADump.cs" />
<Compile Include="DataModels\Interfaces\TrashCans\AGarbageCollectorContainer.cs" /> <Compile Include="DataModels\Interfaces\TrashCans\AGarbageCollectorContainer.cs" />
<Compile Include="DataModels\Interfaces\TrashCans\ATrashCan.cs" /> <Compile Include="DataModels\Interfaces\TrashCans\ATrashCan.cs" />
<Compile Include="DataModels\MapLoader.cs" />
<Compile Include="DataModels\Models\Coords.cs" /> <Compile Include="DataModels\Models\Coords.cs" />
<Compile Include="DataModels\Models\Dump.cs" /> <Compile Include="DataModels\Models\Dump.cs" />
<Compile Include="DataModels\Models\Garbage.cs" /> <Compile Include="DataModels\Models\Garbage.cs" />
<Compile Include="DataModels\Models\GarbageCollectorContainer.cs" /> <Compile Include="DataModels\Models\GarbageCollectorContainer.cs" />
<Compile Include="DataModels\Models\GarbageLocalization.cs" /> <Compile Include="DataModels\Models\GarbageLocalization.cs" />
<Compile Include="DataModels\Models\Grass.cs" />
<Compile Include="DataModels\Models\House.cs" /> <Compile Include="DataModels\Models\House.cs" />
<Compile Include="DataModels\Models\Map.cs" /> <Compile Include="DataModels\Models\Map.cs" />
<Compile Include="DataModels\Models\Road1.cs" /> <Compile Include="DataModels\Models\Road1.cs" />