Esempio n. 1
0
 private static void DoOperations(int[] operations, Bottle bottle1, Bottle bottle2)
 {
     bottle1.Empty();
     bottle2.Empty();
     foreach (var operation in operations)
     {
         if (operation == 0)
         {
             bottle1.FillToTopFromTap();                 // Fylle flaske 1 fra springen
         }
         else if (operation == 1)
         {
             bottle2.FillToTopFromTap(); // Fylle flaske 2 fra springen
         }
         else if (operation == 2)        // Tømme flaske 1 i flaske 2
         {
             if (bottle1.IsEmpty())
             {
                 break;
             }
             var success = bottle2.Fill(bottle1.Empty());
             if (!success)
             {
                 break;
             }
         }
         else if (operation == 3) // Tømme flaske 2 i flaske 1
         {
             if (bottle2.IsEmpty())
             {
                 break;
             }
             var success = bottle1.Fill(bottle2.Empty());
             if (!success)
             {
                 break;
             }
         }
         else if (operation == 4) // Fylle opp flaske 2 med flaske 1
         {
             var success = bottle2.FillToTop(bottle1);
             if (!success)
             {
                 break;
             }
         }
         else if (operation == 5) // Fylle opp flaske 1 med flaske 2
         {
             var success = bottle1.FillToTop(bottle2);
             if (!success)
             {
                 break;
             }
         }
         else if (operation == 6) // Tømme flaske 1 (kaste vannet)
         {
             bottle1.Empty();
         }
         else if (operation == 7) // Tømme flaske 2 (kaste vannet)
         {
             bottle2.Empty();
         }
     }
 }
Esempio n. 2
0
        private static void TryWithGivenNumberOfOperations(int numberOfOperations, Bottle bottle1, Bottle bottle2,
                                                           int wantedVolume)
        {
            Console.WriteLine("Prøver med " + numberOfOperations + " operasjon(er).");
            var operations = new int[numberOfOperations];

            while (true)
            {
                DoOperations(operations, bottle1, bottle2);
                CheckIfSolvedAndExitApplicationIfSo(bottle1, bottle2, wantedVolume, operations);
                var success = UpdateOperations(operations);
                if (!success)
                {
                    break;
                }
            }
        }