From 5fc5a254ab3e151b006f3ddece11cdd98834b8e9 Mon Sep 17 00:00:00 2001 From: Lucas Scharenbroich Date: Wed, 30 Nov 2016 16:13:43 -0600 Subject: [PATCH] Change the success function to allow the order of the succesors to be controlled --- SpriteCompiler/AI/ISearchProblem.cs | 2 +- SpriteCompiler/AI/ISuccessorFunction.cs | 3 ++- SpriteCompiler/AI/InformedNodeExpander.cs | 4 ++-- SpriteCompiler/AI/SearchProblem.cs | 2 +- SpriteCompiler/Problem/SpriteGeneratorSuccessorFunction.cs | 7 ++++--- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/SpriteCompiler/AI/ISearchProblem.cs b/SpriteCompiler/AI/ISearchProblem.cs index fef1cf3..739f0e7 100644 --- a/SpriteCompiler/AI/ISearchProblem.cs +++ b/SpriteCompiler/AI/ISearchProblem.cs @@ -6,7 +6,7 @@ public interface ISearchProblem where C : IComparable { - IDictionary Successors(S state); + IEnumerable> Successors(S state); bool IsGoal(S state); C StepCost(S fromState, A action, S toState); C Heuristic(S state); diff --git a/SpriteCompiler/AI/ISuccessorFunction.cs b/SpriteCompiler/AI/ISuccessorFunction.cs index 876c501..b9c90a7 100644 --- a/SpriteCompiler/AI/ISuccessorFunction.cs +++ b/SpriteCompiler/AI/ISuccessorFunction.cs @@ -1,9 +1,10 @@ namespace SpriteCompiler.AI { + using System; using System.Collections.Generic; public interface ISuccessorFunction { - IDictionary Successors(S state); + IEnumerable> Successors(S state); } } diff --git a/SpriteCompiler/AI/InformedNodeExpander.cs b/SpriteCompiler/AI/InformedNodeExpander.cs index 138ae3e..d41e3f6 100644 --- a/SpriteCompiler/AI/InformedNodeExpander.cs +++ b/SpriteCompiler/AI/InformedNodeExpander.cs @@ -13,8 +13,8 @@ namespace SpriteCompiler.AI { foreach (var successor in problem.Successors(node.State)) { - var action = successor.Key; - var state = successor.Value; + var action = successor.Item1; + var state = successor.Item2; var next = CreateNode(node, state); next.Action = action; diff --git a/SpriteCompiler/AI/SearchProblem.cs b/SpriteCompiler/AI/SearchProblem.cs index d6941fb..688bd42 100644 --- a/SpriteCompiler/AI/SearchProblem.cs +++ b/SpriteCompiler/AI/SearchProblem.cs @@ -22,7 +22,7 @@ namespace SpriteCompiler.AI this.heuristicFn = heuristicFn; } - public IDictionary Successors(S state) + public IEnumerable> Successors(S state) { return successorFn.Successors(state); } diff --git a/SpriteCompiler/Problem/SpriteGeneratorSuccessorFunction.cs b/SpriteCompiler/Problem/SpriteGeneratorSuccessorFunction.cs index 0d37414..69600e4 100644 --- a/SpriteCompiler/Problem/SpriteGeneratorSuccessorFunction.cs +++ b/SpriteCompiler/Problem/SpriteGeneratorSuccessorFunction.cs @@ -7,7 +7,7 @@ public sealed class SpriteGeneratorSuccessorFunction : ISuccessorFunction { - public IDictionary Successors(SpriteGeneratorState state) + public IEnumerable> Successors(SpriteGeneratorState state) { // This is the work-horse of the compiler. For a given state we need to enumerate all of the // potential next operations. @@ -26,7 +26,8 @@ // 3. Single-byte at the end of a solid run // a. If no registers are 8-bit, LDA #Imm/STA 0,s (8 cycles, sets Acc) // b. If any reg is already 8-bit, LDA #imm/PHA (6 cycles) - + // + // We al var actions = new List(); var bytes = state.Bytes.ToDictionary(x => x.Offset, x => x); @@ -117,7 +118,7 @@ } // Run through the actions to create a dictionary - return actions.ToDictionary(x => x, x => x.Apply(state)); + return actions.Select(x => Tuple.Create(x, x.Apply(state))); } private Func WithinRangeOf(int addr, int range)