-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c936ee8
commit 6efded6
Showing
38 changed files
with
1,749 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Hanoi | ||
{ | ||
enum Direction | ||
{ | ||
left, right, far_left, far_right | ||
} | ||
|
||
public class Action | ||
{ | ||
public Peg Source { get; set; } | ||
public Peg Destination{ get; set; } | ||
public string Direction { get; set; } | ||
|
||
//copy constractor | ||
public Action(Action copy) | ||
{ | ||
Source = copy.Source; | ||
Destination = copy.Destination; | ||
Direction = copy.Direction; | ||
} | ||
|
||
public Action(Peg source, Peg destination, string direction) | ||
{ | ||
Source = source; | ||
Destination = destination; | ||
Direction = direction; | ||
} | ||
public static Stack<Action> getSolutionPath(Node goal) | ||
{ | ||
//last action should proccessed last | ||
var solution = new Stack<Action>(); | ||
//holds all nodes lies on the solution path | ||
var tmp = goal; | ||
// | ||
//extract actions going from the goal to the root(initial state) | ||
//scan the path bottom-up | ||
// | ||
//is it the initial state? | ||
//while the root isnt' reached yet | ||
while (tmp.Parent != null) | ||
{ | ||
solution.Push(tmp.Action); | ||
//point to the previous node at the solution path | ||
tmp = tmp.Parent; | ||
} | ||
return solution; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" 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>{48AD6D4F-93CD-4791-95B7-352AFD7F442E}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<RootNamespace>Hanoi</RootNamespace> | ||
<AssemblyName>Hanoi</AssemblyName> | ||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
</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="Action.cs" /> | ||
<Compile Include="Node.cs" /> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
<Compile Include="State.cs" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.27428.2005 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hanoi", "Hanoi.csproj", "{48AD6D4F-93CD-4791-95B7-352AFD7F442E}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hanoi.GUI", "..\hanoi.GUI\hanoi.GUI.csproj", "{B643FC42-9390-4393-9C9C-1FC7CA446E47}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{48AD6D4F-93CD-4791-95B7-352AFD7F442E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{48AD6D4F-93CD-4791-95B7-352AFD7F442E}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{48AD6D4F-93CD-4791-95B7-352AFD7F442E}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{48AD6D4F-93CD-4791-95B7-352AFD7F442E}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{B643FC42-9390-4393-9C9C-1FC7CA446E47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{B643FC42-9390-4393-9C9C-1FC7CA446E47}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{B643FC42-9390-4393-9C9C-1FC7CA446E47}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{B643FC42-9390-4393-9C9C-1FC7CA446E47}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {60FE9A17-9DA6-425B-9E37-1D1DC9FFF2FF} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Hanoi | ||
{ | ||
public class Node | ||
{ | ||
public Node() | ||
{ | ||
|
||
} | ||
|
||
//copy constructor | ||
public Node(Node node) | ||
{ | ||
Depth = node.Depth; | ||
Parent = node.Parent; | ||
Action = new Action(node.Action); | ||
State = new State(node.State); | ||
} | ||
|
||
public int Depth { get; set; } | ||
public Node Parent { get; set; } | ||
public Hanoi.State State { get; set; } | ||
public Action Action { get; set; } | ||
// | ||
// | ||
// | ||
//check near(adjacent pegs) first then far | ||
// check moving to lift first then to right | ||
public List<Node> Expand() | ||
{ | ||
//output | ||
List<Node> successors = new List<Node>(); | ||
//get all pegs | ||
var pegs = this.State.Pegs; | ||
// explore all pegs as source | ||
// i points to source peg | ||
for (int i = 0; i < pegs.Length; i++) | ||
{ | ||
//if i points to an empty peg as the source | ||
if (pegs[i].Count == 0) | ||
{ | ||
//look for another peg | ||
continue; | ||
}//else | ||
//get the disc on the top of the source | ||
var discToMoveFromSource = pegs[i].Peek(); | ||
//find all possible successors | ||
// explore all pegs as destination | ||
// j points to destination peg | ||
for (int j = 0; j < pegs.Length; j++) | ||
{ | ||
// | ||
//if the destination peg is empty | ||
//important information we'll need later | ||
bool emptyPeg = pegs[j].Count == 0; | ||
// avoid nullRefExeption | ||
// when comparing the source disc with +infinity | ||
// (source < destination) this condition -which checks | ||
// if disk to move from source is smaller than the disc at the top of destination- | ||
// will be always true (at Line 44, 88) | ||
var discOnTheTopOfDestination = (emptyPeg) ? int.MaxValue : pegs[j].Peek(); | ||
// | ||
// skip if i and j points to the same peg | ||
var samePeg = i == j; | ||
// | ||
// (source is smaller than destination)? | ||
var sourceSmaller = (discToMoveFromSource < discOnTheTopOfDestination); | ||
// | ||
if (!samePeg && sourceSmaller) | ||
{ | ||
// | ||
//make a move & create a new successor | ||
// where to move the source disc | ||
string direction = GetDirection(i, j); | ||
// | ||
//move from i to j | ||
Peg source = (Peg) i; | ||
Peg destination = (Peg) j; | ||
Action action = new Action(source, destination, direction); | ||
// | ||
// the action which leads to new successor function | ||
successors.Add( | ||
CreateSuccessor(discToMoveFromSource, (Peg) j, emptyPeg, action)); | ||
} | ||
} | ||
} | ||
return successors; | ||
} | ||
|
||
// | ||
//input: | ||
//i is the source peg index | ||
//j is the destination peg index | ||
private static string GetDirection(int i, int j) | ||
{ | ||
// where to move the source disc | ||
string direction; | ||
// | ||
//if the source and destination pegs are adjacent | ||
//the action is move near not far | ||
bool adjacentPegs = Math.Abs(i - j) == 1; | ||
if (adjacentPegs) | ||
{ | ||
direction = leftOrRight(i, j); | ||
} | ||
// | ||
//if the source and destination pegs are first and third | ||
//the action is move far | ||
else | ||
{ | ||
direction = $"far {leftOrRight(i,j)}"; | ||
} | ||
|
||
// | ||
//short code | ||
//direction = (adjacentPegs) ? leftOrRight(i, j) : $"far {leftOrRight(i, j)}"; | ||
// | ||
var x = | ||
//pegs are adjacent? | ||
(adjacentPegs) ? | ||
// | ||
//if the source and destination pegs are adjacent | ||
//the action is move near not far | ||
"near " : | ||
// | ||
//if the source and destination pegs are first and third | ||
//the action is move far | ||
"far " + leftOrRight(i, j); | ||
// | ||
return direction; | ||
} | ||
|
||
// | ||
//input: | ||
//i is the source peg index | ||
//j is the destination peg index | ||
private static string leftOrRight(int i, int j) | ||
{ | ||
string direction; | ||
// | ||
//if the source is on the right of the destination | ||
//the action is move left | ||
//if difference between source index i and destination index j is positive | ||
if (i - j > 0) | ||
{ | ||
direction = "left"; | ||
} | ||
// | ||
//if the source is on the lift of the destination | ||
//the action is move right | ||
// if difference is negative | ||
else | ||
{ | ||
direction = "right"; | ||
} | ||
|
||
return direction; | ||
} | ||
|
||
private Node CreateSuccessor(int discToMoveFromSource, Peg destinationPeg, bool isEmptyPeg, Action action) | ||
{ | ||
Node child = new Node | ||
{ | ||
//depth of parent plus one | ||
Depth = this.Depth + 1, | ||
//parent is the current node | ||
Parent = this, | ||
//extract the child state from the parent state and the new action | ||
State = new State(this.State, discToMoveFromSource, destinationPeg, isEmptyPeg), | ||
Action = action | ||
}; | ||
return child; | ||
} | ||
} | ||
} |
Oops, something went wrong.