C# ,How can I fix it ?

Updated on January 13, 2018 in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on January 13, 2018

I just found a good challenge but I have no idea how to do it .
It says :
Write the code in C# which find the combinations like below :
Two robots R1 and R2 deliver boxes.R1 can deliver 3 or 4 boxes at a time while R2 can only deliver 1 or 2.
If there are 34 boxes in total write a program which finds all the movement combinations of the robots R1 and R2.The R1 moves first and then R2.In case  there are left 1 or 2 boxes and its R1 turn then the program ends and in the end tells that there are left x boxes because its R1 turn and it cant carry just 1 or 2. 
I was thinking to make a

 Random R1 = new Random();
            int rob1=0;
            int rob2=0;
            int count = 0;
            int boxes = 34;
            do
            {
                rob1 = R1.Next(3, 5);
                rob2 = R1.Next(1, 3);
                if (boxes< rob1)
                    break;
                    boxes= boxes – rob1;
                if (boxes < rob2)
                    break;
                boxes = boxes – rob2;
               
                count++;
                Console.WriteLine(“R1={0} and R2={1}”, rob1, rob2);
            } while (boxes> 0);
            Console.WriteLine(“It took  {0} movements to finish all the boxes”, count);
            Console.WriteLine(“Left boxes : {0}”,boxes);
          
            Console.ReadKey();

I thought it  would work but no because if it robot 1 turn  and there are 3 boxes left the random generator might
generate 4 and the robot 1 wont be able to carry these boxes and it will break , I want to make it be able to carry those boxes 

  • Liked by
Reply