Ejemplo n.º 1
0
        public override bool Equals(Object obj)
        {
            BoxState s = obj as BoxState;

            if (object.ReferenceEquals(s, null))
            {
                return(false);
            }

            return(this.IsBoxStateEquals(s));
        }
Ejemplo n.º 2
0
        public bool HasPath(BoxState end)
        {
            if (!ValidEnd(end))
            {
                return(false);
            }

            if (this.queue.FindSameBoxState(end).Count > 0)
            {
                return(true);
            }

            return(RunToStop(state => state.IsBoxStateEquals(end)));
        }
Ejemplo n.º 3
0
        public BoxMovingStep(BoxState state, int movingBoxIndex, BoxMovingDirection boxMovingDirection)
        {
            if (state == null)
            {
                throw new ArgumentNullException("state");
            }

            if (movingBoxIndex < 0 || movingBoxIndex >= state.BoxCount)
            {
                throw new ArgumentOutOfRangeException("movingBoxIndex");
            }

            Orignal         = state;
            MovingBoxIndex  = movingBoxIndex;
            MovingDirection = boxMovingDirection;
        }
Ejemplo n.º 4
0
        public List <T> FindSameBoxState(BoxState state)
        {
            if (state == null)
            {
                return(new List <T>());
            }

            var hashcode = state.GetBoxStateHashCode();

            List <T> indexedList;

            if (this.indexedHash.TryGetValue(hashcode, out indexedList))
            {
                return(indexedList.FindAll(s => s.IsBoxStateEquals(state)));
            }

            return(new List <T>());
        }
Ejemplo n.º 5
0
        public bool IsBoxStateEquals(BoxState state)
        {
            if (state == null || state.boxs.Length != this.boxs.Length)
            {
                return(false);
            }

            var s = boxs.ToList();
            var d = state.boxs.ToList();

            for (int i = 0; i < boxs.Length; i++)
            {
                if (!d.Remove(s[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 6
0
        public List <BoxState> FindPath(BoxState end)
        {
            List <BoxState> path = new List <BoxState>();

            if (this.HasPath(end))
            {
                BoxState find = this.queue.FindSameBoxState(end).First();

                path.Add(find);

                while (find.Parent != null)
                {
                    path.Add(find.Parent);
                    find = find.Parent;
                }

                path.Reverse();
            }
            return(path);
        }
Ejemplo n.º 7
0
 private bool ValidEnd(BoxState end)
 {
     if (end == null)
     {
         throw new ArgumentNullException("end");
     }
     if (end.IsOverlapping)
     {
         return(false);
     }
     if (start.BoxCount != end.BoxCount)
     {
         return(false);
     }
     if (!end.IsMeetMap(map))
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            Map map = new Map(new Point[] { new Point(0, 0), new Point(0, 1), new Point(3, 0), new Point(4, 0), new Point(5, 0), new Point(1, 3)
                                            , new Point(3, 2), new Point(3, 4) });

            map.XDownBound = 0;
            map.XUpBound   = 5;
            map.YDownBound = 0;
            map.YUpBound   = 4;

            KakuBoxState start = new KakuBoxState(new Point(5, 4), new Point[] { new Point(1, 2), new Point(3, 3), new Point(4, 2) });
            BoxState     end   = new BoxState(new Point[] { new Point(2, 1), new Point(2, 2), new Point(2, 4) });

            KakuBoxMovingFinder finder = new KakuBoxMovingFinder(map, start);

            Stopwatch sw = new Stopwatch();

            sw.Start();

            var path = finder.FindPath(end);

            sw.Stop();

            if (path.Count > 0)
            {
                Console.WriteLine("Find it!");

                foreach (var state in path)
                {
                    Console.WriteLine(state.ToString());
                }
            }
            else
            {
                Console.WriteLine("No answer. Are you kidding me!");
            }
            Console.WriteLine("Total used time:{0}", sw.Elapsed.TotalSeconds);
            Console.ReadLine();
        }