Esempio n. 1
0
        public Node(Jug jug1, Jug jug2, Jug jug3, int depth)
        {
            Jugs = new List <Jug>
            {
                jug1,
                jug2,
                jug3
            };

            Depth = depth;

            Heuristc = Depth > 0 ? (Math.Abs(Jugs.ElementAt(0).Current - 4)) + (Math.Abs(Jugs.ElementAt(1).Current - 4)) : 0;

            Function = Heuristc + Depth;
        }
Esempio n. 2
0
        public List <Node> AvailableTransitions(Node node)
        {
            List <Node> transitions = new List <Node>();

            var jugOne   = node.Jugs.ElementAt(0);
            var jugTwo   = node.Jugs.ElementAt(1);
            var jugThree = node.Jugs.ElementAt(2);

            if (!jugOne.IsFull())
            {
                if (!node.Jugs.ElementAt(1).IsEmpty())
                {
                    var jug1EmptySpace = node.Jugs.ElementAt(0).EmptySpace;
                    var jug2Current    = node.Jugs.ElementAt(1).Current;

                    if (jug1EmptySpace >= jug2Current)
                    {
                        var jug1 = new Jug(node.Jugs.ElementAt(0).Capacity, node.Jugs.ElementAt(0).Current);
                        var jug2 = new Jug(node.Jugs.ElementAt(1).Capacity, node.Jugs.ElementAt(1).Current);

                        jug1.UpdateContent(jug2.Current);
                        jug2.EmptyJug();
                        transitions.Add(new Node(jug1, jug2, node.Jugs.ElementAt(2), node.Depth + 1));
                    }

                    else
                    {
                        var jug1 = new Jug(node.Jugs.ElementAt(0).Capacity, node.Jugs.ElementAt(0).Current);
                        var jug2 = new Jug(node.Jugs.ElementAt(1).Capacity, node.Jugs.ElementAt(1).Current);

                        jug2.Current -= jug1.EmptySpace;
                        jug1.FillJug();
                        transitions.Add(new Node(jug1, jug2, node.Jugs.ElementAt(2), node.Depth + 1));
                    }
                }

                if (!node.Jugs.ElementAt(2).IsEmpty())
                {
                    var jug1EmptySpace = node.Jugs.ElementAt(0).EmptySpace;
                    var jug3Current    = node.Jugs.ElementAt(2).Current;

                    if (jug1EmptySpace >= jug3Current)
                    {
                        var jug1 = new Jug(node.Jugs.ElementAt(0).Capacity, node.Jugs.ElementAt(0).Current);
                        var jug3 = new Jug(node.Jugs.ElementAt(2).Capacity, node.Jugs.ElementAt(2).Current);

                        jug1.UpdateContent(jug3.Current);
                        jug3.EmptyJug();
                        transitions.Add(new Node(jug1, node.Jugs.ElementAt(1), jug3, node.Depth + 1));
                    }

                    else
                    {
                        var jug1 = new Jug(node.Jugs.ElementAt(0).Capacity, node.Jugs.ElementAt(0).Current);
                        var jug3 = new Jug(node.Jugs.ElementAt(2).Capacity, node.Jugs.ElementAt(2).Current);

                        jug3.Current -= jug1.EmptySpace;
                        jug1.FillJug();
                        transitions.Add(new Node(jug1, node.Jugs.ElementAt(1), jug3, node.Depth + 1));
                    }
                }
            }

            if (!jugTwo.IsFull())
            {
                if (!jugOne.IsEmpty())
                {
                    var jug2EmptySpace = node.Jugs.ElementAt(1).EmptySpace;
                    var jug1Current    = node.Jugs.ElementAt(0).Current;

                    if (jug2EmptySpace >= jug1Current)
                    {
                        var jug1 = new Jug(node.Jugs.ElementAt(0).Capacity, node.Jugs.ElementAt(0).Current);
                        var jug2 = new Jug(node.Jugs.ElementAt(1).Capacity, node.Jugs.ElementAt(1).Current);

                        jug2.UpdateContent(jug1.Current);
                        jug1.EmptyJug();
                        transitions.Add(new Node(jug1, jug2, node.Jugs.ElementAt(2), node.Depth + 1));
                    }

                    else
                    {
                        var jug1 = new Jug(node.Jugs.ElementAt(0).Capacity, node.Jugs.ElementAt(0).Current);
                        var jug2 = new Jug(node.Jugs.ElementAt(1).Capacity, node.Jugs.ElementAt(1).Current);

                        jug1.Current -= jug2.EmptySpace;
                        jug2.FillJug();
                        transitions.Add(new Node(jug1, jug2, node.Jugs.ElementAt(2), node.Depth + 1));
                    }
                }

                if (!jugThree.IsEmpty())
                {
                    var jug2EmptySpace = node.Jugs.ElementAt(1).EmptySpace;
                    var jug3Current    = node.Jugs.ElementAt(1).Current;

                    if (jug2EmptySpace >= jug3Current)
                    {
                        var jug2 = new Jug(node.Jugs.ElementAt(1).Capacity, node.Jugs.ElementAt(1).Current);
                        var jug3 = new Jug(node.Jugs.ElementAt(2).Capacity, node.Jugs.ElementAt(2).Current);

                        jug2.UpdateContent(jug3.Current);
                        jug3.EmptyJug();
                        transitions.Add(new Node(node.Jugs.ElementAt(0), jug2, jug3, node.Depth + 1));
                    }

                    else
                    {
                        var jug2 = new Jug(node.Jugs.ElementAt(1).Capacity, node.Jugs.ElementAt(1).Current);
                        var jug3 = new Jug(node.Jugs.ElementAt(2).Capacity, node.Jugs.ElementAt(2).Current);

                        jug3.Current -= jug2.EmptySpace;
                        jug2.FillJug();
                        transitions.Add(new Node(node.Jugs.ElementAt(0), jug2, jug3, node.Depth + 1));
                    }
                }
            }

            if (!jugThree.IsFull())
            {
                if (!jugOne.IsEmpty())
                {
                    var jug3EmptySpace = jugThree.EmptySpace;
                    var jug1Current    = jugOne.Current;

                    if (jug3EmptySpace >= jug1Current)
                    {
                        var jug3 = new Jug(jugThree.Capacity, jugThree.Current);
                        var jug1 = new Jug(jugOne.Capacity, jugOne.Current);

                        jug3.UpdateContent(jug1.Current);
                        jug1.EmptyJug();

                        transitions.Add(new Node(jug1, jugTwo, jug3, node.Depth + 1));
                    }

                    else
                    {
                        var jug3 = new Jug(jugThree.Capacity, jugThree.Current);
                        var jug1 = new Jug(jugOne.Capacity, jugOne.Current);

                        jug1.Current -= jug3.EmptySpace;
                        jug3.FillJug();

                        transitions.Add(new Node(jug1, jugTwo, jug3, node.Depth + 1));
                    }
                }

                if (!jugTwo.IsEmpty())
                {
                    var jug3EmptySpace = jugThree.EmptySpace;
                    var jug2Current    = jugTwo.Current;

                    if (jug3EmptySpace >= jug2Current)
                    {
                        var jug3 = new Jug(jugThree.Capacity, jugThree.Current);
                        var jug2 = new Jug(jugTwo.Capacity, jugTwo.Current);

                        jug3.UpdateContent(jug2.Current);
                        jug2.EmptyJug();

                        transitions.Add(new Node(jugOne, jug2, jug3, node.Depth + 1));
                    }

                    else
                    {
                        var jug3 = new Jug(jugThree.Capacity, jugThree.Current);
                        var jug2 = new Jug(jugTwo.Capacity, jugTwo.Current);

                        jug2.Current -= jug3.EmptySpace;
                        jug3.FillJug();

                        transitions.Add(new Node(jugOne, jug2, jug3, node.Depth + 1));
                    }
                }
            }

            return(GetOnlyAvailableTransitions(transitions));
        }