C# ,How can I fix it ?

Updated on February 6, 2018 in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 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
0 on February 6, 2018

https://gist.github.com/DylanGuidry95/0dececfd765801347b8ac505e8da56bb

This is my solution I worked on for about 30 minutes. Robot 1 will all ways pick up the most he can carry as long as robot 2 can at least pick up his minimum. If the Robot can not pick up his max with Robot 2 not be able to pick up his minimum, Robot 1 will try and pick up the minimum as long as robot 2 can meet his minimum crates.

If Robot 2 cannot pick up something while still allowing robot 2 to pick up at least his minimum the program will break out of the loop.

Randomly picking how many creates to pick up may not be the best way to go about it since its not controlled very well and as you said what if it tries to pick up more than what is left. If you still wanted to keep the random you would need some kind of check to make sure there is enough crates for both robots to meet the minimum requirements.

  • Liked by
Reply
Cancel