Ejemplo n.º 1
0
 void AddState(KnightSpearmanState state)
 {
     if (!stack.Contains(state))
     {
         stack.Push(state);
     }
 }
Ejemplo n.º 2
0
        public LinkedList <KnightSpearmanState> Solve(KnightSpearmanState initial)
        {
            closed = new List <KnightSpearmanState>();
            AddState(initial);

            while (HasElements())
            {
                KnightSpearmanState state = NextState();

                if (state.IsSolution())
                {
                    return(FindPath(state));
                }

                closed.Add(state);

                LinkedList <KnightSpearmanState> moves = state.GetPossibleMoves();

                foreach (KnightSpearmanState move in moves)
                {
                    if (!closed.Contains(move))
                    {
                        AddState(move);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            KnightSpearmanState knightsSpearman    = new KnightSpearmanState();
            LinkedList <KnightSpearmanState> moves = null;
            DFS solver = new DFS();

            moves = solver.Solve(knightsSpearman);

            int n = 1;

            foreach (KnightSpearmanState state in moves)
            {
                KnightSpearmanState nextState = (KnightSpearmanState)state;

                Console.WriteLine(n.ToString() + " ");

                if (nextState.IsSolution())
                {
                    Console.WriteLine("Solution");
                }

                Console.WriteLine(nextState.ToString());
                n++;
            }

            Console.ReadKey();
        }
Ejemplo n.º 4
0
        private LinkedList <KnightSpearmanState> FindPath(KnightSpearmanState solution)
        {
            LinkedList <KnightSpearmanState> path = new LinkedList <KnightSpearmanState>();

            while (solution != null)
            {
                path.AddFirst(solution);
                solution = solution.Parent;
            }

            return(path);
        }
Ejemplo n.º 5
0
 public KnightSpearmanState(KnightSpearmanState parent,
                            Side knight1, Side spearman1,
                            Side knight2, Side spearman2,
                            Side knight3, Side spearman3)
 {
     this.Parent    = parent;
     this.knight1   = knight1;
     this.spearman1 = spearman1;
     this.knight2   = knight2;
     this.spearman2 = spearman2;
     this.knight3   = knight3;
     this.spearman3 = spearman3;
 }
Ejemplo n.º 6
0
        public override bool Equals(object obj)
        {
            if (obj == null || !(obj is KnightSpearmanState))
            {
                return(false);
            }

            KnightSpearmanState knightsSpearman = (KnightSpearmanState)obj;

            return
                (knight1.KeepSide == knightsSpearman.knight1.KeepSide &&
                 spearman1.KeepSide == knightsSpearman.spearman1.KeepSide &&
                 knight2.KeepSide == knightsSpearman.knight2.KeepSide &&
                 spearman2.KeepSide == knightsSpearman.spearman2.KeepSide &&
                 knight3.KeepSide == knightsSpearman.knight3.KeepSide &&
                 spearman3.KeepSide == knightsSpearman.spearman3.KeepSide);
        }
Ejemplo n.º 7
0
 public KnightSpearmanState(KnightSpearmanState parent)
 {
     this.parent = parent;
 }