Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.11 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. class RobotControl
  4. {
  5. private Robot r;
  6. public static StringBuilder sb;
  7. public RobotControl(Robot r)
  8. {
  9. this.r = r;
  10. if (Robot.assessment == true)
  11. {
  12.  
  13. r.speedUp(5);
  14. }
  15. }
  16. public void control(int barHeights[], int blockHeights[])
  17. {
  18. //sampleControlMechanism(barHeights, blockHeights);
  19. run(barHeights, blockHeights);
  20. }
  21.  
  22. //Beginning of the good stuff :^]
  23.  
  24. public static int extendAmt = 10; //static variable for loop to refer to when initally extending the arm back to the tower
  25. int sourceHt = 0; //declaring blank integer to be used in to find the size of the block tower via scanning the array
  26. int blockHt = 0; //declaring blank integer to be used in for finding the size of the block being moved by the robot
  27. int h = 0; //declaring blank integer for height of robot's first arm
  28. int w = 0;//declaring blank integer for width of robot's second arm
  29. int contractAmt = 0; //declaring blank integer for amount of spaces the robot's second arm must contract
  30. int blockPosition = 7; //declaring initial position that the robot must place a block of 3 via ruleset of assignment
  31. int maxOfAllColumns = 0; //declaring blank integer to be used to find the maximum height of all columns
  32. int columnHt1 = 0; //declaring blank int value for height of first column (where 1's are placed)
  33. int columnHt2 = 0; //declaring blank value for height of second column (where 2's are placed)
  34.  
  35.  
  36. public int adjustHt() //method that decreases the height of sourceHt(The block tower) by the size of the block taken off by robot arm
  37. {
  38. return sourceHt -= blockHt;
  39.  
  40. }
  41.  
  42. public void robotRaise() //method used to raised the robots arm depending on how large the sourceHt is
  43. {
  44. while (h < sourceHt + 1)
  45. {
  46. // Raising 1
  47. r.up();
  48.  
  49. // Current height of arm1 being incremented by 1
  50. h++;
  51. }
  52.  
  53. }
  54.  
  55. 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
  56. {
  57. switch (blockHt) {
  58. case 1:
  59. contractAmt = 9;
  60. break;
  61.  
  62. case 2:
  63. contractAmt = 8;
  64. break;
  65.  
  66. case 3:
  67. contractAmt = blockPosition;
  68. blockPosition --;
  69. break;
  70. }
  71.  
  72. while (contractAmt > 0)
  73. {
  74. r.contract();
  75. contractAmt--;
  76. w--;
  77. }
  78. }
  79.  
  80. 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
  81. {
  82. if (maxOfAllColumns < columnHt1)
  83. {
  84. maxOfAllColumns = columnHt1;
  85. }
  86.  
  87. if (maxOfAllColumns < columnHt2)
  88. {
  89. maxOfAllColumns = columnHt2;
  90. }
  91.  
  92. }
  93.  
  94.  
  95. public void run(int barHeights[],int blockHeights[])
  96. {
  97. h = 2; // Initial height of arm 1
  98. w = 1; // Initial width of arm 2
  99. int d = 0; // Initial depth of arm 3
  100. int currentBar = 0; //blank int for location of current bar
  101. int counter = 0; //blank integer value to be used in the loop to count the amount of times the loop has run
  102. int initalMax = 0; //blank int that will find the tallest column from the array without any changes via a loop outside of robot loop
  103. int maxAfterBlockPlace = 0; //blank int that will find the tallest column after blocks have been placed by robot via loop within the robot loop
  104.  
  105.  
  106.  
  107.  
  108. 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
  109. {
  110. sourceHt += blockHeights[x];
  111. }
  112.  
  113. for (int y = 0; y < barHeights.length; y++) //for loop that runs once for each int value within the array barHeights
  114. {
  115. for (int n = 0; n < barHeights.length; n++) //inner loop that runs for each int value that is in the array
  116. {
  117. if (barHeights[n] > initalMax) //inner inner loop that checks and delcares initalMax to be the largest value within the array
  118. {
  119. initalMax = barHeights[n];
  120. }
  121. }
  122. }
  123.  
  124.  
  125. while (sourceHt > 0) //Start of robot loop that stops when the block tower is empty
  126. {
  127.  
  128. blockHt = blockHeights[blockHeights.length - (counter +1)]; //statement to make sure that blockHt is the current block that the robot must pick up
  129.  
  130. maxAfterBlockPlace = 0;
  131.  
  132. 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
  133. {
  134. if (barHeights[n] > maxAfterBlockPlace)
  135. {
  136. maxAfterBlockPlace = barHeights[n];
  137. }
  138. }
  139.  
  140. robotRaise(); //external method
  141.  
  142.  
  143. while (w < extendAmt) //moving the arm towards the blocktower
  144. {
  145. // moving 1 step horizontally
  146. r.extend();
  147.  
  148. // Current width of arm2 being incremented by 1
  149. w++;
  150. }
  151.  
  152. while (h - d > sourceHt + 1) //lowering the arm to the top of the block
  153. {
  154. // lowering third arm
  155. r.lower();
  156.  
  157. // current depth of arm 3 being incremented
  158. d++;
  159. }
  160.  
  161. // picking the topmost block
  162. r.pick();
  163.  
  164. // When you pick the top block height of source decreases
  165. adjustHt();
  166.  
  167. //loop to find the max of all columns
  168. for (int n = 0; n < barHeights.length; n++)
  169. {
  170. if (barHeights[n] > maxOfAllColumns)
  171. {
  172. maxOfAllColumns = barHeights[n];
  173. }
  174. }
  175.  
  176. //external method
  177. checkHt();
  178.  
  179. //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
  180. switch (blockHt)
  181. {
  182. case 1:
  183. while (d > 0)
  184. {
  185. if (h - d - blockHt > maxOfAllColumns)
  186. {
  187. while (d > h - maxOfAllColumns - blockHt )
  188. {
  189. r.raise();
  190. d--;
  191. }
  192. break;
  193. }
  194. r.raise();
  195. d--;
  196. }
  197. break;
  198. case 2:
  199. while (d > 0)
  200. {
  201. if (h - d - blockHt > maxOfAllColumns)
  202. {
  203. while (d > h - maxOfAllColumns - blockHt )
  204. {
  205. r.raise();
  206. d--;
  207. }
  208. break;
  209. }
  210. r.raise();
  211. d--;
  212. }
  213. break;
  214. case 3:
  215. while (d > 0)
  216. {
  217. if (h - d - blockHt > maxAfterBlockPlace)
  218. {
  219. break;
  220. }
  221.  
  222. r.raise();
  223. d--;
  224. }
  225. break;
  226. }
  227.  
  228.  
  229. //switch statement to contract arm back to block tower depending on size of block
  230. switch (blockHt)
  231. {
  232. case 1:
  233. contractAmt = 9;
  234. break;
  235.  
  236. case 2:
  237. contractAmt = 8;
  238. break;
  239.  
  240. case 3:
  241. contractAmt = blockPosition;
  242. blockPosition --; //changes where the block 3 is placed since it can only be placed on the furthest column that hasn't been used
  243. break;
  244. }
  245.  
  246. //
  247. while (contractAmt > 0)
  248. {
  249. r.contract();
  250. contractAmt--;
  251. w--;
  252. }
  253.  
  254.  
  255. // lowering third arm via switch statements for the column that the block is being placed on
  256. switch (blockHt)
  257. {
  258. case 1:
  259. while ((h-1) - d - blockHt > columnHt1)
  260. {
  261. r.lower();
  262. d++;
  263. }
  264. columnHt1++; //adding the size of block onto total size
  265.  
  266. break;
  267.  
  268. case 2:
  269. while ((h-1) - d - blockHt > columnHt2)
  270. {
  271. r.lower();
  272. d++;
  273. }
  274. columnHt2 += 2;
  275. break;
  276.  
  277. case 3:
  278. while ((h - 1) - d - blockHt > barHeights[currentBar])
  279. {
  280. r.lower();
  281. d++;
  282. }
  283. barHeights[(currentBar)] = barHeights[(currentBar)] + 3; //changing the height of the column via the array
  284.  
  285. currentBar++;
  286. break;
  287.  
  288.  
  289. }
  290.  
  291.  
  292. // dropping the block
  293. r.drop();
  294.  
  295. //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
  296. while (d > 0)
  297. {
  298. if(blockHt == 1 || blockHt == 2)
  299. {
  300. if ((h - d > maxOfAllColumns && h - d > sourceHt) || sourceHt == 0)
  301. {
  302. break;
  303. }
  304.  
  305. }
  306. else
  307. {
  308. if ((h - d > initalMax && h - d > sourceHt) || sourceHt == 0)
  309. {
  310. break;
  311. }
  312. }
  313. r.raise();
  314. d--;
  315. }
  316.  
  317. counter++;
  318. }
  319. }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement