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;
- // fixed variable used throughout the whole robot
- private final int endZone = 10;
- private final int bar1pos = 1;
- private final int bar2pos = 2;
- private final int bar3pos = 3;
- public RobotControl(Robot r) {
- this.r = r;
- if (Robot.assessment == true) {
- r.speedUp(5);
- }
- }
- public void control(int barHeights[], int blockHeights[]) {
- run(barHeights, blockHeights);
- }
- public void run(int barHeights[], int blockHeights[]) {
- int arm1 = 2; // Initial height of arm 1
- int arm2 = 1; // Initial width of arm 2
- int arm3 = 0; // Initialnt x = 0; depth of arm 3
- int safeDown = 0;
- int targetPosition = 0;
- int currBlockHt;
- int maxTargetHt;
- int contractAmt = 0; // arm 2 contracting
- int blockPosition = 7;
- int currentBar = 0;
- int pickHt = 0;
- int target1Ht = 0;
- int target2Ht = 0;
- int maxtargetHt = 0;
- int maxBlockHt = 0;
- int block3Go = 0;
- int safeArmHt = 0;
- int targetBarIndex = 0;
- int sourceHt = findSourceHt(blockHeights);
- int maxBarHt = findMaxBarHt(blockHeights);
- block3Go = block3Destination(blockHeights, block3Go);
- int clearence = 12;
- for (int i = blockHeights.length - 1; i >= 0; i--) {
- currBlockHt = blockHeights[i];
- // CLEARENCE / OPTIMIZING
- //*************************************************************************************************
- safeArmHt = optimizingArmHt(arm2, target1Ht, target2Ht, targetBarIndex, maxBarHt, sourceHt, barHeights,
- blockHeights);
- if (i < blockHeights.length - 1) {
- while (arm3 > 0 && pickHt < safeArmHt) {
- r.raise();
- arm1--;
- pickHt++;
- }
- }
- while (pickHt < safeArmHt) {
- r.up();
- pickHt++;
- arm3++;
- }
- arm2 = arm2ToEnd(arm2);
- while (pickHt < safeArmHt) {
- r.up();
- arm3++;
- pickHt++;
- }
- r.pick();
- sourceHt -= currBlockHt;
- //********************************************************************************************
- maxTargetHt = findDestinationHt(maxBarHt, currBlockHt, target2Ht, target1Ht);
- if ()
- // System.out.println("Debug 1: height(arm1)= " + arm1 + " width (arm2) = " +
- // arm2 + " depth (arm3) =" + arm3);
- //
- // // this will need to be updated each time a block is dropped off
- // int extendAmt = 10;
- //
- // // Bring arm 2 to column 10
- // while (arm2 < extendAmt) {
- // r.extend();
- // arm2++;
- // }
- //
- // System.out.println("Debug 2: height(arm1)= " + arm1 + " width (arm2) = " +
- // arm2 + " depth (arm3) =" + arm3);
- //
- // while (arm1 - arm3 > sourceHt + 1) {
- // // lowering third arm
- // r.lower();
- // arm3++;
- // }
- //
- // // picking the topmost block
- // r.pick();
- //
- // sourceHt -= currBlockHt;
- //
- // // raising third arm all the way until arm3 becomes 0
- // while (arm3 > 0) {
- // r.raise();
- // arm3--;
- // }
- //
- // System.out.println(currBlockHt);
- //
- //
- while (contractAmt > 0) {
- r.contract();
- contractAmt--;
- arm2--;
- }
- System.out.println("Debug 4: height(arm1)= " + arm1 + " width (arm2) = " + arm2 + " depth (arm3) =" + arm3);
- System.out.printf("currentBarHeight: %d%n", barHeights[currentBar]);
- // lowering third arm
- if (currBlockHt == 3) {
- while ((arm1 - 1) - arm3 - currBlockHt > barHeights[currentBar]) {
- r.lower();
- arm3++;
- }
- barHeights[currentBar++] += currBlockHt;
- } else if (currBlockHt == 2) {
- while ((arm1 - 1) - arm3 - currBlockHt > target2Ht) {
- r.lower();
- arm3++;
- }
- target2Ht += currBlockHt;
- } else if (currBlockHt == 1) {
- while ((arm1 - 1) - arm3 - currBlockHt > target1Ht) {
- r.lower();
- arm3++;
- }
- target1Ht++;
- }
- System.out.println("Debug 5: height(arm1)= " + arm1 + " width (arm2) = " + arm2 + " depth (arm3) =" + arm3);
- // dropping the block
- r.drop();
- // The height of currentBar increases by block just placed
- // raising the third arm all the way
- while (arm3 > 0) {
- r.raise();
- arm3--;
- }
- System.out.println("Debug 6: height(arm1)= " + arm1 + " width (arm2) = " + arm2 + " depth (arm3) =" + arm3);
- }
- }
- // **** Method used ****
- public int block3Destination(int[] blockHeights, int firstLargeBlockIndex) {
- {
- for (int i = 0; i < blockHeights.length; i++) {
- if (blockHeights[i] == 3) {
- firstLargeBlockIndex = i;
- }
- }
- }
- return firstLargeBlockIndex;
- }
- // used to send arm 2 to last position
- public int arm2ToEnd(int arm2) {
- if (arm2 < endZone) {
- r.extend();
- arm2++;
- }
- return arm2;
- }
- // method used to find height of source
- public int findSourceHt(int[] blockHeights) {
- int sourceHt = 0;
- for (int i = 0; i < blockHeights.length; i++) {
- }
- return sourceHt;
- }
- // method used to locate the highest bar
- public int findMaxBarHt(int[] barHeights) {
- int maxHt = 0;
- for (int i = 0; i < barHeights.length; i++) {
- if (barHeights[i] > maxHt) {
- maxHt = barHeights[i];
- }
- }
- return maxHt;
- }
- public int optimizingArmHt(int arm2, int target1Ht, int target2Ht, int targetBarIndex, int maxBarHt, int sourceHt,
- int[] barHeights, int[] blockHeights) {
- // optimize for minimum move
- int optArmHt = 0;
- if (arm2 == bar1pos) {
- optArmHt = Math.max(target1Ht, target2Ht);
- optArmHt = Math.max(optArmHt, maxBarHt);
- } else if (arm2 == bar2pos) {
- optArmHt = Math.max(target2Ht, maxBarHt);
- } else {
- optArmHt = barHeights[targetBarIndex];
- for (int j = targetBarIndex; j < barHeights.length; j++) {
- optArmHt = Math.max(optArmHt, barHeights[j]);
- }
- }
- if (arm2 <= bar2pos) {
- optArmHt = Math.max(optArmHt, maxBarHt);
- }
- optArmHt = Math.max(optArmHt, sourceHt);
- return optArmHt;
- }
- public int findDestinationHt(int maxBarHt, int currBlockHt, int target2Ht, int target1Ht) {
- int maxTargetHt = 0;
- if (currBlockHt == 2) {
- maxTargetHt = Math.max(maxBarHt, target2Ht);
- } else if (currBlockHt == 1) {
- maxTargetHt = Math.max(target2Ht, target1Ht);
- }
- return maxTargetHt;
- }
- }
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement