Exemple #1
0
        public void TestSolver_2_5_11()
        {
            int expected = 2;

            Bucket b1 = new Bucket(5);
            Bucket b2 = new Bucket(11);

            Solver solver = new Solver(expected);
            CommandBase current = solver.Solve(b1, b2);

            Assert.IsTrue(current.B1.Level == expected || current.B2.Level == expected);
        }
Exemple #2
0
        public void Transfer(Bucket toBucket)
        {
            if (toBucket.IsFull) return;
            if (this.IsEmpty) return;

            if (toBucket.Level + this.Level <= toBucket.Capacity)
            {
                toBucket.Level += this.Level;
                this.Level = 0;
            }
            else
            {
                this.Level -= (toBucket.Capacity - toBucket.Level);
                toBucket.Fill();
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            int s1, s2, f;

            if (args.Length == 3)
            {
                s1 = Convert.ToInt32(args[0]);
                s2 = Convert.ToInt32(args[1]);
                f = Convert.ToInt32(args[2]);
            }
            else
            {
                System.Console.Write("bucket 1 size : ");
                string input = System.Console.ReadLine();
                s1 = Convert.ToInt32(input);

                System.Console.Write("bucket 2 size : ");
                input = System.Console.ReadLine();
                s2 = Convert.ToInt32(input);

                System.Console.Write("fetch : ");
                input = System.Console.ReadLine();
                f = Convert.ToInt32(input);
            }

            Solver solver = new Solver(f);
            Bucket b1 = new Bucket(s1);
            Bucket b2 = new Bucket(s2);

            try
            {
                CommandBase solution = solver.Solve(b1, b2);

                System.Console.WriteLine(string.Format("\n\nsteps : \n"));
                foreach (string step in solution.Steps)
                {
                    System.Console.WriteLine(step);
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }

            System.Console.WriteLine(string.Format("\n\npress any key to exit."));
            System.Console.ReadKey();
        }
Exemple #4
0
 public TransferCommand(Bucket to, Bucket from, CommandBase parent)
     : base(to, from, parent)
 {
     foreach (string stp in parent.Steps)
     {
         Steps.Add(stp);
     }
 }
Exemple #5
0
        public CommandBase Solve(Bucket b1, Bucket b2)
        {
            if (_gallonsNeeded == 0) throw new Exception("Fetching nothing. Give me something to do.");

            if (b1.Capacity == 0 || b2.Capacity == 0) throw new Exception("Invalid bucket capacity. Must be more than zero.");

            if (b1.Capacity > 15 || b2.Capacity > 15) throw new Exception("Bucket is too big. Use a smaller bucket. Must be less than 15 gallons");

            if (_gallonsNeeded > b1.Capacity && _gallonsNeeded > b2.Capacity) throw new Exception("Can not fetch that much in any of the buckets.");

            if (b1.Capacity == b2.Capacity)
                throw new Exception(
                    "It is not possible to create a differential because capacities of two buckets are the same.");

            // more validation here

            Queue<CommandBase> queue = new Queue<CommandBase>();

            CommandBase rootCommand = new CommandBase(b1, b2, null);

            CommandBase current = rootCommand;

            int attempts = 0;
            while (current != null)
            {
                attempts++;

                current.Execute();

                if (current.CheckLevel(_gallonsNeeded)) break;

                if (attempts > 1000000) throw new Exception("mission impossible.");

                if (!current.B1.IsFull)
                    queue.Enqueue(new FillCommand(current.B1, current.B2, current));

                if (!current.B2.IsFull)
                    queue.Enqueue(new FillCommand(current.B2, current.B1, current));

                if (!current.B1.IsEmpty)
                    queue.Enqueue(new TransferCommand(current.B1, current.B2, current));

                if (!current.B2.IsEmpty)
                    queue.Enqueue(new TransferCommand(current.B2, current.B1, current));

                if (!current.B1.IsEmpty)
                    queue.Enqueue(new DumpCommand(current.B1, current.B2, current));

                if (!current.B2.IsEmpty)
                    queue.Enqueue(new DumpCommand(current.B2, current.B1, current));

                current = queue.Dequeue();
            }

            return current;
        }
Exemple #6
0
 public FillCommand(Bucket b1, Bucket b2, CommandBase parent)
     : base(b1, b2, parent)
 {
     foreach (string stp in parent.Steps)
     {
         Steps.Add(stp);
     }
 }
Exemple #7
0
 public CommandBase(Bucket b1, Bucket b2, CommandBase parent)
     : base(parent)
 {
     Pending = true;
     B1 = (Bucket)b1.Clone();
     B2 = (Bucket)b2.Clone();
 }
Exemple #8
0
        public void test_transfer_when_target_level_will_exceed_capacity()
        {
            Bucket b1 = new Bucket(7);
            Bucket b2 = new Bucket(13);

            b1.Level = 5;
            b2.Level = 12;
            b1.Transfer(b2);

            Assert.AreEqual(4, b1.Level);
            Assert.AreEqual(b2.Capacity, b2.Level);
        }
Exemple #9
0
        public void test_transfer_when_source_can_be_transferred_completely()
        {
            Bucket b1 = new Bucket(7);
            Bucket b2 = new Bucket(13);

            b1.Level = 5;

            b1.Transfer(b2);

            Assert.AreEqual(0, b1.Level);
            Assert.AreEqual(5, b2.Level);
        }