dfs dziala prawie

This commit is contained in:
ryuga4 2019-04-22 03:16:30 +02:00
parent 1488c8f153
commit e1aac08c6b
27 changed files with 373 additions and 40 deletions

View File

@ -41,6 +41,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DFS.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -0,0 +1,109 @@
using CzokoŚmieciarka.DataModels.Enums;
using CzokoŚmieciarka.DataModels.Interfaces;
using CzokoŚmieciarka.DataModels.Interfaces.GarbageCollector;
using CzokoŚmieciarka.DataModels.Interfaces.TrashCans;
using CzokoŚmieciarka.DataModels.Models;
using CzokoŚmieciarka.DataModels.Models.Steps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CzokoŚmieciarka.AI_Naive
{
public class DFS
{
public DFS()
{
}
int count = 0;
public List<IStep> BestPath(AGarbageCollector collector, object[,] grid)
{
var r=Search(collector, grid, 0).Key;
Console.WriteLine(count);
return r;
}
List<IStep> PossibleSteps(AGarbageCollector collector, object[,] grid)
{
var result = new List<IStep>();
var moveSteps = new List<IStep>()
{
new MoveStep(Direction.Up),
new MoveStep(Direction.Down),
new MoveStep(Direction.Left),
new MoveStep(Direction.Right)
};
var filteredMoveSteps = new List<IStep>();
foreach (var item in moveSteps)
{
var copyCollector = (AGarbageCollector)collector.Clone();
var copyGrid = (object[,])grid.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)
{
result.Add(item);
}
}
catch (Exception e)
{
}
}
return result;
}
KeyValuePair<List<IStep>, int> Search(AGarbageCollector collector, object[,] grid, int length)
{
count++;
if (length > 40) return new KeyValuePair<List<IStep>, int>(new List<IStep>(), length);
var possibleSteps = PossibleSteps(collector, grid);
foreach (var item in possibleSteps)
{
var copyCollector = (AGarbageCollector)collector.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();
item.Invoke(copyCollector, copyGrid);
var s = Search(copyCollector, copyGrid, length + 1);
if (s.Key != null)
{
s.Key.Insert(0, item);
return s;
}
}
return new KeyValuePair<List<IStep>, int>(null, length);
/*var min = int.MaxValue;
var min_index = 0;
for (int i = 0; i < mapped.Count; i++)
{
if (mapped.ElementAt(i).Value <= min)
{
min = mapped.ElementAt(i).Value;
min_index = i;
}
}
return mapped.ElementAt(min_index);
*/
}
}
}

View File

@ -51,6 +51,8 @@
<Compile Include="Interfaces\Garbage\AGarbage.cs" />
<Compile Include="Interfaces\Garbage\IGarbage.cs" />
<Compile Include="Interfaces\IRoad.cs" />
<Compile Include="Interfaces\IRoad1.cs" />
<Compile Include="Interfaces\IRoad2.cs" />
<Compile Include="Interfaces\TrashCans\ADump.cs" />
<Compile Include="Interfaces\IGarbageLocalization.cs" />
<Compile Include="Interfaces\ITypeOfGarbage.cs" />
@ -60,6 +62,8 @@
<Compile Include="Interfaces\IStep.cs" />
<Compile Include="Models\Coords.cs" />
<Compile Include="Models\Map.cs" />
<Compile Include="Models\Road1.cs" />
<Compile Include="Models\Road2.cs" />
<Compile Include="Models\Steps\CollectStep.cs" />
<Compile Include="Models\Steps\MoveStep.cs" />
<Compile Include="Models\Steps\SpillStep.cs" />

View File

@ -9,7 +9,7 @@ using CzokoŚmieciarka.DataModels.Models;
namespace CzokoŚmieciarka.DataModels.Interfaces.GarbageCollector
{
public abstract class AGarbageCollector : IGarbageCollector
public abstract class AGarbageCollector : IGarbageCollector, ICloneable
{
public AGarbageCollector(Coords startPosition, IEnumerable<AGarbageCollectorContainer> trashContainers, int columns, int rows)
{
@ -57,9 +57,9 @@ namespace CzokoŚmieciarka.DataModels.Interfaces.GarbageCollector
Coords.X += 1;
}
public object Clone()
public virtual object Clone()
{
return this.MemberwiseClone();
throw new NotImplementedException();
}
public IEnumerable<AGarbageCollectorContainer> TrashContainers { get; }

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CzokoŚmieciarka.DataModels.Interfaces
{
public interface IRoad1
{
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CzokoŚmieciarka.DataModels.Interfaces
{
public interface IRoad2
{
}
}

View File

@ -1,7 +1,9 @@
namespace CzokoŚmieciarka.DataModels.Interfaces
using CzokoŚmieciarka.DataModels.Interfaces.GarbageCollector;
namespace CzokoŚmieciarka.DataModels.Interfaces
{
public interface IStep
{
void Invoke();
void Invoke(IGarbageCollector collector, object [,] grid);
}
}

View File

@ -35,5 +35,20 @@ namespace CzokoŚmieciarka.DataModels.Models
{
return new Coords(a.X - b.X, a.Y - b.Y);
}
public override string ToString()
{
return base.ToString();
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
}

View File

@ -0,0 +1,13 @@
using CzokoŚmieciarka.DataModels.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CzokoŚmieciarka.DataModels.Models
{
public class Road1 :IRoad1
{
}
}

View File

@ -0,0 +1,13 @@
using CzokoŚmieciarka.DataModels.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CzokoŚmieciarka.DataModels.Models
{
public class Road2 : IRoad2
{
}
}

View File

@ -10,19 +10,16 @@ namespace CzokoŚmieciarka.DataModels.Models.Steps
{
public class CollectStep : IStep
{
public CollectStep(IGarbageCollector garbageCollector, IGarbageLocalization garbageLocalization, ITypeOfGarbage typeOfGarbage)
public CollectStep(ITypeOfGarbage typeOfGarbage)
{
this._garbageCollector = garbageCollector;
this._garbageLocalization = garbageLocalization;
this._typeOfGarbage = typeOfGarbage;
}
private IGarbageCollector _garbageCollector;
private ITypeOfGarbage _typeOfGarbage;
private IGarbageLocalization _garbageLocalization;
public void Invoke()
public void Invoke(IGarbageCollector garbageCollector, object [,] grid)
{
/*
if(_garbageCollector.Coords != _garbageLocalization.Coords)
throw new WrongPositionException("Śmieciarka nie jest w miejscu oderbania śmieci");
@ -33,6 +30,7 @@ namespace CzokoŚmieciarka.DataModels.Models.Steps
throw new TrashContainerNotFound($"Nie znaleziono kontenera na {_typeOfGarbage.GarbageType}.");
_garbageCollector.TrashContainers.First(t => t.TypeOfGarbage == _typeOfGarbage).AddGarbage(garbage);
*/
}
}
}

View File

@ -11,17 +11,18 @@ namespace CzokoŚmieciarka.DataModels.Models.Steps
{
public class MoveStep : IStep
{
public MoveStep(Direction direction, IGarbageCollector garbageCollector)
public MoveStep(Direction direction)
{
this._garbageCollector = garbageCollector;
this._direction = direction;
}
private Direction _direction;
private IGarbageCollector _garbageCollector;
public void Invoke()
public void Invoke(IGarbageCollector _garbageCollector, object[,] grid)
{
if(grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y] is Road1)
grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y] = new Road2();
switch (_direction)
{
case Direction.Up: _garbageCollector.MoveUp(); break;

View File

@ -10,19 +10,15 @@ namespace CzokoŚmieciarka.DataModels.Models.Steps
{
public class SpillStep : IStep
{
public SpillStep(IGarbageCollector garbageCollector, ADump dump, ITypeOfGarbage typeOfGarbage)
public SpillStep(ITypeOfGarbage typeOfGarbage)
{
this._garbageCollector = garbageCollector;
this._dump = dump;
this._typeOfGarbage = typeOfGarbage;
}
private IGarbageCollector _garbageCollector;
private ADump _dump;
private ITypeOfGarbage _typeOfGarbage;
public void Invoke()
{
public void Invoke(IGarbageCollector garbageCollector, object [,] grid)
{/*
if(_garbageCollector.Coords != _dump.Coords)
throw new WrongPositionException("Śmieciarka nie na terenie podanego wyspiska");
@ -36,7 +32,7 @@ namespace CzokoŚmieciarka.DataModels.Models.Steps
.Select(t => t.TakeGarbage())
.Aggregate((a, b) => a + b);
_dump.AddGarbage(garbage);
_dump.AddGarbage(garbage);*/
}
}
}

View File

@ -13,7 +13,7 @@
#---------------------------------- Content ---------------------------------#
#begin intersection.png
#begin collector.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
@ -23,9 +23,9 @@
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:intersection.png
/build:collector.png
#begin road.png
#begin grass.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
@ -35,5 +35,41 @@
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:road.png
/build:grass.png
#begin house.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:house.png
#begin road1.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:road1.png
#begin road2.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:road2.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 369 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 420 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

View File

Before

Width:  |  Height:  |  Size: 180 B

After

Width:  |  Height:  |  Size: 180 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 193 B

View File

@ -1,9 +1,17 @@
using Microsoft.Xna.Framework;
using CzokoŚmieciarka.AI_Naive;
using CzokoŚmieciarka.DataModels.Interfaces;
using CzokoŚmieciarka.DataModels.Interfaces.GarbageCollector;
using CzokoŚmieciarka.DataModels.Interfaces.TrashCans;
using CzokoŚmieciarka.DataModels.Models;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGameView;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MonoGameView
namespace CzokoŚmieciarka.MonoGameView
{
/// <summary>
/// This is the main type for your game.
@ -12,8 +20,16 @@ namespace MonoGameView
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D road;
Texture2D road1;
Texture2D road2;
Texture2D grass;
Vector2 roadPos;
float timer;
const float TIMER = 0.2f;
int stepN;
List<IStep> steps;
GarbageCollector collector;
object[,] grid = new object[10, 10];
public Game1()
{
graphics = new GraphicsDeviceManager(this);
@ -22,6 +38,8 @@ namespace MonoGameView
graphics.PreferredBackBufferHeight = 500;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
@ -32,6 +50,24 @@ namespace MonoGameView
{
// TODO: Add your initialization logic here
roadPos = new Vector2(0, 0);
timer = 0.2f;
for (int x=0;x<10;x++)
{
for (int y=0;y<10;y++)
{
if (x % 2 == 0 || y % 2 == 0) grid[x, y] = new Road1();
else grid[x, y] = null;
}
}
collector = new GarbageCollector(Content,new Coords(0, 0), new List<AGarbageCollectorContainer>(), 10, 10);
stepN = 0;
var dfs = new DFS();
steps = dfs.BestPath(collector, grid);
base.Initialize();
}
@ -43,7 +79,9 @@ namespace MonoGameView
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
road = Content.Load<Texture2D>("road");
road1 = Content.Load<Texture2D>("road1");
road2 = Content.Load<Texture2D>("road2");
grass = Content.Load<Texture2D>("grass");
// TODO: use this.Content to load your game content here
}
@ -65,11 +103,24 @@ namespace 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)
{
timer = TIMER;
if (steps.Any())
{
steps.First().Invoke(collector, grid);
steps.RemoveAt(0);
}
}
// TODO: Add your update logic here
double time = gameTime.TotalGameTime.TotalMilliseconds / 1000 * Math.PI;
roadPos.X = (float) (225 + 200*Math.Sin(time));
roadPos.Y = (float)(225 + 200*Math.Cos(time));
base.Update(gameTime);
}
@ -82,7 +133,17 @@ namespace MonoGameView
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(road, roadPos, Color.White);
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
if (grid[x, y] is Road1) spriteBatch.Draw(road1, new Vector2(x*50,y*50), Color.White);
else if (grid[x,y] is Road2) spriteBatch.Draw(road2, new Vector2(x * 50, y * 50), Color.White);
else spriteBatch.Draw(grass, new Vector2(x * 50, y * 50), Color.White);
}
}
collector.Draw(spriteBatch);
spriteBatch.End();
// TODO: Add your drawing code here

View File

@ -0,0 +1,40 @@
using CzokoŚmieciarka.DataModels.Interfaces.GarbageCollector;
using CzokoŚmieciarka.DataModels.Interfaces.TrashCans;
using CzokoŚmieciarka.DataModels.Models;
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;
namespace MonoGameView
{
public class GarbageCollector : AGarbageCollector
{
public Texture2D Image { get; set; }
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)
{
Image = image;
}
public void Draw(SpriteBatch batch)
{
batch.Draw(Image, new Vector2(Coords.X*50, Coords.Y*50), Color.White);
}
public override object Clone()
{
var cloneList = new List<AGarbageCollectorContainer>();
return new GarbageCollector(Image, new Coords(Coords.X,Coords.Y), cloneList, rows,columns);
}
}
}

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -13,7 +13,8 @@
<AssemblyName>MonoGameView</AssemblyName>
<FileAlignment>512</FileAlignment>
<MonoGamePlatform>Windows</MonoGamePlatform>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
@ -42,6 +43,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Game1.cs" />
<Compile Include="GarbageCollector.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
@ -57,8 +59,23 @@
</ItemGroup>
<ItemGroup>
<MonoGameContentReference Include="Content\Content.mgcb" />
<None Include="app.config" />
<None Include="app.manifest" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Components\CzokoŚmieciarka.AI_Naive\CzokoŚmieciarka.AI_Naive.csproj">
<Project>{10e77bbe-55e1-483d-a456-0e94eac9b24a}</Project>
<Name>CzokoŚmieciarka.AI_Naive</Name>
</ProjectReference>
<ProjectReference Include="..\Components\CzokoŚmieciarka.DataModels.GeneralModels\CzokoŚmieciarka.DataModels.GeneralModels.csproj">
<Project>{a3d5da96-69d7-463f-b1ee-6fc70716e3b2}</Project>
<Name>CzokoŚmieciarka.DataModels.GeneralModels</Name>
</ProjectReference>
<ProjectReference Include="..\Components\CzokoŚmieciarka.DataModels\CzokoŚmieciarka.DataModels.csproj">
<Project>{f2e11fee-c5ac-47d2-ba9c-819909b6dff7}</Project>
<Name>CzokoŚmieciarka.DataModels</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Content.Builder.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@ -1,6 +1,6 @@
using System;
namespace MonoGameView
namespace CzokoŚmieciarka.MonoGameView
{
#if WINDOWS || LINUX
/// <summary>

View File

@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup></configuration>