mirror of
https://github.com/Wessel/Roommapper.git
synced 2026-07-22 07:47:10 +02:00
add astar test
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -6,4 +6,6 @@ src/tmp
|
||||
node_modules/
|
||||
|
||||
obj/
|
||||
out/
|
||||
out/
|
||||
/src/Client/RobotController/.vs
|
||||
/.vs
|
||||
|
||||
22
src/Pathfinder/AStarPathfinding.sln
Normal file
22
src/Pathfinder/AStarPathfinding.sln
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2013
|
||||
VisualStudioVersion = 12.0.31101.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AStarPathfinding", "AStarPathfinding\AStarPathfinding.csproj", "{97647F1E-1A0A-444D-A437-A809F9EB855F}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{97647F1E-1A0A-444D-A437-A809F9EB855F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{97647F1E-1A0A-444D-A437-A809F9EB855F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{97647F1E-1A0A-444D-A437-A809F9EB855F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{97647F1E-1A0A-444D-A437-A809F9EB855F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
59
src/Pathfinder/AStarPathfinding/AStarPathfinding.csproj
Normal file
59
src/Pathfinder/AStarPathfinding/AStarPathfinding.csproj
Normal file
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{97647F1E-1A0A-444D-A437-A809F9EB855F}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>AStarPathfinding</RootNamespace>
|
||||
<AssemblyName>AStarPathfinding</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
6
src/Pathfinder/AStarPathfinding/App.config
Normal file
6
src/Pathfinder/AStarPathfinding/App.config
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
|
||||
</startup>
|
||||
</configuration>
|
||||
146
src/Pathfinder/AStarPathfinding/Program.cs
Normal file
146
src/Pathfinder/AStarPathfinding/Program.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AStarPathfinding
|
||||
{
|
||||
class Location
|
||||
{
|
||||
public int X;
|
||||
public int Y;
|
||||
public int F;
|
||||
public int G;
|
||||
public int H;
|
||||
public Location Parent;
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.Title = "A* Pathfinding";
|
||||
|
||||
// draw map
|
||||
|
||||
string[] map = new string[]
|
||||
{
|
||||
"+------+",
|
||||
"| |",
|
||||
"|A X |",
|
||||
"|XXX |",
|
||||
"| X |",
|
||||
"| B |",
|
||||
"| |",
|
||||
"+------+",
|
||||
};
|
||||
|
||||
foreach (var line in map)
|
||||
Console.WriteLine(line);
|
||||
|
||||
// algorithm
|
||||
|
||||
Location current = null;
|
||||
var start = new Location { X = 1, Y = 2 };
|
||||
var target = new Location { X = 2, Y = 5 };
|
||||
var openList = new List<Location>();
|
||||
var closedList = new List<Location>();
|
||||
int g = 0;
|
||||
|
||||
// start by adding the original position to the open list
|
||||
openList.Add(start);
|
||||
|
||||
while (openList.Count > 0)
|
||||
{
|
||||
// get the square with the lowest F score
|
||||
var lowest = openList.Min(l => l.F);
|
||||
current = openList.First(l => l.F == lowest);
|
||||
|
||||
// add the current square to the closed list
|
||||
closedList.Add(current);
|
||||
|
||||
// show current square on the map
|
||||
Console.SetCursorPosition(current.X, current.Y);
|
||||
Console.Write('.');
|
||||
Console.SetCursorPosition(current.X, current.Y);
|
||||
System.Threading.Thread.Sleep(1000);
|
||||
|
||||
// remove it from the open list
|
||||
openList.Remove(current);
|
||||
|
||||
// if we added the destination to the closed list, we've found a path
|
||||
if (closedList.FirstOrDefault(l => l.X == target.X && l.Y == target.Y) != null)
|
||||
break;
|
||||
|
||||
var adjacentSquares = GetWalkableAdjacentSquares(current.X, current.Y, map);
|
||||
g++;
|
||||
|
||||
foreach(var adjacentSquare in adjacentSquares)
|
||||
{
|
||||
// if this adjacent square is already in the closed list, ignore it
|
||||
if (closedList.FirstOrDefault(l => l.X == adjacentSquare.X
|
||||
&& l.Y == adjacentSquare.Y) != null)
|
||||
continue;
|
||||
|
||||
// if it's not in the open list...
|
||||
if (openList.FirstOrDefault(l => l.X == adjacentSquare.X
|
||||
&& l.Y == adjacentSquare.Y) == null)
|
||||
{
|
||||
// compute its score, set the parent
|
||||
adjacentSquare.G = g;
|
||||
adjacentSquare.H = ComputeHScore(adjacentSquare.X, adjacentSquare.Y, target.X, target.Y);
|
||||
adjacentSquare.F = adjacentSquare.G + adjacentSquare.H;
|
||||
adjacentSquare.Parent = current;
|
||||
|
||||
// and add it to the open list
|
||||
openList.Insert(0, adjacentSquare);
|
||||
}
|
||||
else
|
||||
{
|
||||
// test if using the current G score makes the adjacent square's F score
|
||||
// lower, if yes update the parent because it means it's a better path
|
||||
if (g + adjacentSquare.H < adjacentSquare.F)
|
||||
{
|
||||
adjacentSquare.G = current.G + 1;
|
||||
adjacentSquare.F = adjacentSquare.G + adjacentSquare.H;
|
||||
adjacentSquare.Parent = current;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// assume path was found; let's show it
|
||||
while (current != null)
|
||||
{
|
||||
Console.SetCursorPosition(current.X, current.Y);
|
||||
Console.Write('_');
|
||||
Console.SetCursorPosition(current.X, current.Y);
|
||||
current = current.Parent;
|
||||
System.Threading.Thread.Sleep(1000);
|
||||
}
|
||||
|
||||
// end
|
||||
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
static List<Location> GetWalkableAdjacentSquares(int x, int y, string[] map)
|
||||
{
|
||||
var proposedLocations = new List<Location>()
|
||||
{
|
||||
new Location { X = x, Y = y - 1 },
|
||||
new Location { X = x, Y = y + 1 },
|
||||
new Location { X = x - 1, Y = y },
|
||||
new Location { X = x + 1, Y = y },
|
||||
};
|
||||
|
||||
return proposedLocations.Where(l => map[l.Y][l.X] == ' ' || map[l.Y][l.X] == 'B').ToList();
|
||||
}
|
||||
|
||||
static int ComputeHScore(int x, int y, int targetX, int targetY)
|
||||
{
|
||||
return Math.Abs(targetX - x) + Math.Abs(targetY - y);
|
||||
}
|
||||
}
|
||||
}
|
||||
36
src/Pathfinder/AStarPathfinding/Properties/AssemblyInfo.cs
Normal file
36
src/Pathfinder/AStarPathfinding/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("AStarPathfinding")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("AStarPathfinding")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2015")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("41c6c38d-e6f0-4437-b8a8-fa0a8ea19eab")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
BIN
src/Pathfinder/AStarPathfinding/bin/Debug/AStarPathfinding.exe
Normal file
BIN
src/Pathfinder/AStarPathfinding/bin/Debug/AStarPathfinding.exe
Normal file
Binary file not shown.
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
|
||||
</startup>
|
||||
</configuration>
|
||||
BIN
src/Pathfinder/AStarPathfinding/bin/Debug/AStarPathfinding.pdb
Normal file
BIN
src/Pathfinder/AStarPathfinding/bin/Debug/AStarPathfinding.pdb
Normal file
Binary file not shown.
Reference in New Issue
Block a user