public IEnumerable <MOVE> Solve(NPuzzle Puzzle, IEvaluation Eval) { List <NPuzzleNode> Open = new List <NPuzzleNode>(); List <NPuzzleNode> Closed = new List <NPuzzleNode>(); NPuzzleNode Goal = null; Open.Add(new NPuzzleNode(null, Puzzle, Eval)); while (Open.Count > 0) { NPuzzleNode U = Open[0]; Open.RemoveAt(0); Closed.Add(U); if (U.H == 0.0f) { Goal = U; break; } foreach (NPuzzleNode S in U.Successors()) { if (!Open.Contains(S) && !Closed.Contains(S)) { Open.Add(S); } } } return(Goal.State.PreviousMoves); }
static void Main(string[] args) { Console.BackgroundColor = ConsoleColor.Black; //Sets background and foreground color Console.ForegroundColor = ConsoleColor.Green; Console.Title = "N-Puzzle"; Console.Clear(); int size; NPuzzle board; String input; do { Console.WriteLine("What size do you want for the puzzle?"); //Tells the user to make an input of a digit input = Console.ReadLine(); } while (!Int32.TryParse(input, out size)); //If it can be parsed we will then compare it to a statement) board = new NPuzzle(size); board.DrawBoard(); //Prints the board on screen Controller controller = new Controller(board); controller.Run(); Console.WriteLine("Congatulations, you have solved it!"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(true); }
public float Evaluate(NPuzzle Puzzle) { float f = 0.0f; foreach (PlacedTile T in Puzzle.Tiles) { iVec2 Target = new iVec2(); Target.x = T.T.Number % Puzzle.Width; Target.y = T.T.Number / Puzzle.Width; f += Math.Abs(T.Coord.x - Target.x) + Math.Abs(T.Coord.y - Target.y); } return(f); }
public IEnumerable <N_Puzzle.MOVE> Solve(NPuzzle Puzzle, IEvaluation Eval) { List <NPuzzleNode> Open = new List <NPuzzleNode>(); List <NPuzzleNode> Closed = new List <NPuzzleNode>(); NPuzzleNode Goal = null; Open.Add(new NPuzzleNode(null, Puzzle, Eval)); while (Open.Count > 0) { NPuzzleNode Q = Open[0]; Open.Remove(Q); if (Q.H == 0.0f) { Goal = Q; break; } foreach (NPuzzleNode S in Q.Successors()) { if (!Closed.Contains(S)) { NPuzzleNode AlreadyExploredOpen = Open.Find(N => N.Equals(S)); if (AlreadyExploredOpen != null) { if (AlreadyExploredOpen.F > S.F) { Open.Remove(AlreadyExploredOpen); Open.InsertSorted(S); } } else { Open.InsertSorted(S); } } } Closed.Add(Q); } return(Goal.State.PreviousMoves); }
public Controller(NPuzzle game) { Game = game; }
public NPuzzleNode(NPuzzleNode _Parent, NPuzzle _State, IEvaluation Eval) { Parent = _Parent; State = _State; Evaluator = Eval; }