Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- class RobotControl
- {
- private Robot r;
- public static StringBuilder sb;
- public RobotControl(Robot r)
- {
- this.r = r;
- if (Robot.assessment == true)
- {
- r.speedUp(5);
- }
- }
- public void control(int barHeights[], int blockHeights[])
- {
- //sampleControlMechanism(barHeights, blockHeights);
- run(barHeights, blockHeights);
- }
- //Beginning of the good stuff :^]
- public static int extendAmt = 10; //static variable for loop to refer to when initally extending the arm back to the tower
- int sourceHt = 0; //declaring blank integer to be used in to find the size of the block tower via scanning the array
- int blockHt = 0; //declaring blank integer to be used in for finding the size of the block being moved by the robot
- int h = 0; //declaring blank integer for height of robot's first arm
- int w = 0;//declaring blank integer for width of robot's second arm
- int contractAmt = 0; //declaring blank integer for amount of spaces the robot's second arm must contract
- int blockPosition = 7; //declaring initial position that the robot must place a block of 3 via ruleset of assignment
- int maxOfAllColumns = 0; //declaring blank integer to be used to find the maximum height of all columns
- int columnHt1 = 0; //declaring blank int value for height of first column (where 1's are placed)
- int columnHt2 = 0; //declaring blank value for height of second column (where 2's are placed)
- public int adjustHt() //method that decreases the height of sourceHt(The block tower) by the size of the block taken off by robot arm
- {
- return sourceHt -= blockHt;
- }
- public void robotRaise() //method used to raised the robots arm depending on how large the sourceHt is
- {
- while (h < sourceHt + 1)
- {
- // Raising 1
- r.up();
- // Current height of arm1 being incremented by 1
- h++;
- }
- }
- public void robotContract() //method that contains the switch statement that tells the robot how many spaces to move it's arm back to return to the block tower
- {
- switch (blockHt) {
- case 1:
- contractAmt = 9;
- break;
- case 2:
- contractAmt = 8;
- break;
- case 3:
- contractAmt = blockPosition;
- blockPosition --;
- break;
- }
- while (contractAmt > 0)
- {
- r.contract();
- contractAmt--;
- w--;
- }
- }
- public void checkHt() //method to check if the heights of the locations where blocks of size 1's and 2's are placed aren't higher than the columns, only needed for custom tests
- {
- if (maxOfAllColumns < columnHt1)
- {
- maxOfAllColumns = columnHt1;
- }
- if (maxOfAllColumns < columnHt2)
- {
- maxOfAllColumns = columnHt2;
- }
- }
- public void run(int barHeights[],int blockHeights[])
- {
- h = 2; // Initial height of arm 1
- w = 1; // Initial width of arm 2
- int d = 0; // Initial depth of arm 3
- int currentBar = 0; //blank int for location of current bar
- int counter = 0; //blank integer value to be used in the loop to count the amount of times the loop has run
- int initalMax = 0; //blank int that will find the tallest column from the array without any changes via a loop outside of robot loop
- int maxAfterBlockPlace = 0; //blank int that will find the tallest column after blocks have been placed by robot via loop within the robot loop
- for (int x = 0; x < blockHeights.length; x++) //For loop to find the height of the tower by adding together the sizes of each block in the array
- {
- sourceHt += blockHeights[x];
- }
- for (int y = 0; y < barHeights.length; y++) //for loop that runs once for each int value within the array barHeights
- {
- for (int n = 0; n < barHeights.length; n++) //inner loop that runs for each int value that is in the array
- {
- if (barHeights[n] > initalMax) //inner inner loop that checks and delcares initalMax to be the largest value within the array
- {
- initalMax = barHeights[n];
- }
- }
- }
- while (sourceHt > 0) //Start of robot loop that stops when the block tower is empty
- {
- blockHt = blockHeights[blockHeights.length - (counter +1)]; //statement to make sure that blockHt is the current block that the robot must pick up
- maxAfterBlockPlace = 0;
- for (int n = currentBar; n < barHeights.length; n++) //loop that runs through each now updated heights of the currentBar varibles and assigns the largest to maxAfterBlockPlace
- {
- if (barHeights[n] > maxAfterBlockPlace)
- {
- maxAfterBlockPlace = barHeights[n];
- }
- }
- robotRaise(); //external method
- while (w < extendAmt) //moving the arm towards the blocktower
- {
- // moving 1 step horizontally
- r.extend();
- // Current width of arm2 being incremented by 1
- w++;
- }
- while (h - d > sourceHt + 1) //lowering the arm to the top of the block
- {
- // lowering third arm
- r.lower();
- // current depth of arm 3 being incremented
- d++;
- }
- // picking the topmost block
- r.pick();
- // When you pick the top block height of source decreases
- adjustHt();
- //loop to find the max of all columns
- for (int n = 0; n < barHeights.length; n++)
- {
- if (barHeights[n] > maxOfAllColumns)
- {
- maxOfAllColumns = barHeights[n];
- }
- }
- //external method
- checkHt();
- //Switch statement that raises the block via the value of either maxOfAllColumns if it's of size 1 + 2, or by maxAfterBlockPlace if it's of size 3
- switch (blockHt)
- {
- case 1:
- while (d > 0)
- {
- if (h - d - blockHt > maxOfAllColumns)
- {
- while (d > h - maxOfAllColumns - blockHt )
- {
- r.raise();
- d--;
- }
- break;
- }
- r.raise();
- d--;
- }
- break;
- case 2:
- while (d > 0)
- {
- if (h - d - blockHt > maxOfAllColumns)
- {
- while (d > h - maxOfAllColumns - blockHt )
- {
- r.raise();
- d--;
- }
- break;
- }
- r.raise();
- d--;
- }
- break;
- case 3:
- while (d > 0)
- {
- if (h - d - blockHt > maxAfterBlockPlace)
- {
- break;
- }
- r.raise();
- d--;
- }
- break;
- }
- //switch statement to contract arm back to block tower depending on size of block
- switch (blockHt)
- {
- case 1:
- contractAmt = 9;
- break;
- case 2:
- contractAmt = 8;
- break;
- case 3:
- contractAmt = blockPosition;
- blockPosition --; //changes where the block 3 is placed since it can only be placed on the furthest column that hasn't been used
- break;
- }
- //
- while (contractAmt > 0)
- {
- r.contract();
- contractAmt--;
- w--;
- }
- // lowering third arm via switch statements for the column that the block is being placed on
- switch (blockHt)
- {
- case 1:
- while ((h-1) - d - blockHt > columnHt1)
- {
- r.lower();
- d++;
- }
- columnHt1++; //adding the size of block onto total size
- break;
- case 2:
- while ((h-1) - d - blockHt > columnHt2)
- {
- r.lower();
- d++;
- }
- columnHt2 += 2;
- break;
- case 3:
- while ((h - 1) - d - blockHt > barHeights[currentBar])
- {
- r.lower();
- d++;
- }
- barHeights[(currentBar)] = barHeights[(currentBar)] + 3; //changing the height of the column via the array
- currentBar++;
- break;
- }
- // dropping the block
- r.drop();
- //loop to raise the arm only as high as it needs to be, while taking into account the sizes of columns and the block tower height
- while (d > 0)
- {
- if(blockHt == 1 || blockHt == 2)
- {
- if ((h - d > maxOfAllColumns && h - d > sourceHt) || sourceHt == 0)
- {
- break;
- }
- }
- else
- {
- if ((h - d > initalMax && h - d > sourceHt) || sourceHt == 0)
- {
- break;
- }
- }
- r.raise();
- d--;
- }
- counter++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement