Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. class RobotControl {
  4. private Robot r;
  5. public static StringBuilder sb;
  6.  
  7. // fixed variable used throughout the whole robot
  8. private final int endZone = 10;
  9. private final int bar1pos = 1;
  10. private final int bar2pos = 2;
  11. private final int bar3pos = 3;
  12.  
  13. public RobotControl(Robot r) {
  14. this.r = r;
  15. if (Robot.assessment == true) {
  16.  
  17. r.speedUp(5);
  18. }
  19. }
  20.  
  21. public void control(int barHeights[], int blockHeights[]) {
  22.  
  23. run(barHeights, blockHeights);
  24.  
  25. }
  26.  
  27. public void run(int barHeights[], int blockHeights[]) {
  28.  
  29. int arm1 = 2; // Initial height of arm 1
  30. int arm2 = 1; // Initial width of arm 2
  31. int arm3 = 0; // Initialnt x = 0; depth of arm 3
  32. int safeDown = 0;
  33. int targetPosition = 0;
  34. int currBlockHt;
  35. int maxTargetHt;
  36. int contractAmt = 0; // arm 2 contracting
  37. int blockPosition = 7;
  38. int currentBar = 0;
  39. int pickHt = 0;
  40. int target1Ht = 0;
  41. int target2Ht = 0;
  42. int maxtargetHt = 0;
  43. int maxBlockHt = 0;
  44. int block3Go = 0;
  45. int safeArmHt = 0;
  46. int targetBarIndex = 0;
  47. int sourceHt = findSourceHt(blockHeights);
  48. int maxBarHt = findMaxBarHt(blockHeights);
  49. block3Go = block3Destination(blockHeights, block3Go);
  50.  
  51. int clearence = 12;
  52.  
  53. for (int i = blockHeights.length - 1; i >= 0; i--) {
  54.  
  55. currBlockHt = blockHeights[i];
  56.  
  57. // CLEARENCE / OPTIMIZING
  58. //*************************************************************************************************
  59. safeArmHt = optimizingArmHt(arm2, target1Ht, target2Ht, targetBarIndex, maxBarHt, sourceHt, barHeights,
  60. blockHeights);
  61.  
  62. if (i < blockHeights.length - 1) {
  63.  
  64. while (arm3 > 0 && pickHt < safeArmHt) {
  65.  
  66. r.raise();
  67. arm1--;
  68. pickHt++;
  69.  
  70. }
  71. }
  72.  
  73. while (pickHt < safeArmHt) {
  74.  
  75. r.up();
  76. pickHt++;
  77. arm3++;
  78. }
  79.  
  80. arm2 = arm2ToEnd(arm2);
  81.  
  82. while (pickHt < safeArmHt) {
  83. r.up();
  84. arm3++;
  85. pickHt++;
  86. }
  87.  
  88. r.pick();
  89. sourceHt -= currBlockHt;
  90.  
  91. //********************************************************************************************
  92.  
  93.  
  94.  
  95. maxTargetHt = findDestinationHt(maxBarHt, currBlockHt, target2Ht, target1Ht);
  96.  
  97. if ()
  98.  
  99. // System.out.println("Debug 1: height(arm1)= " + arm1 + " width (arm2) = " +
  100. // arm2 + " depth (arm3) =" + arm3);
  101. //
  102.  
  103. // // this will need to be updated each time a block is dropped off
  104. // int extendAmt = 10;
  105. //
  106. // // Bring arm 2 to column 10
  107. // while (arm2 < extendAmt) {
  108. // r.extend();
  109. // arm2++;
  110. // }
  111. //
  112. // System.out.println("Debug 2: height(arm1)= " + arm1 + " width (arm2) = " +
  113. // arm2 + " depth (arm3) =" + arm3);
  114. //
  115. // while (arm1 - arm3 > sourceHt + 1) {
  116. // // lowering third arm
  117. // r.lower();
  118. // arm3++;
  119. // }
  120. //
  121. // // picking the topmost block
  122. // r.pick();
  123. //
  124. // sourceHt -= currBlockHt;
  125. //
  126. // // raising third arm all the way until arm3 becomes 0
  127. // while (arm3 > 0) {
  128. // r.raise();
  129. // arm3--;
  130. // }
  131. //
  132. // System.out.println(currBlockHt);
  133. //
  134. //
  135.  
  136. while (contractAmt > 0) {
  137. r.contract();
  138. contractAmt--;
  139. arm2--;
  140. }
  141.  
  142. System.out.println("Debug 4: height(arm1)= " + arm1 + " width (arm2) = " + arm2 + " depth (arm3) =" + arm3);
  143.  
  144. System.out.printf("currentBarHeight: %d%n", barHeights[currentBar]);
  145. // lowering third arm
  146. if (currBlockHt == 3) {
  147. while ((arm1 - 1) - arm3 - currBlockHt > barHeights[currentBar]) {
  148. r.lower();
  149. arm3++;
  150. }
  151. barHeights[currentBar++] += currBlockHt;
  152. } else if (currBlockHt == 2) {
  153. while ((arm1 - 1) - arm3 - currBlockHt > target2Ht) {
  154. r.lower();
  155. arm3++;
  156. }
  157. target2Ht += currBlockHt;
  158. } else if (currBlockHt == 1) {
  159. while ((arm1 - 1) - arm3 - currBlockHt > target1Ht) {
  160. r.lower();
  161. arm3++;
  162. }
  163. target1Ht++;
  164. }
  165.  
  166. System.out.println("Debug 5: height(arm1)= " + arm1 + " width (arm2) = " + arm2 + " depth (arm3) =" + arm3);
  167.  
  168. // dropping the block
  169. r.drop();
  170.  
  171. // The height of currentBar increases by block just placed
  172.  
  173. // raising the third arm all the way
  174. while (arm3 > 0) {
  175. r.raise();
  176. arm3--;
  177.  
  178. }
  179. System.out.println("Debug 6: height(arm1)= " + arm1 + " width (arm2) = " + arm2 + " depth (arm3) =" + arm3);
  180.  
  181. }
  182. }
  183.  
  184. // **** Method used ****
  185.  
  186. public int block3Destination(int[] blockHeights, int firstLargeBlockIndex) {
  187. {
  188. for (int i = 0; i < blockHeights.length; i++) {
  189. if (blockHeights[i] == 3) {
  190. firstLargeBlockIndex = i;
  191. }
  192. }
  193. }
  194. return firstLargeBlockIndex;
  195. }
  196.  
  197. // used to send arm 2 to last position
  198. public int arm2ToEnd(int arm2) {
  199. if (arm2 < endZone) {
  200. r.extend();
  201. arm2++;
  202. }
  203. return arm2;
  204. }
  205.  
  206. // method used to find height of source
  207. public int findSourceHt(int[] blockHeights) {
  208.  
  209. int sourceHt = 0;
  210. for (int i = 0; i < blockHeights.length; i++) {
  211.  
  212. }
  213. return sourceHt;
  214. }
  215.  
  216. // method used to locate the highest bar
  217. public int findMaxBarHt(int[] barHeights) {
  218.  
  219. int maxHt = 0;
  220. for (int i = 0; i < barHeights.length; i++) {
  221. if (barHeights[i] > maxHt) {
  222. maxHt = barHeights[i];
  223. }
  224. }
  225. return maxHt;
  226.  
  227. }
  228.  
  229. public int optimizingArmHt(int arm2, int target1Ht, int target2Ht, int targetBarIndex, int maxBarHt, int sourceHt,
  230. int[] barHeights, int[] blockHeights) {
  231.  
  232. // optimize for minimum move
  233. int optArmHt = 0;
  234.  
  235. if (arm2 == bar1pos) {
  236. optArmHt = Math.max(target1Ht, target2Ht);
  237. optArmHt = Math.max(optArmHt, maxBarHt);
  238.  
  239. } else if (arm2 == bar2pos) {
  240.  
  241. optArmHt = Math.max(target2Ht, maxBarHt);
  242. } else {
  243. optArmHt = barHeights[targetBarIndex];
  244. for (int j = targetBarIndex; j < barHeights.length; j++) {
  245.  
  246. optArmHt = Math.max(optArmHt, barHeights[j]);
  247. }
  248.  
  249. }
  250.  
  251. if (arm2 <= bar2pos) {
  252. optArmHt = Math.max(optArmHt, maxBarHt);
  253.  
  254. }
  255. optArmHt = Math.max(optArmHt, sourceHt);
  256. return optArmHt;
  257. }
  258.  
  259. public int findDestinationHt(int maxBarHt, int currBlockHt, int target2Ht, int target1Ht) {
  260.  
  261. int maxTargetHt = 0;
  262. if (currBlockHt == 2) {
  263. maxTargetHt = Math.max(maxBarHt, target2Ht);
  264. } else if (currBlockHt == 1) {
  265. maxTargetHt = Math.max(target2Ht, target1Ht);
  266.  
  267. }
  268. return maxTargetHt;
  269.  
  270. }
  271.  
  272. }
  273.  
  274. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement